1
votes

Based on documentation examples from: https://github.com/ClosedXML/ClosedXML/wiki/Using-Tables

In a C# application, I use ClosedXML to create a workbook with new content. Part of the content is a range of cells that I would like to style as a table. However, the first "column" of the table actually spans multiple cell columns. Without applying any table formatting, the result is like the following simplified snippet.

enter image description here

In my C# application, after I have populated the cells of that table, I grab the range of those cells and create an IXLTable from them.

IXLWorkbook book = ...;
IXLWorksheet sheet = ...;

//------------------------------
// Populate cells with milestone data.
...

//------------------------------
// Create a table.
IXLRange range = sheet.Range( "A1:D3");
IXLTable table = range.CreateTable();

//------------------------------
// Done.
book.SaveAs( "foo.xlsx" );

That works, insofar as the C# application compiles, executes, and generates a workbook file.

However, when I open that workbook in Microsoft Excel, Excel decides that there are errors and prompts me whether to repair the workbook. Excel repairs the workbook by just eliminating any <table> that was in it.

In contrast, if the first "column" of the table just spans one cell column, then the created table has no errors and survives to be loaded in Excel. The result is like the following simplified snippet.

enter image description here

Therefore, the root problem is that the table contains merged cells.

Is there any way to accomplish what I want, with one of the table "columns" consisting of merged cells that span multiple cell columns?

1

1 Answers

0
votes
IXLRange range = sheet.Range( "A1:D3");
range.Range(startRow, startColumn, startRow, endColumn).Merge();