Tôi đang sử dụng bản phát hành 3.1 của thư viện EPPlus để thử truy cập vào một trang tính trong tệp Excel. Khi tôi thử một trong các phương pháp sau, tôi nhận được System.ArgumentException : An item with the same key has already been added
.Cách truy cập trang tính trong EPPlus?
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()
Điều này có vẻ như chức năng rất cơ bản để có được như vậy bị phá vỡ .. Tôi đang làm điều gì sai?
Có thể rằng file excel là một file 'xls' ban đầu? EPPlus chỉ hiểu 'xlsx'. Cố gắng lưu nó dưới dạng 'xlsx' theo cách thủ công trước. –