C# 导出到excel时线程被中止?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/421199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Thread was being aborted when exporting to excel?
提问by Xaisoft
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:
我有一个绑定到 GridView 的 DataTable。我还有一个按钮,单击该按钮可将 DataTable 导出到 Excel 文件。但是,发生以下错误:
ErrMsg = "Thread was being aborted."
ErrMsg = "线程被中止。"
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
{
}
}
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.
将 HttpContext.Current.Response.End 更改为 HttpContext.Current.ApplicationInstance.CompleteRequest 后,它现在只转到 finally 块,我无法弄清楚正在抛出什么错误消息。
采纳答案by Darin Dimitrov
回答by Paul Edward
The exception was caused by Response.End
异常是由 Response.End
One thing you can do is to logically trap the exception message...
您可以做的一件事是在逻辑上捕获异常消息......
this method will not affect the data but it will only trap the exception...
此方法不会影响数据,但只会捕获异常...
Try
.........(codes here)
Response.End
Catch ex as Message
If Not ex.Message = "Thread was being aborted." Then
Response.write(ex.message)
End If
End Try
回答by Viral Jain
I tried using HttpContext.Current.ApplicationInstance.CompleteRequest
. It did work but also exported the complete HTML Code of the page at the end of the downloaded file, which was undesirable.
我尝试使用HttpContext.Current.ApplicationInstance.CompleteRequest
. 它确实有效,但也在下载文件的末尾导出了页面的完整 HTML 代码,这是不可取的。
Response.BuffferOutput = True;
Response.Flush();
Response.Close();
Then after a hell of effort, I bumped into the above code. And it works perfectly without any exception or undesirable code at the end of the downloaded file.
然后经过一番努力,我碰到了上面的代码。它可以完美运行,在下载的文件末尾没有任何异常或不需要的代码。
回答by ablaze
I had the different story here, I am using telerikcontrols and NONE helped me, eventually I came to know that I had to just wrap my markup in telerik:RadAjaxPanel, below is the code snippet to disable ajax on RequestStart Client side event handler.
我在这里遇到了不同的故事,我使用了Telerik控件并且 NONE 帮助了我,最终我知道我必须将标记包装在telerik:RadAjaxPanel 中,下面是在 RequestStart 客户端事件处理程序上禁用 ajax 的代码片段。
<telerik:RadAjaxPanel runat="server"
ClientEvents-OnRequestStart="AjaxRequestStart">
and then in my user control, I added just one more function as depicted below:
然后在我的用户控件中,我又添加了一个函数,如下所示:
<script type="text/javascript">
function AjaxRequestStart(target, arguments) {
arguments.set_enableAjax(false);
}
回答by Ankita_systematix
Its simple, you can try:
很简单,你可以试试:
Response.Close();
in place of Response.End();
Response.Close();
代替 Response.End();