【发布时间】:2016-09-08 11:40:20
【问题描述】:
我在互联网上搜索并找到了许多将 gridview 导出到 excel 的解决方案。我尝试了所有解决方案,但没有一个对我有用。按照建议,我跟踪了 Application_Error 模块。
在调试时我的执行转到
//Global.asax
protected void Application_Error(object sender, EventArgs e)
{
Exception exc = Server.GetLastError();
}
我得到的异常是 :: 抛出了“System.Web.HttpUnhandledException”类型的异常。
我无法弄清楚我做错了什么。任何人都可以建议我,发生了什么。
private void ExportGridToExcel()
{
try
{
Response.Clear();
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.Charset = "";
string FileName = "PatientCount_" + DateTime.Now + ".xls";
StringWriter strwritter = new StringWriter();
HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
GridView1.GridLines = GridLines.Both;
GridView1.HeaderStyle.Font.Bold = true;
GridView1.RenderControl(htmltextwrtter);
Response.Write(strwritter.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.SuppressContent = true;
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
catch (Exception ex) { }
}
在调试时我没有收到任何异常,但在执行此代码后,执行转到 Global.asax 文件 Application_Error 模块。
请帮帮我!!!
【问题讨论】:
标签: c# asp.net excel global-asax