欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

HTML转EXCEL的方法

程序员文章站 2022-04-28 13:01:58
1 private void ToExcel(string html) 2 { 3 4 Response.ContentType = "application/force-download"; 5 Response.AddHeader("content-disposition", 6 ... ......
 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         }