Web页中table导出到execl(带模板)

时间:2024-10-28 13:37:32

1.将excel另存为html,将其复制到aspx文件中

2.输出格式为excel

  InitData();
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AddHeader("Content-Disposition", "attachment; filename=" + new AutoNumber().GetNumberByTime()+ ".xls");//这样的话,可以设置文件名为中文,且文件名不会乱码。其实就是将汉字转换成UTF8 // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.RenderControl(oHtmlTextWriter);//Add the Panel into the output Stream.
//this.GridView1.RenderControl(oHtmlTextWriter);//将gridview输出,你也可以类似的写TextBox1.RenderControl...
Response.Output.Write(oStringWriter.ToString());//Output the stream.
Response.Flush();
Response.End();