Error dialog displayed when opening an excel file generated with EPPlus

Waqas Ali picture Waqas Ali · Sep 9, 2014 · Viewed 12.3k times · Source

I am creating an Excel file using the EPPlus library. When I create file and open up the file, the following pop up message shows:

We found a problem with some content in 'ExcelDemo.xlsx'. Do you want us to try to recover as much as we can? If you trust the source of this workbook, Click Yes

I am using following code

using (ExcelPackage pck = new ExcelPackage())
{
    //Create the worksheet
    ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Demo");

    //Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
    ws.Cells[1, 2].Value = "Excel Download";

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

Is there problem in my code or is this an Excel issue?

Answer

Ads picture Ads · Feb 19, 2015

At the start, you need to add in a:

Response.Clear();

Then at the end add a

Response.End();