Shaman.EPPlus + ASP.NET Core MVC - Part already exist exception

╄→尐↘猪︶ㄣ 提交于 2019-11-30 05:43:08

问题


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);
}

It seems that an exception containing the message "Par already exist" is thrown when GetAsByteArray method is called.

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?


回答1:


The problem are these line:

pck.Save();
....
var bytes = pck.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.



来源:https://stackoverflow.com/questions/40064880/shaman-epplus-asp-net-core-mvc-part-already-exist-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!