将数据导出为ex​​cel时出现异常

时间:2010-12-01 05:17:41

标签: .net export-to-excel

将数据导出到Excel工作表时出现以下错误

ERROR:
Message : 
Exception of type 'System.Web.HttpUnhandledException' was thrown.
Error Description : 
System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.Data.SqlClient.SqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

有人建议我做我必须做的事。

CODE:
gridData.dataSource = GetData()
gridData.DataBind()


Response.Clear()
Response.AddHeader("content-disposition",   "attachment;filename=CompletionDatesReport.xls")
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = "application/vnd.xls"
Dim stringWrite As StringWriter = New StringWriter()
Dim htmlWrite As HtmlTextWriter = New HtmlTextWriter(stringWrite)
gridData.RenderControl(htmlWrite)
Response.Write(stringWrite.ToString())
Response.End()

2 个答案:

答案 0 :(得分:0)

没有代码我们只能猜测,但SqlException: Timeout expired中有一个很大的线索 - 这表明你的查询花了太长时间。您可以通过SqlCommand.CommandTimeout增加命令的超时(但试着保持理智......),但是编写查询以提高效率,或者不在一个查询中提取尽可能多的数据,通常会更好。

答案 1 :(得分:0)

我同意马克。查看您的查询并优化它,删除DISTINCT语句等,使其更快。