0

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();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • provided code is hardly enough to debug this issue – Nick Farsi Aug 18 '22 at 12:55
  • I am running this on winforms application with a button click, but even if i just copy and paste this part to the main function of the app it still gives the same error – Berkcan Akca Aug 18 '22 at 13:24
  • Try commenting out the 4th and 5th lines and see if it still throws the error. Could be an issue with the file? If no error, add in 4th & 5th one at a time. See if its the value or worksheet causing the problem. – flashsplat Aug 18 '22 at 13:28
  • It still gives the same error even if the 4th and 5th line is commented, i tried using an emptyfile info and using saveas to save the file it worked so we know that epplus works as it can create an empty excel, i tried reading with the same fileinfo and saveas to another location with different name it failed, no matter what i do as long as i start with this existing fileinfo it fails to save and gives the same error, after a day of failing i just gave up and used closedxml to do the same thing with same file(which worked flawlessy first time btw) i still dont know why openxml isnt working – Berkcan Akca Aug 21 '22 at 07:24

0 Answers0