This project has moved and is read-only. For the latest updates, please go here.

Write causes warning that the file is corrupt when trying to open it in Excel

Mar 9, 2015 at 5:03 PM
I am trying to generate the Simple Excel file using the plugin in ASP.Net with following Code.

The file gets generated successfully, however, after downloading when user open the file it gets a warning message indicating the file is corrupt, do you want excel to fix it ? How do I remove this message ? Below is my code.

private void CreateExcel()
        using (pck = new ExcelPackage())
            MemoryStream memory = new MemoryStream();

            String tmpFile = Path.GetTempFileName();
            FileStream liveStream = new FileStream(tmpFile, FileMode.Create, FileAccess.ReadWrite);

            //Create the worksheet
            ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Log");

            ws.Cells["A1"].Value = "Test";

            //Write it back to the client
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.AddHeader("content-disposition", "attachment;  filename=ExcelDemo.xlsx");

When I checked the details of the fix that Excel is performing, I found below XML

<?xml version="1.0" encoding="UTF-8" standalone="true"?>
-<recoveryLog xmlns=""><logFileName>error036720_01.xml</logFileName><summary>Errors were detected in file 'C:\Users\ipatel\Downloads\ExcelDemo (1).xlsx'</summary>-<additionalInfo><info>Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.</info></additionalInfo></recoveryLog>
Mar 9, 2015 at 5:12 PM
Please, take a look at this thread and tell us if it work
Marked as answer by imran7799 on 3/9/2015 at 9:54 AM
Mar 9, 2015 at 5:54 PM
It works perfectly!!!!!

Thank you very much for a quick response.
Mar 9, 2015 at 5:55 PM
Just copying the answer from the suggested thread.

Call response.clear before send stream back to client.
                Response.AddHeader("content-disposition", "attachment;  filename=file.xlsx");
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";                    
Marked as answer by imran7799 on 3/9/2015 at 9:55 AM