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



我正在尝试使用 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");
但是,如果没有

"恢复",则Microsoft无法打开生成的文件,则其他MS Office兼容应用程序(即OpenOffice)无法打开该文件。

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

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

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

您错过了Response.End()的电话。如果没有这个,您将使用二进制有效负载(.xlsx文件)发送响应,该有效负载正确返回,然后您正在编码的.aspx页面也会在有效负载中发送。此处证明,如十六进制编辑器所示。

    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()到文件的保存位置。

尝试将代码更改为以下请注意我如何使用字符串。格式化功能创建文件名+扩展名

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

最新更新