This project has moved and is read-only. For the latest updates, please go here.

The given key was not present in the dictionary

Sep 19, 2012 at 3:42 PM
Edited Sep 19, 2012 at 3:42 PM

I am attempting to open a template .xlsx workbook, set a value to a named range and then save the workbook.  Seems like a simple task, but I am getting the following error when saving:

KeyNotFoundException: The given key was not present in the dictionary.

My code is as follows:

 XLWorkbook rptWorkbook = new XLWorkbook(templateFile);

rptWorkbook.Range("_ClientName").SetValue("Test Client");

rptWorkbook.SaveAs(templatePath + "Testing.xlsx");

The workbook does contain a named range called "_ClientName" and it has a workbook scope.  Even though I get this exception, the wrokbook is still created and the value "Test Client" is written to the correct range.

Any idea why this exception would be thrown?

Sep 19, 2012 at 4:13 PM

Here is the stack trace from the SaveAs function in:

[KeyNotFoundException: The given key was not present in the dictionary.]
   System.Collections.Generic.Dictionary`2.get_Item(TKey key) +9462333
   ClosedXML.Excel.XLCFCellIsConverter.Convert(IXLConditionalFormat cf, Int32 priority, SaveContext context) +95
   ClosedXML.Excel.XLCFConverters.Convert(IXLConditionalFormat conditionalFormat, Int32 priority, SaveContext context) +80
   ClosedXML.Excel.XLWorkbook.GenerateWorksheetPartContent(WorksheetPart worksheetPart, XLWorksheet xlWorksheet, SaveContext context) +22705
   ClosedXML.Excel.XLWorkbook.CreateParts(SpreadsheetDocument document) +3444
   ClosedXML.Excel.XLWorkbook.CreatePackage(String filePath) +159
   ClosedXML.Excel.XLWorkbook.SaveAs(String file) +224
It appears that the problem is with a conditional formatting that I am using. I will do a little more testing to see what I can find.  I'll keep you posted
Sep 19, 2012 at 4:23 PM

It seems to not like a conditional formatting rule that I have with the following formula:


Are conditional formatting rules with formulas not supported by closedXML or is there something specific about this formula that it doesn't like?

Sep 19, 2012 at 4:26 PM

I don't think that's the problem. Create an issue and attach the file so I can inspect it.