【问题标题】:Export to Excel - ThreadAbortException导出到 Excel - ThreadAbortException
【发布时间】:2011-09-17 15:07:08
【问题描述】:

我在查找的转换为 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 错误:“无法解析从服务器接收到的消息。”

我很想了解这里到底发生了什么,但无论了解如何,我都需要结果。我尝试过的所有其他方法(数据网格而不是 GridView 等)都遇到了同样的问题,并且在归结为“接管”时基本相同 当前响应并使用 stringwriter 和 htmlwriter 将数据呈现为具有 excel contentType 的响应。而且由于这在用户控件的上下文中可以证明是有效的,所以我不知道为什么直接调用它时它不起作用......

【问题讨论】:

标签: c# asp.net .net export-to-excel threadabortexception


【解决方案1】:

改为尝试: HttpApplication.CompleteRequest() 按照: http://www.c6software.com/codesolutions/dotnet/threadabortexception.aspx

他们讨论正在刷新的其他 html

【讨论】:

  • 切换到 CompleteRequest 确实避免了错误,正如我在问题中所述,但它没有解决 javascript 错误“无法解析从服务器接收到的消息。”我确实从您的链接中添加了提到的覆盖,但没有效果。
  • 查看 Fiddler (www.fiddler2.com) 中返回的数据 - 某些内容可能正在写入您不期望的响应流,您可能需要缓冲输出和 response.clear ,但试试提琴手。
  • 亚当,下次遇到这样的问题时,我会记得使用它。感谢您的建议和意见。我发布了解决方案,因为我确实找到了问题的根源。再次感谢。
【解决方案2】:

使用这个

   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

【讨论】:

  • 除了 Response.Charset = “” 和 Response.Cache.SetCacheablility(HttpCacheability.NoCache),这和我的一样。也许并不奇怪,添加这些行没有效果。除非我抑制它,否则我会收到 ThreadAbortException,并且在任何一种情况下,我都会收到 javascript 错误“无法解析从服务器接收到的消息”。我确实在页面中覆盖了 VerifyRenderingInServerForm。
  • 您是否在定义页面类的 aspx 源页面顶部添加了 ValidateRequest="false"?
  • 不,我没有。我看不出你提到在哪里这样做。我能够找到错误并发布了我的答案。感谢您对此问题的意见。非常感谢。
【解决方案3】:

这个问题实际上与 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);
    }
}

【讨论】:

    【解决方案4】:

    调用导出到 excel 代码的事件必须进行完整回发。问题在于它只进行部分回发。

    我遇到了同样的错误,当我进行完整的回发时它得到了解决。

    希望这对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多