导出到Excel时正在中止线程? [英] Thread was being aborted when exporting to excel?
问题描述
我有这势必会一个GridView的DataTable。我也有一个按钮,点击时的出口将DataTable到Excel文件。但是,下面的误差是发生
I have a DataTable which is bound to a GridView. I also have a button that when clicked exports the DataTable to an Excel file. However, the following error is occuring:
ERRMSG =线程已被中止。
ErrMsg = "Thread was being aborted."
下面是code,其中被抛出的错误的一部分:
Here is part of the code where the error is being thrown:
private static void Export_with_XSLT_Web(DataSet dsExport,
string[] sHeaders,
string[] sFileds,
ExportFormat FormatType,
string FileName)
{
try
{
// Appending Headers
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
if(FormatType == ExportFormat.CSV)
{
HttpContext.Current.Response.ContentType = "text/csv";
HttpContext.Current.Response.AppendHeader("content-disposition",
"attachment;
filename=" + FileName);
}
else
{
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.AppendHeader("content-disposition",
"attachment;
filename=" + FileName);
}
// XSLT to use for transforming this dataset.
MemoryStream stream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8);
CreateStylesheet(writer, sHeaders, sFileds, FormatType);
writer.Flush();
stream.Seek(0, SeekOrigin.Begin);
XmlDataDocument xmlDoc = new XmlDataDocument(dsExport);
//dsExport.WriteXml("Data.xml");
XslTransform xslTran = new XslTransform();
xslTran.Load(new XmlTextReader(stream), null, null);
using(StringWriter sw = new StringWriter())
{
xslTran.Transform(xmlDoc, null, sw, null);
//Writeout the Content
HttpContext.Current.Response.Write(sw.ToString());
writer.Close();
stream.Close();
HttpContext.Current.Response.End();
}
}
catch(ThreadAbortException Ex)
{
string ErrMsg = Ex.Message;
}
catch(Exception Ex)
{
throw Ex;
}
finally
{
}
}
改变HttpContext.Current.Response.End到HttpContext.Current.ApplicationInstance.CompleteRequest之后,它现在只是进入finally块,我无法弄清楚被抛出什么样的错误信息。
After changing HttpContext.Current.Response.End to HttpContext.Current.ApplicationInstance.CompleteRequest, it now just goes to the finally block and I can't figure out what error message is being thrown.
推荐答案
该ThreadAbortException从以下行抛出:
The ThreadAbortException is thrown from the following line:
HttpContext.Current.Response.End();
下面更详细信息和解决方法。
这篇关于导出到Excel时正在中止线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!