When I try to open test.xlsx created by the code below, I get "test.xlsx couldn't be downloaded". That said, if I choose to save the file, I am able to save and open the file just fine.
Please let me know what I am doing wrong.
Thanks!
Dim pack As New ExcelPackage
Dim ws As ExcelWorksheet = pack.Workbook.Worksheets.Add("Sheet1")
Dim ms As New MemoryStream
Dim dt As New DataTable
ws.Cells(1, 1).Value = "Test"
pack.SaveAs(ms)
ms.WriteTo(Context.Response.OutputStream)
Context.Response.Clear()
Context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Context.Response.AddHeader("Content-Disposition", "attachment;filename=test.xlsx")
Context.Response.StatusCode = 200
Context.Response.End()
The following sample code should do the job:
Using pkg = New ExcelPackage()
Dim ws = pkg.Workbook.Worksheets.Add("Sheet 1")
ws.Cells(1, 1).Value = "Hello"
Dim buf = pkg.GetAsByteArray()
Response.Clear()
Response.AddHeader("Content-Disposition", "attachment;filename=test.xlsx")
Response.ContentType = MimeMapping.GetMimeMapping("*.xlsx")
Response.BinaryWrite(buf)
Response.Flush()
Response.[End]()
End Using