简体   繁体   English

C# 中的 OfficeOpenXML EPPlus - Excel 文件在写入一定大小的数据后会损坏

[英]OfficeOpenXML EPPlus in C# - Excel file becomes corrupt after a certain size of data is written to it

I am using the OfficeOpenXML library to export excel data in my .NET MVC Application.我正在使用OfficeOpenXML库在我的.NET MVC应用程序中导出 excel 数据。 However I noticed something funny going on.然而,我注意到一些有趣的事情正在发生。 I am able to download the file but when opening it I am prompted with the following message.我能够下载该文件,但打开它时,我收到以下消息提示。 If I hit "Yes" all the data is shown without any issues.如果我点击“是”,所有数据都会显示没有任何问题。

I noticed this prompt only shows up after a certain amount of data is written to the excel file.我注意到只有在将一定数量的数据写入 excel 文件后才会出现此提示。 And not a huge amount.而且数量不是很大。 If I write 1000 rows of data as shown below the prompt shows, but 10 rows it doesn't show.如果我按照提示显示的方式写入 1000 行数据,但没有显示 10 行。

在此处输入图片说明

Code below:代码如下:

public static ExcelPackage GenerateExcelFile<T>(ExcelPackage pck)
{
    ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Data");
    DataTable dt = new DataTable();

    dt.Columns.Add(new DataColumn { ColumnName = "A" });
    dt.Columns.Add(new DataColumn { ColumnName = "B" });
    dt.Columns.Add(new DataColumn { ColumnName = "C" });
    dt.Columns.Add(new DataColumn { ColumnName = "D" });
    dt.Columns.Add(new DataColumn { ColumnName = "E" });

    for (int i = 0; i < 1000; i++)
    {
        DataRow row = dt.NewRow();
        row["A"] = "Value";
        row["B"] = "Value";
        row["C"] = "Value";
        row["D"] = "Value";
        row["E"] = "Value";
        dt.Rows.Add(row);
    }
    ws.Cells["A1"].LoadFromDataTable(dt, true);
    return pck;
}

This is taken from the following controller:这是从以下控制器中获取的:

public HttpResponseMessage ExportGridToExcelWithFilters ()
{
    using (ExcelPackage pck = new ExcelPackage())
    {
        var excelData = ExcelExportVM.GenerateExcelFile(pck);
        using (MemoryStream ms = new MemoryStream())
        {
            excelData.SaveAs(ms);
            HttpResponseMessage responsePackage = new HttpResponseMessage();
            responsePackage.Content = new ByteArrayContent(ms.GetBuffer());
            responsePackage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            return responsePackage;
        }
    }
}

Changing the above code to produce less data:更改上面的代码以产生更少的数据:

for (int i = 0; i < 10; i++) //FROM 1000
{
    DataRow row = dt.NewRow();
    row["A"] = "Value";
    row["B"] = "Value";
    row["C"] = "Value";
    row["D"] = "Value";
    row["E"] = "Value";
    dt.Rows.Add(row);
}  

It now works and I am able to open the file without entering recovery mode.它现在可以工作了,我可以在不进入恢复模式的情况下打开文件。

I've had the same issue, this was caused by memorystream and the bad code :)我遇到了同样的问题,这是由内存流和错误代码引起的:)

When you handle the stream after the file save, I can assume you are trying to send it somewhere, for example HttpResponse, and if you do MemoryStream to byte[] conversion there, that should not be done with GetBuffer() method, call ToArray() instead.当您在文件保存后处理流时,我可以假设您正在尝试将其发送到某个地方,例如 HttpResponse,并且如果您在那里进行MemoryStreambyte[]转换,则不应使用GetBuffer()方法来完成,请调用ToArray()代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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