VahidN / EPPlus.Core

EPPlus.Core is an unofficial port of the EPPlus library to .NET Core
GNU Lesser General Public License v3.0
370 stars 93 forks source link

Export xlsx in ASP.NET Core - file is corrupted #9

Closed mskuratowski closed 7 years ago

mskuratowski commented 7 years ago

I'm trying to export XLSX file in ASP.NET Core using EPPlus.Core library.

Here's my code:

        byte[] bytes;

        MemoryStream stream = new MemoryStream();
        using (ExcelPackage package = new ExcelPackage(stream))
        {
            ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Employee");
            //First add the headers
            worksheet.Cells[1, 1].Value = "ID";
            worksheet.Cells[1, 2].Value = "Name";
            worksheet.Cells[1, 3].Value = "Gender";
            worksheet.Cells[1, 4].Value = "Salary (in $)";

            //Add values
            worksheet.Cells["A2"].Value = 1000;
            worksheet.Cells["B2"].Value = "Jon";
            worksheet.Cells["C2"].Value = "M";
            worksheet.Cells["D2"].Value = 5000;

            worksheet.Cells["A3"].Value = 1001;
            worksheet.Cells["B3"].Value = "Graham";
            worksheet.Cells["C3"].Value = "M";
            worksheet.Cells["D3"].Value = 10000;

            worksheet.Cells["A4"].Value = 1002;
            worksheet.Cells["B4"].Value = "Jenny";
            worksheet.Cells["C4"].Value = "F";
            worksheet.Cells["D4"].Value = 5000;
            bytes = package.GetAsByteArray();
        }

        return File(bytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "test");

It was successfully exported a file but there is an error message when I try to open a file using Microsoft Excel I'm getting an error message:

"We found a problem with some content in 'text.xslx'. Do you want to us to try to recover as much as we can? If you trust the source of this workbook, click Yes."

and after I click "Yes" button, I'm getting an error message:

"Excel cannot open the file "test.xslx", because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.

Moreover, I tried to unzip the xslx file and I'm getting an error message: "Can not open the file as [zip] archive. Is not archive. Warnings: Headers Error".

Also, I opened a generated file using notepad++ and it starts with "PK", so it's a zip file.

When I generate an Excel file and save it to the wwwroot folder it works fine. I used a code from talkingdotnet.com/import-export-xlsx-asp-net-core

Do you have any idea how can I solve this problem?

VahidN commented 7 years ago
mskuratowski commented 7 years ago

I still can not open the file. Maybe that's why because I use a docker ?

VahidN commented 7 years ago

Can you run this app?

mskuratowski commented 7 years ago

You know what, it works fine. The problem is when I test an app using Swagger.

To sum up, everything is okay. Thanks