EPPlus C# cannot save a file, no matter what. It gives an error: An item with the same key has already been added. Key:

388 Views Asked by At

I am writing a really simple save operation with EPPlus, and it keeps saying that the key already exists after I hit save.

This exception was originally thrown at this call stack:

System.ThrowHelper.ThrowAddingDuplicateWithKeyArgumentException<T>(T)
System.Collections.Generic.Dictionary<TKey, TValue>.TryInsert(TKey, TValue, System.Collections.Generic.InsertionBehavior)
OfficeOpenXml.Table.ExcelTableCollection.ExcelTableCollection(OfficeOpenXml.ExcelWorksheet)
OfficeOpenXml.ExcelWorksheet.Tables.get()
OfficeOpenXml.ExcelWorkbook.ReadAllTables()
OfficeOpenXml.ExcelWorksheet.Tables.get()
OfficeOpenXml.Style.Dxf.DxfStyleHandler.UpdateDxfXmlTables(OfficeOpenXml.ExcelStyles, System.Xml.XmlNode, OfficeOpenXml.ExcelWorksheet)
OfficeOpenXml.Style.Dxf.DxfStyleHandler.UpdateDxfXmlWorksheet(OfficeOpenXml.ExcelWorkbook, OfficeOpenXml.ExcelStyles, System.Xml.XmlNode)
OfficeOpenXml.Style.Dxf.DxfStyleHandler.UpdateDxfXml(OfficeOpenXml.ExcelWorkbook)
OfficeOpenXml.ExcelWorkbook.Save()
...
[Call Stack Truncated]

Here is the code:

string path = @"D:\RevenueCopy.xlsx";

ExcelPackage.LicenseContext = OfficeOpenXml.LicenseContext.NonCommercial;

FileInfo file = new FileInfo(path);

ExcelPackage package = new ExcelPackage(file);

ExcelWorksheet ws = package.Workbook.Worksheets[CurrentMarket + 1];

ws.Cells[21, 41].Value = "TESSSTTTTTT";

package.Save();
0

There are 0 best solutions below