0

我正在使用下面的代码将数据表中的记录导出到使用 EPPlus 的 excel 文件中。

        Dim excelPackage = New ExcelPackage
        Dim excelWorksheet = excelPackage.Workbook.Worksheets.Add("DemoPage")
        excelWorksheet.Cells("A1").LoadFromDataTable(dt, True)

        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        Response.AddHeader("content-disposition", "attachment;  filename=ExcelDemo.xlsx")
        Response.BinaryWrite(excelPackage.GetAsByteArray())
        Response.End()

但是,在遍历代码块之后,在 Response.End(),我得到一个异常{Property evaluation failed.}

更新:
错误日志是:

ERROR: System.Threading.ThreadAbortException: Thread was being aborted.
   at System.Threading.Thread.AbortInternal()
   at System.Threading.Thread.Abort(Object stateInfo)
   at System.Web.HttpResponse.End()
   at Reporting.Page_Load(Object sender, EventArgs e) in C:\Reporting.aspx.vb:line 38

知道是什么原因造成的吗?

4

2 回答 2

2

删除Response.End()行并编写以下代码:

HttpContext.Current.Response.Clear()
Response.ClearHeaders()
Response.Buffer = True

Dim excelPackage = New ExcelPackage       
Dim excelWorksheet = excelPackage.Workbook.Worksheets.Add("DemoPage")              

excelWorksheet.Cells("A1").LoadFromDataTable(dt, True)        
Response.ContentType = "application/vnd.openxmlformats- 

officedocument.spreadsheetml.sheet"         
Response.AddHeader("content-disposition", "attachment;  filename=ExcelDemo.xlsx")  
Response.Charset = ""
Response.BinaryWrite(excelPackage.GetAsByteArray())  
Response.Flush()
Response.Close()
excelPackage.close() or
excelPackage=nothing or excelPackage.dispose() whichever is fit.

hope you will not get "Property Evaluation Error"
于 2012-09-25T10:18:33.010 回答
0

笔记:

在本地机器上测试下载文件时不要使用 Response.End() ; 否则会抛出错误。

但是当在服务器中部署相同的代码并从客户端机器测试相同的代码时添加 Response.End()。在这种情况下不会引发错误。

删除 Response.Charset=""

于 2012-12-07T18:31:25.660 回答