代码:
1 /// 2 /// HTML Table表格数据(html)导出EXCEL 3 /// 4 /// 表头 5 /// 内容 6 /// 文件名称 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(" ");15 sw.WriteLine("
");16 sw.WriteLine(" ");17 sw.WriteLine(tableHeader);18 sw.WriteLine("
");19 sw.WriteLine(tableContent);20 sw.WriteLine("
");21 sw.WriteLine("");22 sw.WriteLine("");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 }