2010-12-09 45 views
1

我的页面上有“Export To Excel”按钮。 ButtonClick函数是将数据网格(dgrISGrid)导出为ex​​cel代码如下所示: 但执行其抛出错误时“线程被中止”。解决方法是什么?导出为Asp.Net中的优秀

protected void imgbtnExport_Click(object sender, ImageClickEventArgs e) 
    { 
     try 
     { 

     Response.Clear(); 
     Response.AddHeader("content-disposition", "attachment;filename=InformationSystems.xls"); 
     Response.Charset = ""; 
     Response.Cache.SetCacheability(HttpCacheability.NoCache); 
     Response.ContentType = "application/vnd.xls"; 
     System.IO.StringWriter stringWrite = new System.IO.StringWriter(); 
     System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); 
     dgrISGrid.RenderControl(htmlWrite); 
     Response.Write(stringWrite.ToString()); 
     Response.End(); 
     } 
     catch (Exception ex) 
     { 

     ExceptionHandler ObjExceptionHandler = new ExceptionHandler(); 
     lblError.Text = ObjExceptionHandler.GetExceptionDetails(ex); 
     } 
    } 

回答

1

Response.End()导致此错误。

尝试在此语句之前使用“Response.Flush()”。

+0

我试了一下。实际上messgaebox弹出要求打开或保存..之后,错误来了..说,里面的文本是纠正。 其实我的应用程序有其他出口擅长..也没有他们工作。是它的Excel版本问题我有>?它是我的电脑上的Microsoft Office Excel 2007 – 2010-12-10 07:11:09