When an xlsm file is imported and then exported, a new ThisWorkbook object is created in the VBA browser. This creates issues when making VBA functions that have to be executed upon opening of the file.
When opening an Xlsx file with conditional formatting I get an Exception:
"System.ArgumentNullException: 'Value cannot be null. (Parameter 'formatting')'
at Telerik.Windows.Documents.Spreadsheet.Model.ConditionalFormattings.ConditionalFormattingDxfRule..ctor(DifferentialFormatting formatting)
at Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.Model.Elements.Worksheets.ConditionalFormattingRuleElement.GetRule(DifferentialFormatting formatting)
at Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.Model.Elements.Worksheets.ConditionalFormattingRuleElement.OnAfterRead(IXlsxWorksheetImportContext context)
at Telerik.Windows.Documents.FormatProviders.OpenXml.Model.Elements.OpenXmlElementBase.ReadChildElements(IOpenXmlReader reader, IOpenXmlImportContext context)
at Telerik.Windows.Documents.FormatProviders.OpenXml.Model.Elements.OpenXmlElementBase.Read(IOpenXmlReader reader, IOpenXmlImportContext context)
at Telerik.Windows.Documents.FormatProviders.OpenXml.Model.Elements.OpenXmlElementBase.ReadChildElements(IOpenXmlReader reader, IOpenXmlImportContext context)
at Telerik.Windows.Documents.FormatProviders.OpenXml.Model.Elements.OpenXmlElementBase.Read(IOpenXmlReader reader, IOpenXmlImportContext context)
at Telerik.Windows.Documents.FormatProviders.OpenXml.OpenXmlImporter`1.ImportPartFromArchive(ZipArchiveEntry zipEntry, PartBase part, IOpenXmlImportContext context)
at Telerik.Windows.Documents.FormatProviders.OpenXml.OpenXmlImporter`1.Import(Stream input, IOpenXmlImportContext context)
at Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.XlsxFormatProvider.ImportOverride(Stream input)
at WpfApp1.MainWindow.StartExcel() in C:\Users\larse\source\repos\WpfApp1\MainWindow.xaml.cs:line 49"
To quote my colleage: "After dissecting the Excel template file, I have isolated what is causing the issue. If conditional formatting is used to disable cell boarders it results in the error, if the conditional formatting is updated to turn the necessary borders white, instead of disabling the report exports correctly, the following screenshot also explains."
If a rule is added with no formatting (for example, in the case when we want to add a rule which preserves the default formatting in some cases) the exported document is corrupted.
DifferentialFormatting formatting2 = new DifferentialFormatting();
ContainsBlanksRule rule2 = new ContainsBlanksRule(formatting2);
rule2.Formatting = formatting2;
ConditionalFormatting conditionalFormatting2 = new ConditionalFormatting(rule2);
worksheet.Cells[1, 2, 12, 2].AddConditionalFormatting(conditionalFormatting2);
A possible workaround is to add formatting to the rule, which copies the default:
DifferentialFormatting formatting2 = new DifferentialFormatting();
formatting2.Fill = new PatternFill(PatternType.Solid, Colors.Transparent, Colors.Transparent);
CellBorder border = new CellBorder(CellBorderStyle.Thin, new ThemableColor(Color.FromRgb(212, 212, 212)));
formatting2.LeftBorder = border;
formatting2.TopBorder = border;
formatting2.RightBorder = border;
formatting2.BottomBorder = border;
Applying text format to a column also sets the format to the next column.
This happens when the columns have a size different from the default and two consecutive columns with different format also have the same size. This can be worked around with a slight change in the column sizes:
int columnCount = worksheet.UsedCellRange.ColumnCount;
for(int i = 1; i < columnCount; i++)
{
ColumnWidth colWidth = worksheet.Columns[i].GetWidth().Value;
if(colWidth.Value != SpreadsheetDefaultValues.DefaultColumnWidth)
{
ColumnWidth previousColWidth = worksheet.Columns[i - 1].GetWidth().Value;
if(colWidth.Equals(previousColWidth))
{
worksheet.Columns[i].SetWidth(new ColumnWidth(colWidth.Value + 1, colWidth.IsCustom));
}
}
}