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

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

ExportExcel.aspx.cs部分代码:
 1 None.gif System.Web.HttpContext.Current.Response.Clear();
 2 None.gifSystem.Web.HttpContext.Current.Response.Buffer  =   true ;
 3 None.gifSystem.Web.HttpContext.Current.Response.Charset  =   " UTF-8 " ;
 4 None.gifSystem.Web.HttpContext.Current.Response.ClearHeaders();
 5 None.gifSystem.Web.HttpContext.Current.Response.AppendHeader( " Content-Disposition "  , " attachment;filename= " + strFileName + " .xls " );    // attachment,下载;inline 在线打开
 7 None.gif System.Web.HttpContext.Current.Response.ContentEncoding  =  System.Text.Encoding.GetEncoding( " UTF-8 " );
 8 None.gifSystem.Web.HttpContext.Current.Response.ContentType  =   " application/ms-excel "
 9 None.gifSystem.Web.HttpContext.Current.Response.Write(sbExcel.ToString());  // sbExcel:用于生成Excel的HTML代码
10   System.Web.HttpContext.Current.Response.Flush();
11   System.Web.HttpContext.Current.Response.End();

转载于:https://www.cnblogs.com/ctrl2/archive/2005/11/27/285504.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值