I am using the code below to export records in a datatable to an excel file using EPPlus.
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()
Howeve开发者_StackOverflowr, after walking through the code block, at Response.End(), I get an exception {Property evaluation failed.}
Update:
The error log is: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
Any idea what could be causing this?
Remove the line Response.End() and write the following code:
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"
Note:
When Downloading file is tested in local machine Dont use Response.End() ; otherwise error can be thrown.
but Add Response.End() when the same code is deployed in server and test the same from client machine. No error is thrown in this case.
Remove Response.Charset=""
精彩评论