我的页面上有“导出到Excel”的按钮。 ButtonClick函数是将datgrid(dgrISGrid)导出到excel代码附加在下面: 但是当“线程被中止”执行其抛出错误时。解决方案是什么?
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);
}
}
答案 0 :(得分:1)
Response.End()导致此错误。
尝试在此声明之前使用“Response.Flush()”。