asp.net将内容导出到Excel,Table表格数据(html)导出EXCEL

代码:

   

 1      /// <summary>
 2         /// HTML Table表格数据(html)导出EXCEL
 3         /// </summary>
 4         /// <param name="tableHeader">表头</param>
 5         /// <param name="tableContent">内容</param>
 6         /// <param name="sheetName">文件名称</param>
 7         public void ExportToExcel(string tableHeader,string tableContent,string sheetName)
 8         {
 9             string fileName = sheetName + DateTime.Now.ToString("yyyyMMddHHmmss");
10             string tabData = htmlTable;
11             if (tabData != null)
12             {
13                 StringWriter sw = new System.IO.StringWriter();
14                 sw.WriteLine("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /></head><body>");
15                 sw.WriteLine("<table>");
16                 sw.WriteLine("<tr style=\"background-color: #e4ecf7; text-align: center; font-weight: bold\">");
17                 sw.WriteLine(tableHeader);
18                 sw.WriteLine("</tr>");
19                 sw.WriteLine(tableContent);
20                 sw.WriteLine("</table>");
21                 sw.WriteLine("</body>");
22                 sw.WriteLine("</html>");
23                 sw.Close();
24                 Response.Clear();
25                 Response.Buffer = true;
26                 Response.Charset = "UTF-8";
27                 //Response.Charset = "GB2312";
28                 //this.EnableViewState = false;
29                 Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".xls");
30                 Response.ContentType = "application/ms-excel";
31                 Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
32                 Response.Write(sw);
33                 Response.End();
34             }
35         }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值