1

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?

Mike Finch
  • 746
  • 1
  • 7
  • 20

1 Answers1

0
IXLRange range = sheet.Range( "A1:D3");
range.Range(startRow, startColumn, startRow, endColumn).Merge();
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Emrah AKIN
  • 137
  • 1
  • 6
  • 1
    This answer was flagged for review as [Low Quality](https://stackoverflow.com/help/review-low-quality) and could benefit from an explanation. Here are some guidelines for [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Code only answers are **not good answers** and are likely to be downvoted and/or deleted because they are **not useful** to a community of learners. It's only obvious to you. Explain what it does, and how it's different / better than existing answers (if there are any). [From Review](https://stackoverflow.com/review/low-quality-posts/29832455) – Trenton McKinney Sep 15 '21 at 21:22