我尝试将数据集转换为 Excel 并下载该 Excel。我得到了所需的 Excel 文件。但是每次 Excel 下载都会引发 System.Threading.ThreadAbortException。
如何解决这个问题?...
请帮我...
我在我的 aspx 屏幕中调用此方法。此方法也引发了相同的异常。
我在许多 aspx 屏幕中调用该 public void ExportDataSet(DataSet ds) 函数,并且我还在维护错误记录器方法,以记录在运行时引发的异常,并将这些异常写入 .txt 文件。因此,相同的异常会记录在所有 aspx 屏幕的 txt 文件中。我只是想避免从方法声明的类文件到 aspx 抛出此异常。只是我只想在我的方法声明类文件本身处理这个异常。
ASPX 文件方法调用:
excel.ExportDataSet(dsExcel);
方法定义:
public void ExportDataSet(DataSet ds)
{
try
{
string filename = "ExcelFile.xls";
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.Charset = "";
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
GridView dg = new GridView();
dg.DataSource = ds.Tables[0];
dg.DataBind();
dg.RenderControl(htw);
// response.Write(style);
response.Write(sw.ToString());
response.End(); // Exception was Raised at here
}
}
}
catch (Exception ex)
{
string Err = ex.Message.ToString();
EsHelper.EsADLogger("HOQCMgmt.aspx ibtnExcelAll_Click()", ex.Message.ToString());
}
finally
{
}
}
我在网上查了一下,发现Response.End()
总是抛出异常。
替换这个:HttpContext.Current.Response.End();
有了这个:
HttpContext.Current.Response.Flush(); // Sends all currently buffered output to the client.
HttpContext.Current.Response.SuppressContent = true; // Gets or sets a value indicating whether to send HTTP content to the client.
HttpContext.Current.ApplicationInstance.CompleteRequest(); // Causes ASP.NET to bypass all events and filtering in the HTTP pipeline chain of execution and directly execute the EndRequest event.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)