Home » excel » c# – Unable to delete a worksheet using EPPlus

c# – Unable to delete a worksheet using EPPlus

Posted by: admin April 23, 2020 Leave a comment


I am using this code:

ExcelPackage pck = new ExcelPackage(newFile);

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


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.

How to&Answers:

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

var workbookFileInfo = new FileInfo(@"Workbook.xlsx");
using (var excelPackage = new ExcelPackage(workbookFileInfo))
using (var excelPackage = new ExcelPackage(workbookFileInfo))
    var worksheet = excelPackage.Workbook.Worksheets.SingleOrDefault(x => x.Name == "Worksheet1");

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.