Search code examples
c#-4.0openxmlopenxml-table

Open XML 2.0 Word Document Merge Cell Horizontally


I have a task where I need to merge more than 2 cells, with below code I am able to merge only 2 cells in the table header under word document.

var tc = new TableCell();
Text header = new Text("");
if (j == 0)
{
    header = new Text("Header1");
    tc.Append(new TableCellProperties(
        new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "1620" },
        new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center }));
}
else if (j == 1)
{
    header = new Text("");
    tc.Append(new TableCellProperties(
        new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "0" }));
}
else if (j == 2)
{
    header = new Text("");
    tc.Append(new TableCellProperties(
       new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "0" }));
}
else if (j == 3)
{
    header = new Text("");
    tc.Append(new TableCellProperties(
       new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "0" }));
}
else if (j == 4)
{
    header = new Text("Header2");
    tc.Append(new TableCellProperties(
        new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "1076" },
        new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center }));
}
else if (j == 5)
{
    header = new Text("Header3");
    tc.Append(new TableCellProperties(
        new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "1004" },
        new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center }));
}

Run runHeaderRun = new Run(); 
runHeaderRun.Append(runHeader);
runHeaderRun.Append(header); 
paraHeader.Append(runHeaderRun);
tc.Append(paraHeader);

if (j == 0 || j == 2)
{
    tc.TableCellProperties = new TableCellProperties();
    tc.TableCellProperties.HorizontalMerge = new HorizontalMerge { Val = MergedCellValues.Restart };
}
else if (j == 1 || j == 3)
{
    tc.TableCellProperties = new TableCellProperties();
    tc.TableCellProperties.HorizontalMerge = new HorizontalMerge { Val = MergedCellValues.Continue };
}
headerRow.Append(tc);

table.Append(headerRow);

I get a result like this:
Sample

But I need it like this:
Sample


Solution

  • Although the code you posted does not compile, it looks like the problem is here:

    if (j == 0 || j == 2)
    {
        tc.TableCellProperties = new TableCellProperties();
        tc.TableCellProperties.HorizontalMerge = new HorizontalMerge { Val = MergedCellValues.Restart };
    }
    else if (j == 1 || j == 3)
    {
        tc.TableCellProperties = new TableCellProperties();
        tc.TableCellProperties.HorizontalMerge = new HorizontalMerge { Val = MergedCellValues.Continue };
    }
    

    I'm assuming that j is a loop index for the columns. Your code says to start a new horizontal merge on column 0 and continue it to column 1, then restart the merge on column 2 and continue it to column 3. This is exactly what we see in the output image you provided. If you want all four cells to be merged, then the first cell should have MergedCellValues.Restart and the other three should have MergedCellValues.Continue.

    Also I notice that you are creating a brand new TableCellProperties to set the HorizontalMerge in, rather than adding it to the TableCellProperties you created earlier in your code for those same cells. So that means that the properties you set earlier, like the TableCellVerticalAlignment, for example, will be lost for those cells.

    I think if you change the above section of code to the following, it will fix both problems:

    if (j < 4)
    {
        var merge = j == 0 ? MergedCellValues.Restart : MergedCellValues.Continue;
        tc.TableCellProperties.HorizontalMerge = new HorizontalMerge { Val = merge };
    }