代码:
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 }