This project has moved. For the latest updates, please go here.

test.xlsx couldn't be downloaded - EPPlus

Dec 11, 2012 at 5:03 PM

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()
Developer
Dec 11, 2012 at 6:07 PM

It seems like you are calling Response.Clear() after you have written to the Outputstream?

Dec 11, 2012 at 8:26 PM

@swmal I tried it, but it did not work.

Developer
Dec 12, 2012 at 5:17 PM

In the source code there is a sample that describes how you can use EPPlus in ASP.NET (also available if you download the samples).

http://epplus.codeplex.com/SourceControl/changeset/view/c7f28bd90c59#EPPlusWebSample%2fGetSample.aspx.cs

/Mats