利用Response.Write导出Excel存在的一点问题

最近发现网上常见的通过Response.Write方法向客户端输出Excel的方法存在一些问题,当客户端环境为Windows 2000操作系统IE6.0时,并且输出文件的页面位于一个框架页面内的话,执行导出操作之后会使当前页所有脚本失效,获取不到当前页的document对象了。后来想改用跳转到新的页面执行导出操作,用了Reponse.Redirect重定向到新页面,结果还是出现相同的错误,后来想到了一种解决方式,向客户端发送脚本块实现重定向可以实现导出,但不会使当前页脚本失效,代码如下:
StringBuilder sbScript = new StringBuilder();
sbScript.Append(
"<script>");
sbScript.Append(
"window.open('ExportExcel.aspx','_perent'");
sbScript.Append(
"</script>");
Page.RegisterStartupScript(
"",sbScript.ToString());

ExportExcel.aspx.cs部分代码:
 1System.Web.HttpContext.Current.Response.Clear();
 2System.Web.HttpContext.Current.Response.Buffer = true;
 3System.Web.HttpContext.Current.Response.Charset = "UTF-8";
 4System.Web.HttpContext.Current.Response.ClearHeaders();
 5System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition" ,"attachment;filename="+strFileName+".xls");   //attachment,下载;inline 在线打开
 7System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
 8System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel"
 9System.Web.HttpContext.Current.Response.Write(sbExcel.ToString()); //sbExcel:用于生成Excel的HTML代码
10   System.Web.HttpContext.Current.Response.Flush();
11   System.Web.HttpContext.Current.Response.End();
posted on 2005-11-27 13:52  ctrl2a  阅读(3144)  评论(2编辑  收藏  举报