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

表格用文件流的方式输出为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.xuebuyuan.com/268161.html 如有侵权请告知
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值