Asp.net(c#)导出有表格线的Excel

[ 2013-05-04 16:45:39 | 作者: admin ]
字号: | |
p.s.挺好,解决了这个问题,而且根据导出需要,可以做个判断,导出web格式时候给头部加上css。

表格用文件流的方式输出为excel。实例代码如下:
                public static void DaochuTalbe(string TableInnerHtml, string filename)
                {
                     StringWriter sw = new StringWriter();
                     sw.WriteLine(TableInnerHtml);
                     sw.Close();
                     System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");
                     System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";
                     System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;
                     System.Web.HttpContext.Current.Response.Write(sw);
                     System.Web.HttpContext.Current.Response.End();
                }


这种方法的从本质上说并非标准的excel格式,不过把html格式的文件另存为excel的格式,然后用excel打开罢了。

打开后会发现导出的excel无表格线,白白的一片,太难看了。

通过分析excel的格式代码,我尝试在程序里增加带表格线的excel头部代码,结果顺利导出有表格线的excel.

增加的代码如下:
             /// <summary>

                /// Excel头部

                /// </summary>

                /// <returns></returns>

                public static string AddExcelHead()

                {

                     StringBuilder sb = new StringBuilder();

                     sb.Append("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");

                     sb.Append(" <head>");

                     sb.Append(" <!--[if gte mso 9]><xml>");

                     sb.Append("<x:ExcelWorkbook>");

                     sb.Append("<x:ExcelWorksheets>");

                     sb.Append("<x:ExcelWorksheet>");

                     sb.Append("<x:Name></x:Name>");

                     sb.Append("<x:WorksheetOptions>");

                     sb.Append("<x:Print>");

                     sb.Append("<x:ValidPrinterInfo />");

                     sb.Append(" </x:Print>");

                     sb.Append("</x:WorksheetOptions>");

                     sb.Append("</x:ExcelWorksheet>");

                     sb.Append("</x:ExcelWorksheets>");

                     sb.Append("</x:ExcelWorkbook>");

                     sb.Append("</xml>");

                     sb.Append("<![endif]-->");

                     sb.Append(" </head>");

                     sb.Append("<body>");

                     return sb.ToString();



                }

                /// <summary>

                /// Excel尾部

                /// </summary>

                /// <returns></returns>

                public static string AddExcelbottom()

                {

                     StringBuilder sb = new StringBuilder();

                     sb.Append("</body>");

                     sb.Append("</html>");

                     return sb.ToString();

                }



http://www.cnblogs.com/jackxia/archive/2009/05/19/1459958.html
[最后修改由 admin, 于 2013-05-04 16:48:15]
评论Feed 评论Feed: http://blog.xg98.com/feed.asp?q=comment&id=2027

这篇日志没有评论。

此日志不可发表评论。