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

C# ASP.NET/C中使用EPPlus的Excel文件输出损坏,c#,asp.net,epplus,C#,Asp.net,Epplus,我正在尝试使用EPPlus在ASP.NET应用程序中创建报表。我尝试使用示例包中提供的代码,但遇到了一些问题 执行以下代码时不会出错: ExcelPackage pck = new ExcelPackage(); var ws = pck.Workbook.Worksheets.Add("Sample1"); _ws.Cells["A1"].Value = "COD. CONV."; _ws.Cells["A1"].Style.F

我正在尝试使用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 office无法打开生成的文件,其他MS office兼容应用程序(即OpenOffice)无法打开该文件

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


顺便说一句,我正在使用通过nuget获得的最后一个4.0.5 ePlus软件包,并在ASP.NET 4.5 web应用程序中运行它。

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

您需要声明一个常量文件名。如果情况变得更糟,请将.xlsx更改为.xls

你错过了一个回应的电话。结束。如果不这样做,您将使用二进制负载发送响应,即.xlsx文件,该文件将正确提交,然后您正在编写此文件的.aspx页面也将在负载中发送

我们应该做到这一点

另一方面,我建议保存文件,然后进行响应。改为重定向到文件的URL,但这与此特定问题无关


编辑:值得注意的是,在正常情况下,我建议避免回应。结束,但是,这是解决你自己编码的问题的最快方法。我建议按照我上面对Response的建议,寻找更好的方法来提供这些文件。重定向到文件的保存位置。

很抱歉,即使更改BinaryWrite调用也没有成功。请尝试Response。在Response之前清除。BinaryWrite?您是否可以尝试先将字节保存到文件中,以区分Excel文件输出和通过响应将其写回之间的问题?您尝试过在记事本中打开结果文件吗?@shriop:在记事本中打开文件将毫无用处,因为.xlsx文件被压缩为zip文件。@weirdgyn:您没有在写响应时进行写操作。您应该运行Response.End。您正在发送整个二进制文件,然后在最后,.aspx页面也被发送过来。请参见此处的屏幕截图:Response.Clear无法解决此问题。。。您的意思是在“上载”之前将流序列化为“服务器上”的文件?我尝试使用Microsoft office“恢复”来“打开”result.xslx文件,我能够看到内容,并且看起来是正确的,但该文件以某种方式损坏,我无法理解。如果您尝试使用其他软件OpenOffice打开该文件,则会报告I/O错误。我还能够将.xslx文件作为归档文件“upack”起来,并且生成的结构看起来也正确。Thnx。xls与.xslx有些不同。。。不管怎样,你的建议很好!Thnx文件名没有问题,我不会硬编码,我会使用string.Format函数,这样它可以动态创建文件,因此如果您想创建一个带有名称和日期等的文件名。。您不必硬编码,例如publicstringfilename=Lista_Anagrafiche_{0};fileName=string.FormatfileName,DateTime.Now.tostringmmaddyyy\u hhmmss;只是一个暗示我的猜测是将文件扩展名更改为与文件本身的实际内容不匹配的扩展名。。。无论如何,真正的代码我不是在应用程序中硬编码文件名,而是在运行时生成。
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.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    Response.AddHeader("content-disposition", "attachment;  filename=Lista_Anagrafiche.xlsx");
    Response.BinaryWrite(_pck.GetAsByteArray());
    Response.End();