gpt4 book ai didi

c# - EPPlus - 保存在内存中创建的 excel 并在 excel 上打开它会出错

转载 作者:行者123 更新时间:2023-12-04 12:43:00 31 4
gpt4 key购买 nike

目标是在内存中创建一个 excel 文件并使用 ASP.Net Core 中内置的 API 在浏览器中下载它,但是将 excel 保存为流并将其转换为字节数组,然后在 excel 上打开它(Office 365版本 1803) 给出错误:
Excel Error
代码字节数组:

public IActionResult Export()
{
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.xlsx");
}

更新:添加了内存流示例

代码内存流:
public IActionResult Export()
{
using (ExcelPackage package = new ExcelPackage())
{
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;
var stream = new MemoryStream(package.GetAsByteArray());

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

更新 2 :使用 .net core 2.1 创建了一个新项目并且代码有效。这似乎是一个与项目相关的错误,以某种方式请求的内容正在被操纵。要调查导致问题的原因并发布它的解决方案。

最佳答案

我使用 实现了类似的场景package.Stream 而不是 package.GetAsByteArray .
(最后一个对我来说也失败了。)

注意 package.Save 我在你的代码中错过了。
( GetAsByteArray 也会失败,即使是 Package.Save 。)

public IActionResult Export()
{
MemoryStream content = new MemoryStream(); // Gets disposed by FileStreamResult.
using (ExcelPackage package = new ExcelPackage(content))
{
// Code to create the content goes here.

package.Save();
}

content.Position = 0;

return new FileStreamResult(content, "application/octet-stream") {
FileDownloadName = "test.xlsx"
};
}

关于c# - EPPlus - 保存在内存中创建的 excel 并在 excel 上打开它会出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51652745/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com