我正在使用3.1版本的EPPlus庫來嘗試訪問Excel文件中的工作表。當我嘗試以下任一方法時,我會收到System.ArgumentException : An item with the same key has already been added
。如何訪問EPPlus中的工作表?
using (ExcelPackage package = new ExcelPackage(new FileInfo(sourceFilePath)))
{
var worksheet = package.Workbook.Worksheets[0];
// OR
foreach (var excelWorksheet in package.Workbook.Worksheets)
...
}
異常堆棧:
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()
這似乎是非常基本的功能已經被打破這樣..我做錯了什麼?
有沒有可能是Excel文件是一個'xls'文件最初? EPPlus只能理解'xlsx'。嘗試首先手動將其保存爲'xlsx'。 –