簡體   English   中英

導出到Excel-ThreadAbortException

[英]Export to Excel - ThreadAbortException

我發現轉換為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內的linkbutton調用它時,我想構建一個動態的Gridview來包含數據並將其導出。 當我這樣做時,我從類中的Response.End調用中獲得了ThreadAbortException。

問題1:當從用戶控件中調用相同的代碼時,為什么沒有得到ThreadAbortException? 用戶控件是否獲得自己的線程或其他某種上下文?

搜索發生ThreadAbortException時得到的錯誤,導致我嘗試用ApplicationInstance.CompleteRequest()替換它。 當我這樣做時,我不再得到ThreadAbortException,但是這破壞了以前正常工作的用戶控件-代替了包含表格數據的excel電子表格,它包含了包含頁面的HTML,無論如何它很容易隱藏帶有空捕獲的錯誤。 但是,它不能解決動態生成的GridView的直接調用,該代碼會呈現javascript錯誤:“無法解析從服務器收到的消息。”

我很想了解這里到底發生了什么,但是無論理解如何,我都需要結果。 我嘗試過的所有其他方法(使用datagrid而不是GridView等)都遇到相同的問題,並且當涉及到“接管”當前響應並使用stringwriter和htmlwriter將數據呈現為用excel contentType響應。 並且由於這顯然在用戶控件的上下文中起作用,因此我直言不諱地解釋了為什么直接調用它時不起作用...

調用Export to excel代碼的事件必須進行完整的回發。 問題是因為它僅執行部分回發。

我有同樣的錯誤,當我進行完整的回發后,它就解決了。

希望這對某人有幫助。

嘗試改用:HttpApplication.CompleteRequest(),方法如下: http ://www.c6software.com/codesolutions/dotnet/threadabortexception.aspx

他們討論了正在處理的其他HTML

用這個

   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

該問題實際上與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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM