我正在使用 Shaman.EPPlus,应该兼容的 EPPlus 版本使用 ASP.NET Core MVC.我正在尝试将对象集合导出为 xlxs 文件.代码如下所示:
I am using Shaman.EPPlus, a version of EPPlus that should be compatible with ASP.NET Core MVC. I am trying to export a collection of object as xlxs file. The code looks like this:
foreach(var client in clientsToExport)
{
clientList.Add(new object[] { "FirstName", client.FirstName });
}
MemoryStream stream = new MemoryStream();
using (ExcelPackage pck = new ExcelPackage(stream))
{
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Clients");
ws.Cells["A1"].LoadFromArrays(clientList);
pck.Save();
Response.Clear();
Response.Headers.Add("content-disposition", "attachment; filename=Clients.xlsx");
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
var bytes = pck.GetAsByteArray();
Response.Body.WriteAsync(bytes, 0, bytes.Length);
}
调用GetAsByteArray方法时似乎抛出了包含Par已经存在"消息的异常.
It seems that an exception containing the message "Par already exist" is thrown when GetAsByteArray method is called.
在 OfficeOpenXml.Packaging.ZipPackage.CreatePart(Uri partUri, String contentType, CompressionLevel compressionLevel)
在 OfficeOpenXml.ExcelWorkbook.Save()
在 OfficeOpenXml.ExcelPackage.GetAsByteArray(布尔保存)
at OfficeOpenXml.Packaging.ZipPackage.CreatePart(Uri partUri, String contentType, CompressionLevel compressionLevel)
at OfficeOpenXml.ExcelWorkbook.Save()
at OfficeOpenXml.ExcelPackage.GetAsByteArray(Boolean save)
你知道我可以检查什么吗?
Do you know what could I check?
问题出在这几行:
pck.Save();
....
var bytes = pck.GetAsByteArray();
这两个调用都会导致包被 Epplus 关闭.您不需要 .Save
调用,因为无论如何 .GetAsByteArray
都会自动调用它,因此只需删除第一行即可.
Both calls will cause the package to be closed by Epplus. You do not need the .Save
call since that will automatically be called by .GetAsByteArray
anyway so simply remove the first line.
这篇关于Shaman.EPPlus + ASP.NET Core MVC - 部分已经存在异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!