How to access worksheets in EPPlus?
Asked Answered
C

6

25

I'm using the 3.1 release of EPPlus library to try to access a worksheet in an Excel file. When I try either of the following methods I get a System.ArgumentException : An item with the same key has already been added.

using (ExcelPackage package = new ExcelPackage(new FileInfo(sourceFilePath)))
{
   var worksheet = package.Workbook.Worksheets[0];

   // OR

   foreach (var excelWorksheet in package.Workbook.Worksheets)
   ...
}

Exception Stack:

System.ArgumentException : An item with the same key has already been added.
   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
   at OfficeOpenXml.ExcelNamedRangeCollection.Add(String Name, ExcelRangeBase Range)
   at OfficeOpenXml.ExcelWorkbook.GetDefinedNames()
   at OfficeOpenXml.ExcelPackage.get_Workbook()

This seems like very basic functionality to have be so broken.. am I doing something wrong?

Coroner answered 26/11, 2012 at 15:55 Comment(1)
Is it possible that the excel file was a xls file originally? EPPlus does only understand xlsx. Try to save it as xlsx manually first.Sundried
C
7

The workbook in question had named ranges defined. These were causing problems so I created a new xlsx file with just the data I needed and it was able to open fine.

Coroner answered 26/11, 2012 at 18:10 Comment(2)
I know this is 2 years old, but if this solved your problem, you should accept your own answer to help highlight it for others who may have the same issueRebecarebecca
this was the only probable solution for me, among all answers. I deleted Named Ranges; it didn't help. Point is that Worksheets object by itself throws the exception.Caia
M
35

I believe that excel does worksheets from index 1 not index 0

 var worksheet = package.Workbook.Worksheets[0]; 

should be

var worksheet = package.Workbook.Worksheets[1];

to read the first worksheet.

Martial answered 17/3, 2013 at 3:3 Comment(1)
In excel and pretty everything that connects to it, indexes start with 1, not 0. Nice reminder. Thanks.Uncovenanted
P
27

Also, you can manage them by referencing the name:

var worksheet = package.Workbook.Worksheets["Sheet1"];
Prudi answered 6/2, 2014 at 14:44 Comment(0)
E
8

At least with Epplus 3.1.3.0 you can simply use the following to access the first worksheet.

ExcelWorksheet workSheet = excel.Workbook.Worksheets.First();
Exsect answered 24/8, 2015 at 10:16 Comment(0)
C
7

The workbook in question had named ranges defined. These were causing problems so I created a new xlsx file with just the data I needed and it was able to open fine.

Coroner answered 26/11, 2012 at 18:10 Comment(2)
I know this is 2 years old, but if this solved your problem, you should accept your own answer to help highlight it for others who may have the same issueRebecarebecca
this was the only probable solution for me, among all answers. I deleted Named Ranges; it didn't help. Point is that Worksheets object by itself throws the exception.Caia
P
3

Make sure the document created or saved with MS Excel (not OpenOffice, Libre Office, etc.)

Parterre answered 25/9, 2015 at 11:3 Comment(0)
D
0

To access/find all worksheets, I used:

 var file = new FileInfo(filename);

 ExcelPackage pck = new ExcelPackage(file);
           
 foreach (var excelWorksheet in pck.Workbook.Worksheets) 
 {
     Console.WriteLine("found sheet: " + excelWorksheet.Name);
 }
Disfranchise answered 20/7, 2022 at 13:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.