Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Export xlsx in ASP.NET Core - cannot open a file.

$
0
0

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

Here's my code: from controller:

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.

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


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>