Unable to delete a worksheet using EPPlus

本小妞迷上赌 提交于 2019-12-10 19:27:10

问题


I am using this code:

ExcelPackage pck = new ExcelPackage(newFile);

var wk = pck.Workbook.Worksheets.SingleOrDefault(x => x.Name == "Content");

pck.Workbook.Worksheets.Delete(wk);

But in delete it gives me "IndexOutOfRangeException", but I am trying to delete from object, I have tried to delete by index "1", I just have two worksheets, and the same exception. The file and worksheet is not null, but when I execute delete in anyway I receive the "IndexOutOfRangeException".

What's happening?

Note: I have created this worksheet from ExcelPackage too and now i want delete it.


回答1:


Looks like you ran into some temporary bug/issue, that was already fixed. As of EpPlus 4.0.1.1, the following code works just fine:

var workbookFileInfo = new FileInfo(@"Workbook.xlsx");
using (var excelPackage = new ExcelPackage(workbookFileInfo))
{
    excelPackage.Workbook.Worksheets.Add("Worksheet1");
    excelPackage.Workbook.Worksheets.Add("Worksheet2");
    excelPackage.Save();
}
using (var excelPackage = new ExcelPackage(workbookFileInfo))
{
    var worksheet = excelPackage.Workbook.Worksheets.SingleOrDefault(x => x.Name == "Worksheet1");
    excelPackage.Workbook.Worksheets.Delete(worksheet);
    excelPackage.Save();
}

Try to update to the latest available stable version of EpPlus and if will not help you, please post additional details applicable for the latest version.



来源:https://stackoverflow.com/questions/22415693/unable-to-delete-a-worksheet-using-epplus

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