导出XML格式的Excel

直接先看效果:

 

实现方法: 

1. 在Excel中画出布局 ;

2. 另存为格式为“.XML”;

3. 将里面的内容拷贝出来,通过代码输出,大功告成;

代码:

  Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DateTime.Now.ToString("yyyyMMddHHmmss") + ".xml", System.Text.Encoding.GetEncoding("gbk")));
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.ContentType = "application/ms-excel";
            System.IO.StringWriter sw = new System.IO.StringWriter();

//第三步中拷贝的内容
 sw.WriteLine("<?xml version=\"1.0\"?>                                                                                                                                                    ");
            sw.WriteLine("<?mso-application progid=\"Excel.Sheet\"?>                                                                                                                                 ");
            sw.WriteLine("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"                                                                                                           ");
            sw.WriteLine(" xmlns:o=\"urn:schemas-microsoft-com:office:office\"                                                                                                                       ");
            sw.WriteLine(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\"                                                                                                                        ");
            sw.WriteLine(" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"                                                                                                                 ");
            sw.WriteLine(" xmlns:html=\"http://www.w3.org/TR/REC-html40\">                                                                                                                           ");
            sw.WriteLine(" <DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">             

........


      Response.Write(sw);
            Response.End();

 

posted @ 2012-06-27 20:18  hejiyong  阅读(230)  评论(0)    收藏  举报