C# 导出Excel "正在中止线程" 错误
导出Excel相信很多人都用过,但是我却遇到了一个问题 “正在中止线程”
源代码如下:
public static void ExportExcel(string fileName, GridView gvMain)
{
//当前对话
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
//IO用于导出并返回excel文件
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null;
if (gvMain.DataSource != null)
{
//设置编码和附件格式
curContext.Response.Clear();
curContext.Response.Buffer = true;
//System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8)作用是方式中文文件名乱码
curContext.Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".xls");
curContext.Response.ContentType = "application/vnd.ms-excel";
//解决输出的内容为乱码问题
curContext.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=UTF-8>");
//导出Excel文件
strWriter = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
//下载到客户端
gvMain.RenderControl(htmlWriter);
curContext.Response.Write(strWriter.ToString());
htmlWriter.Close();
strWriter.Close();
curContext.Response.End();
}
}
执行了也可以把数据导出来,但是却抛出异常。
症状
如果使用 Response.End、Response.Redirect 或 Server.Transfer 方法,将出现 ThreadAbortException 异常。您可以使用 try-catch 语句捕获此异常。
原因
<!-- Inject Script Filtered -->
Response.End 方法终止页的执行,并将此执行切换到应用程序的事件管线中的 Application_EndRequest 事件。不执行 Response.End 后面的代码行。
此问题出现在 Response.Redirect 和 Server.Transfer 方法中,因为这两种方法均在内部调用 Response.End。
解决方案
要解决此问题,请使用下列方法之一: 对于 Response.End,调用 HttpContext.Current.ApplicationInstance.CompleteRequest 方法而不是 Response.End 以跳过 Application_EndRequest 事件的代码执行。
对于 Response.Redirect,请使用重载 Response.Redirect(String url, bool endResponse),该重载对 endResponse 参数传递 false 以取消对 Response.End 的内部调用。例如:
Response.Redirect ("nextpage.aspx", false);
如果使用此替代方法,将执行 Response.Redirect 后面的代码。
对于 Server.Transfer,请改用 Server.Execute 方法。
from:http://blog.csdn.net/zhensoft163/article/details/5995146
C# 导出Excel "正在中止线程" 错误的更多相关文章
- Response.Redirect 产生的“正在中止线程”错误
Response.Redirect 产生的“正在中止线程”错误 今天在开发调试过程中,出现在一个 "正在中止线程"异常信息. 调用Response.Redirect()方法产生的, ...
- ASP.NET: 正在中止线程 错误原及解决方法
#[操作记录]:2010-02-23 9:25:12 System.Threading.ThreadAbortException: 正在中止线程. 症状 如果使用 Response.End.Resp ...
- 【转】ASP.NET"正在中止线程"错误原因
最近做的系统中老出现的一些问题不太明白,在使用 Response.End.Response.Redirect 或 Server.Transfer 时出现 ThreadAbortException , ...
- Asp.net中"正在中止线程"错误解决方法
项目中出现“正在中止线程”问题,百度后台发现,都是因为用到Response.End.Response.Redirect 或 Server.Transfer 方法. 原因: Response.End 方 ...
- asp.net中导出excel数据的方法汇总
1.由dataset生成 代码如下 复制代码 public void CreateExcel(DataSet ds,string typeid,string FileName) { Htt ...
- 谷歌浏览器导出excel失败问题解决(网上都没解决)
java poi导出excel报了网络错误,信息已经写回到chrome浏览器(IE/FF均无此问题).如下所示: 从chrome的network大小部分也可以看出是正确的. 网上很多答案说将file. ...
- C#错误之 System.Threading.ThreadAbortException:正在中止线程
参考:http://www.cnblogs.com/chendaoyin/archive/2013/06/27/3159211.html 1.开启一个子线程 //开启一个子线程,子线程调用方法 Met ...
- DateGridew导出Excel表+常见错误提示
在敲机房收费系统的时候,显示数据的时候需要将DateGridew 中的数据导出进Excel表.DateGridew导出Excel表是比较常见的,当然导出Excel表有很多种方法,下面是个人认为比较容易 ...
- 解析ArcGis拓扑——根据拓扑错误记录提取shp文件、导出Excel表格
在ArcGis拓扑检查的流程——以面重叠检查为例中讲述了如何在ArcGis进行拓扑检查与修改. 在实际操作中,有时我们还需要将ArcGis拓扑检查的结果制作成报告或者提取错误信息反馈作业方. 本文仍然 ...
随机推荐
- Spring @Autowired、@Resource、@Required、@Component、@Repository、@Service、@Controller注解的用法和作用
Spring @Autowired,@Resource,@Required注解的用法和作用 Spring中 @Autowired标签与 @Resource标签 的区别 Spring注解@Compone ...
- 【Redis】redis+php处理高并发,很好的教程||附上 php的文件锁
链接至:http://blog.csdn.net/nuli888/article/details/51865401 很好的教程,其中redis+php有点小问题. 附上php文件锁: $fp = fo ...
- Python 以指定的概率选取元素
Python 以指定的概率选取元素 Problem You want to pick an item at random from a list, just about as random.choic ...
- 【LeetCode】77. Combinations (2 solutions)
Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ... ...
- tp模型和数据库操作方法
一.新建的模型名和表名一样,采用驼峰式,如表名user_type模型取名为UserType namespace app\index\model;use think\Model;class UserTy ...
- php 关于日期的一些计算
1.strtotime基本使用 date_default_timezone_set('PRC'); //设置中国时区 echo "今天:", date("Y-m-d&qu ...
- atitit.故障排除------有时会错误com.microsoft.sqlserver.jdbc.SQLServerException: Connection reset by peer: soc
atitit.故障排除------有时会错误com.microsoft.sqlserver.jdbc.SQLServerException: Connection reset by peer: soc ...
- CYQ学习主要摘要4
http://www.cnblogs.com/cyq1162/archive/2010/11/03/1867642.html Xml的处理 http://www.cnblogs.com/cyq1162 ...
- C++中explicit的用法
https://blog.csdn.net/qq_35524916/article/details/58178072 https://blog.csdn.net/jinjin1062495199/ar ...
- location 将多级目录下的文件转成一个文件
/ck-135-201-7142.html 指向/ck/135/201/7142.html [root@web01 www]# cat /app/server/nginx/conf/rewrite/d ...