简体   繁体   中英

Corrupt Excel file output using EPPlus in ASP.NET / C#

I'm trying to use EPPlus to create a report inside an ASP.NET application. I tried using the code provided into samples package but I'm having some troubles.

The following code is executed without error:

        ExcelPackage pck = new ExcelPackage();
        var ws = pck.Workbook.Worksheets.Add("Sample1");

        _ws.Cells["A1"].Value = "COD. CONV.";
        _ws.Cells["A1"].Style.Font.Bold = true;
        _ws.Cells["A1"].Style.Border.Bottom.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thick;
        _ws.Cells["B1"].Value = "RAGIONE SOCIALE";
        _ws.Cells["B1"].Style.Font.Bold = true;
        _ws.Cells["B1"].Style.Border.Bottom.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thick;
        _ws.Cells["C1"].Value = "COMMERCIALE A";
        _ws.Cells["C1"].Style.Font.Bold = true;
        _ws.Cells["C1"].Style.Border.Bottom.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thick;
        _ws.Cells["D1"].Value = "PROVINCIA";
        _ws.Cells["D1"].Style.Font.Bold = true;
        _ws.Cells["D1"].Style.Border.Bottom.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thick;
        _ws.Cells["E1"].Value = "ZONA";
        _ws.Cells["E1"].Style.Font.Bold = true;
        _ws.Cells["E1"].Style.Border.Bottom.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thick;
        _ws.Cells["F1"].Value = "TELEFONO";
        _ws.Cells["F1"].Style.Font.Bold = true;
        _ws.Cells["F1"].Style.Border.Bottom.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thick;
        _ws.Cells["G1"].Value = "EMAIL";
        _ws.Cells["G1"].Style.Font.Bold = true;
        _ws.Cells["G1"].Style.Border.Bottom.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thick;

        int _i = 2;

        foreach (DataRow _drRow in dtAnagrafiche.Rows)
        {
            _ws.Cells["A"+_i.ToString()].Value = _drRow["codice"].ToString();
            _ws.Cells["B"+_i.ToString()].Value = _drRow["Nome"].ToString();
            _ws.Cells["C"+_i.ToString()].Value = "";
            _ws.Cells["D"+_i.ToString()].Value = _drRow["Provincia"].ToString();
            _ws.Cells["E"+_i.ToString()].Value = _drRow["Zona"].ToString();
            _ws.Cells["F"+_i.ToString()].Value = _drRow["Telefono"].ToString();
            _ws.Cells["G"+_i.ToString()].Value = _drRow["Email"].ToString();

            _i++;
        }

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

but the resulting file cannot be opened by Microsoft office if not 'recovered', other MS Office compatibile applications (ie OpenOffice) cannot open the file.

I can provide the output file if needed.

https://drive.google.com/file/d/0B-lPXYt7laDrbUFKbFZEWEwxckk/view?usp=sharing

BTW I'm using the last (4.0.5) EPPlus package obtained trough nuget, and running it in ASP.NET 4.5 web appplication.

You're missing a call to Response.End() . Without this, you're sending the response with the binary payload (the .xlsx file), which is coming over correctly, then the .aspx page that you're coding this under is being sent in the payload as well. Proof here as shown in a hex editor.

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

Should do the trick.

As an aside, I would suggest saving the file, then doing a Response.Redirect() to the URL of the file, instead, but that's unrelated to this specific issue.

EDIT: Notably, in normal circumstances, I would suggest avoiding Response.End() , but, that is the quickest way to solve the problem you've coded yourself into. I would suggest looking for better ways to serve up these files in general, as per my above suggestion to Response.Redirect() to a saved location of the file.

Try changing your code to the following notice how I am using the string.Format function to create the filename + extension

you need to declare a constant fileName. if worse comes to worse change the .xlsx to .xls

Response.Clear();    
Response.ClearHeaders();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AppendHeader("Content-Disposition", string.Format("attachment;filename={0}.xlsx", fileName));
Response.BinaryWrite(_pck.GetAsByteArray());
Response.Flush();
Response.End();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM