怎么把动态生成html table导出到Excel?求高手!!
要求点导出按钮能直接进入Excel~
------解决方案--------------------
将数据按照一定的格式导出
private void ToExcel(Control ctl, string FileName)
{
// 设置 字符集
Response.Charset = "UTF-8";
// 设置 内容编码格式
Response.ContentEncoding = System.Text.Encoding.UTF8;
// 设置"导出"内容格式 (word excel txt)等等
Response.ContentType = "application/ms-excel";
// 将下载信息追加到Header中
Response.AppendHeader("Content-Disposition", "attachment;filename=" +FileName);
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw); // ctl为数据展示控件的Id
Response.Write(tw.ToString());
Response.End();
}
// 记得重写这个方法
public override void VerifyRenderingInServerForm(Control control)
{
}