我们在做html" title=报表>报表统计的时候,在页面中放个服务器端的控件Table1
在cs代码中自动组合行和列
第一种情况:页面有两个按钮,分别为显示html" title=报表>报表按钮和导出html" title=报表>报表
显示html" title=报表>报表按钮将组合成的html" title=table>table中的数据显示到界面上
导出html" title=报表>报表按钮将已显示到界面html" title=table>table中的数据些入到html" title=excel>excel中
结果看到html" title=excel>excel中的数据只是一个空的html" title=table>table没有任何数据
经过验证当向html" title=excel>excel中开始写入数据的时候html" title=table>table中的数据已经是空的,这种方法行不同
第二种情况:页面有两个按钮,分别为显示html" title=报表>报表按钮和导出html" title=报表>报表
显示html" title=报表>报表按钮将组合成的html" title=table>table中的数据显示到界面上
导出html" title=报表>报表按钮将显示html" title=报表>报表的过程再重复一遍,再将已显示到界面html" title=table>table中的数据些入到html" title=excel>excel中
结果看到html" title=excel>excel中的数据与界面显示的数据相吻合
所以要采用第二种方法
打开html" title=excel>excel可看到
关键代码:
protected void Btn_ShowReport_Click(object sender, System.EventArgs e)
{
TableDialoutReport();
}
protected void Btn_ExportReport_Click(object sender, System.EventArgs e)
{
TableDialoutReport();
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=DialoutReportTable.xls");
// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/ms-html" title=excel>excel";//设置输出文件类型为html" title=excel>excel文件。
//Response.ContentType = "application/vnd.ms-html" title=excel>excel";//输出类型
//Response.Charset = "";
//关闭 ViewState
Table1.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();//将信息写入字符串
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);//在WEB窗体页上写出一系列连续的HTML特定字符和文本。
//此类提供ASP.NET服务器控件在将HTML内容呈现给客户端时所使用的格式化功能
//获取control的HTML
Table1.RenderControl(hw);//将html" title=table>table中的内容输出到HtmlTextWriter对象中
// 把HTML写回浏览器
Response.Write(tw.ToString());
Response.Flush();
Response.End();
}