Export to Excel - ThreadAbortException

本文关键字:ThreadAbortException Excel to Export | 更新日期: 2023-09-27 17:59:22

我发现转换为Excel代码时遇到问题。我正在.NET 4.0中进行一个网站项目,我为此创建了一个类,该类执行以下操作(基于http://mattberseth.com/blog/2007/04/export_gridview_to_excel_1.html):

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition",
string.Format("attachment; filename={0}", fileName)); HttpContext.Current.Response.ContentType = "application/ms-excel"; using (StringWriter sw = new StringWriter()) {
   using (HtmlTextWriter htw = new HtmlTextWriter(sw)) {
    //Create a table to contain the grid
    //Add header row
    //Add each data row
    //Add Footer row
    //Render the table into the htmlwriter
    //  render the htmlwriter into the response
    HttpContext.Current.Response.Write(sw.ToString());
    HttpContext.Current.Response.End();
  }
}

我从一个用户控件调用这个类,该控件包含一个添加到页面上显示的GridView中的按钮。这正如预期的那样工作-单击按钮,您将看到一个下载选项,可以打开或保存包含GridView数据的excel电子表格。

然而,当我从另一个GridView中的链接按钮调用它时,我想构建一个动态网格视图来包含数据并导出数据。当我这样做时,我会从类中的Response.End调用中获得ThreadAbortException。

问题1:从用户控件内调用相同的代码时,为什么我没有得到ThreadAbortException?用户控件是否有自己的线程或其他类型的上下文?

搜索ThreadAbortException发生时我得到的错误,导致我尝试用ApplicationInstance.CompleteRequest()替换它。当我这样做时,我不再得到ThreadAbortException,但这破坏了以前工作的用户控制-它不是包含网格中数据的excel电子表格,而是包含包含页面中的HTML,无论如何,用一个空捕获来抑制这个错误是很容易的。然而,它并没有修复动态生成的GridView的直接调用,该代码呈现了一个javascript错误:"从服务器接收的消息无法解析。"

我很想了解这里到底发生了什么,但无论理解如何,我都需要结果。我尝试过的所有其他方法(datagrid而不是GridView等)都遇到了同样的问题,在"接管"方面基本上是一样的当前响应,并使用stringwriter和htmlwriter将数据呈现为具有excelcontentType的响应。由于这显然在用户控件的上下文中有效,我无法理解为什么直接调用它时不起作用。。。

Export to Excel - ThreadAbortException

问题实际上与excel导出完全无关。"…无法解析"错误是关键。从这些链接中,我得到了关键,那就是网格事件只会导致部分回发事件:

http://forums.asp.net/t/1392827.aspx

http://forums.aspfree.com/net-development-11/gridview-footer-template-button-in-updatepanel-not-posting-back-236087.html

这解释了ThreadAbortException和"…无法解析"错误。将其添加到ImageButton的OnPreRender中就是解决方案:

protected void addTrigger_PreRender(object sender, EventArgs e)
{
    if (sender is ImageButton)
    {
        ImageButton imgBtn = (ImageButton)sender;
        ScriptManager ScriptMgr = (ScriptManager)this.FindControl("ScriptManager1");
        ScriptMgr.RegisterPostBackControl(ImgBtn);
    }
}

请尝试:HttpApplication.CompleteRequest()根据:http://www.c6software.com/codesolutions/dotnet/threadabortexception.aspx

他们讨论了

使用此

   Response.Clear()
    Response.AddHeader("content-disposition", atchment;filename=fm_specification.xls")
    Response.Charset = ""
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Response.ContentType = "application/vnd.xls"
    Dim stringWrite As System.IO.StringWriter = New System.IO.StringWriter
    Dim htmlwrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)
    GridView1.RenderControl(htmlwrite)
    Response.Write(stringWrite.ToString)
    Response.End()

您可以使用div 而不是gridview1

                            dont forget to add this on your page
 Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
 End Sub

调用Export to excel代码的事件必须进行完全回发。问题是因为它只进行部分回发。

我也有同样的错误,当我完全回发时,它得到了解决。

希望这能帮助到别人。