在 ASP.NET / C# 中使用 EPPlus 损坏 Excel 文件输出

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

我正在尝试使用 EPPlus 在 ASP.NET 应用程序中创建报告。我尝试使用样本包中提供的代码,但遇到了一些麻烦。

以下代码执行无误:

        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");

但如果不是'recovered',则生成的文件无法由 Microsoft office 打开,其他 MS Office 兼容应用程序(即 OpenOffice)无法打开该文件。

如果需要我可以提供输出文件。

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

顺便说一句,我正在使用通过 nuget 获得的最后一个 (4.0.5) EPPlus 包,运行 它在 ASP.NET 4.5 网络应用程序中。

尝试将您的代码更改为以下注意我如何使用 string.Format 函数创建文件名 + 扩展名

您需要声明一个常量文件名。如果情况变得更糟,请将 .xlsx 更改为 .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();

您未接来电 Response.End()。如果没有这个,您将发送带有二进制有效负载(.xlsx 文件)的响应,它会正确传输,然后您正在对其进行编码的 .aspx 页面也会在有效负载中发送。 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();

应该可以解决问题。

顺便说一句,我建议保存文件,然后对文件的 URL 执行 Response.Redirect(),但这与此特定问题无关。

编辑:值得注意的是,在正常情况下,我建议避免使用 Response.End(),但是,这是解决您自己编写的问题的最快方法。我建议寻找更好的方法来提供这些文件,按照我上面的建议 Response.Redirect() 到文件的保存位置。