gpt4 book ai didi

c# - 打开使用 EPPlus 生成的 excel 文件时显示的错误对话框

转载 作者:可可西里 更新时间:2023-11-01 08:50:53 27 4
gpt4 key购买 nike

我正在使用 EPPlus 库创建一个 Excel 文件。当我创建文件并打开文件时,显示以下弹出消息:

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

我正在使用下面的代码

using (ExcelPackage pck = new ExcelPackage())
{
//Create the worksheet
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Demo");

//Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
ws.Cells[1, 2].Value = "Excel Download";

Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=ExcelDemo.xlsx");
Response.BinaryWrite(pck.GetAsByteArray());
}

我的代码有问题还是 Excel 有问题?

最佳答案

在我的例子中,问题在于调用

package.Save();

并使用

Response.BinaryWrite(package.GetAsByteArray());

同时。

当您调用 package.GetAsByteArray() 时,它会在内部执行以下操作:

this.Workbook.Save();
this._package.Close();
this._package.Save(this._stream);

因此,调用 package.Save 两次会导致在 Excel 中打开时出现此错误。

关于c# - 打开使用 EPPlus 生成的 excel 文件时显示的错误对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25750616/

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