将html导出到excel或word
本质是将html写成word或excel支持的html格式。
如何将html写成word或excel支持的格式?
只需打开计算机上任意一个word或excel文档,打开文件->另存为,选择文件类型为“*.htm,*.html”格式,即将一篇word转化为html格式文件。
在浏览器中打开该文件,查看源码,即可看到转化后的html文档。
只需参照此文档内容,将html写成该格式,然后将html内容写入到文件中,把文件后缀名改为“.xls”或“.doc”即可。
1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace ConsoleApplication1 9 { 10 class Program 11 { 12 /// <summary> 13 /// 导出到excel 14 /// </summary> 15 /// <returns></returns> 16 public static void exportExcel() { 17 string HEADER = "<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">" + 18 "<meta http-equiv=Content-Type content=\"text/html; charset=\"gb2312\">" + 19 "<head>" + 20 "<!--[if gte mso 9]><xml>" + 21 "<x:ExcelWorkbook>" + 22 "<x:ExcelWorksheets>" + 23 "<x:ExcelWorksheet>" + 24 "<x:Name>工作表标题</x:Name>" + 25 "<x:WorksheetOptions>" + 26 "<x:Print>" + 27 "<x:ValidPrinterInfo />" + 28 "</x:Print>" + 29 "</x:WorksheetOptions>" + 30 "</x:ExcelWorksheet>" + 31 "</x:ExcelWorksheets>" + 32 "</x:ExcelWorkbook>" + 33 "</xml>" + 34 "<![endif]-->" ; 35 36 string STYLE="<style type=\"text/css\">" + 37 ".spercent" + 38 " {" + 39 " background-color:#ffff99;" + 40 " mso-number-format:0.00%;" + 41 " }" + 42 ".sId" + 43 " {" + 44 " background-color:#ff6633;" + 45 " mso-number-format:0;" + 46 " }" + 47 ".sName" + 48 " {" + 49 " color:red;" + 50 " }" + 51 ".sValue" + 52 " {" + 53 " color:blue;" + 54 " mso-number-format:0;" + 55 " }" + 56 "</style>"; 57 58 using (StreamWriter writer = new StreamWriter(@"C:\Users\linghuam\Desktop\1111.xls", true, System.Text.Encoding.GetEncoding("gb2312"), 512)) 59 { 60 writer.WriteLine(HEADER); 61 writer.WriteLine(STYLE); 62 writer.WriteLine("</head><body><table border=\"1\" style=\"font-size:9pt\"><tr>"); 63 writer.WriteLine("<th>ID</th>"); 64 writer.WriteLine("<th>Name</th>"); 65 writer.WriteLine("<th>Value</th>"); 66 writer.WriteLine("<th>Percent</th>"); 67 68 for (int row = 1; row < 50; row++) 69 { 70 writer.WriteLine("<tr>"); 71 writer.WriteLine("<td class=\"sId\">{0}</td>", row); 72 writer.WriteLine("<td class=\"sName\">{0}</td>", Guid.NewGuid().ToString()); 73 writer.WriteLine("<td class=\"sValue\">{0}</td>", new Random().Next()); 74 writer.WriteLine("<td class=\"spercent\">{0}</td>", new Random().NextDouble()); 75 writer.WriteLine("</tr>"); 76 } 77 78 writer.WriteLine("</table></body>"); 79 } 80 } 81 /// <summary> 82 /// 导出到word 83 /// </summary> 84 /// <returns></returns> 85 86 public static bool exportWord() { 87 string html = "<html xmlns:v=\"urn:schemas-microsoft-com:vml\"xmlns:o=\"urn:schemas-microsoft-com:office:office\"xmlns:w=\"urn:schemas-microsoft-com:office:word\"xmlns:m=\"http://schemas.microsoft.com/office/2004/12/omml\"xmlns=\"http://www.w3.org/TR/REC-html40\"> <head> <![endif]><!--[if gte mso 9]><xml> <x:ExcelWorkbook> <x:ExcelWorksheets> <x:ExcelWorksheet> <x:Name>gis</x:Name> <x:WorksheetSource/> </x:ExcelWorksheet> </x:ExcelWorksheets> <x:ProtectStructure>False</x:ProtectStructure> <x:ProtectWindows>False</x:ProtectWindows> </x:ExcelWorkbook> </xml><![endif]--> <meta http-equiv=Content-Type content=\"text/html; charset=gb2312\"> <meta name=ProgId content=Word.Document> <meta name=Generator content=\"Microsoft Word 15\"> <meta name=Originator content=\"Microsoft Word 15\"> <style> div{background:rgba(255,0,0,0.5); } p{color: blue; font-size: 22px; font-weight: bolder; } h1{color: rgb(125,125,125); width: 100%; height: 100%; padding: 20px; background:rgb(66,89,224); } </style> </head> <body> <div> <p>I am chinese!</p> <h1>我发发非法上访</h1> </div> </body> </html>"; 88 using (StreamWriter writer = new StreamWriter(@"C:\Users\linghuam\Desktop\1111.doc", true, System.Text.Encoding.GetEncoding("gb2312"), 512)) 89 { 90 writer.WriteLine(html); 91 return true; 92 } 93 return false; 94 } 95 static void Main(string[] args) 96 { 97 exportExcel(); 98 exportWord(); 99 } 100 } 101 }