2011-06-18 48 views
0

我遇到了转换为Excel代码的问题,我在找。我工作在.NET 4.0中一个网站项目,我创建了一个类此,做以下(基于 http://mattberseth.com/blog/2007/04/export_gridview_to_excel_1.html):导出到Excel - ThreadAbortException

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:为什么在调用usercontrol中的相同代码时没有得到该ThreadAbortException?用户控件是否有自己的线程或其他类型的上下文?

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

我很想知道这里到底发生了什么,但我无论理解如何都需要结果。我尝试过的所有其他方法(数据网格而不是GridView等)都遇到了相同的问题,在涉及到“接管”当前响应并使用stringwriter和htmlwriter将数据转换为excel contentType的响应。而且,由于这明显符合UserControl的情况下工作,我在我束手无策,为什么时直接调用它不会工作...

+0

可能的重复[为什么我的asp.net应用程序抛出ThreadAbortException?](http://stackoverflow.com/questions/12476/why-is-my-asp-net-application-throwing-threadabortexception) –

+0

I don不相信这是重复的 - 这是一个非常具体的案例,处理两种情况 - 一种情况是发生了对response.end的调用,另一种情况下它并不支持 –

回答

0

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

尝试,而不是: HttpApplication.CompleteRequest() 按: http://www.c6software.com/codesolutions/dotnet/threadabortexception.aspx

他们讨论额外的HTML被flished

+0

切换到CompleteRequest确实避免了错误,正如我在问题,但它没有解决JavaScript错误“从服务器收到的消息无法解析。”我没有添加从您的链接提到的覆盖,没有任何效果。 – Jarrod

+0

查看Fiddler(www.fiddler2.com)中返回的数据 - 可能会写入您不期望的响应流,并且您可能必须缓冲输出和response.clear,但试试fiddler。 –

+0

亚当,下次我遇到这样的问题时,我会记得使用它。感谢您的建议和意见。我发布了解决方案,因为我找到了问题的根源。再次感谢。 – Jarrod

0

使用本

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() 

,而不是gridview1可以使用div

      dont forget to add this on your page 

Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control) 
End Sub 
+0

除了Response.Charset =“”和Response.Cache.SetCacheablility(HttpCacheability.NoCache)之外,这与我所拥有的完全相同。也许并不奇怪,添加这些行不起作用。我得到了ThreadAbortException,除非我禁止它,并且在任何一种情况下,我都会收到javascript错误“从服务器接收到的消息无法解析。”我在页面中重写了VerifyRenderingInServerForm。 – Jarrod

+0

是否在您定义页面类的aspx源页面的顶部添加了ValidateRequest =“false”? – Vikky

+0

不,我没有。我没有看到你提到要做到这一点。我能够找到那些错误,并发布了我的答案。感谢您就此问题提出的意见。非常感激。 – Jarrod

0

在其上导出到Excel中的代码调用的事件,必须做出一个完整的回发。这个问题是因为它只做了部分回发。

我有同样的错误,当我做了一个完整的回发时它得到了解决。

希望这可以帮助别人。