C# HTML转EXCEL的方法

 1         private void ToExcel(string html)
 2         {
 3 
 4             Response.ContentType = "application/force-download";
 5             Response.AddHeader("content-disposition",
 6                 "attachment; filename=" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");
 7             Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
 8             Response.Write("<head>");
 9             Response.Write("<META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
10             string fileCss = Server.MapPath("~/UI/themes/DRP.UI.Ext.css");
11             string cssText = string.Empty;
12             StreamReader sr = new StreamReader(fileCss);
13             var line = string.Empty;
14             while ((line = sr.ReadLine()) != null)
15             {
16                 cssText += line;
17             }
18             sr.Close();
19             Response.Write("<style>" + cssText + "</style>");
20             Response.Write("<!--[if gte mso 9]><xml>");
21             Response.Write("<x:ExcelWorkbook>");
22             Response.Write("<x:ExcelWorksheets>");
23             Response.Write("<x:ExcelWorksheet>");
24             Response.Write("<x:Name>Report Data</x:Name>");
25             Response.Write("<x:WorksheetOptions>");
26             Response.Write("<x:Print>");
27             Response.Write("<x:ValidPrinterInfo/>");
28             Response.Write("</x:Print>");
29             Response.Write("</x:WorksheetOptions>");
30             Response.Write("</x:ExcelWorksheet>");
31             Response.Write("</x:ExcelWorksheets>");
32             Response.Write("</x:ExcelWorkbook>");
33             Response.Write("</xml>");
34             Response.Write("<![endif]--> ");
35             Response.Write(html);//HTML
36             Response.Flush();
37             Response.End();
38         }

 

转载于:https://www.cnblogs.com/maja/p/10231347.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值