ajr
ajr

Reputation: 974

Error while saving modified excel(xlsx) file with ClosedXML

I am trying to add title rows on an existing excel file (xlsx) with ClosedXML library and following code:

public void HandlePrintTitleRows(IList<int> sheetIndex)
{
    using (_workbook = new XLWorkbook(_filePath))
    {
        foreach (var i in sheetIndex)
        {
            IXLWorksheet workSheet = _workbook.Worksheet(i);
            workSheet.PageSetup.SetRowsToRepeatAtTop(1, 5);
        }
        _workbook.SaveAs(_filePath);
    }
}

File path is from SaveFileDialog dialog and it opens seemingly correct as XLWorkbook.

I am receiving this exception message from the _workbook.SaveAs method:

Attribute 'xfId' should have unique value.
Its current value '10' duplicates with others. in /x:styleSheet[1]/x:cellStyles[1]/x:cellStyle[21]
Attribute 'xfId' should have unique value.
Its current value '12' duplicates with others. in /x:styleSheet[1]/x:cellStyles[1]/x:cellStyle[24]
Attribute 'xfId' should have unique value.
Its current value '50' duplicates with others. in /x:styleSheet[1]/x:cellStyles[1]/x:cellStyle[55
Attribute 'xfId' should have unique value.
Its current value '50' duplicates with others. in /x:styleSheet[1]/x:cellStyles[1]/x:cellStyle[59]"

StackTrace:

at ClosedXML.Excel.XLWorkbook.Validate(SpreadsheetDocument package) in C:\Git\ClosedXML\ClosedXML\Excel\XLWorkbook_Save.cs:line 90

at ClosedXML.Excel.XLWorkbook.CreatePackage(String filePath, SpreadsheetDocumentType spreadsheetDocumentType, B...

I am not sure how to proceed with this problem, all tips would be greatly appreciated ! The excel file has been previously created by Telerik.Windows.Documents.Spreadsheet.Model.Workbook class and opens normally with excel application.

Upvotes: 0

Views: 2449

Answers (1)

Francois Botha
Francois Botha

Reputation: 4849

Open your original file (unmodified by ClosedXML) with the OpenXML SDK Validator to see if it's a valid file. If the validation errors occur in the original file, fix them, or you can disable validation in ClosedXML by using the SaveAs(_filePath, false) overload at your own risk.

Upvotes: 3

Related Questions