Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

test.xlsx couldn't be downloaded - EPPlus

Tags:

vb.net

epplus

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()
like image 954
chaltahai Avatar asked Nov 20 '25 22:11

chaltahai


1 Answers

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
like image 93
Pete Avatar answered Nov 23 '25 04:11

Pete



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!