打开XML 2.0 Word文档水平合并单元格

Open XML 2.0 Word Document Merge Cell Horizontally

我有一个任务需要合并 2 个以上的单元格,使用下面的代码我只能合并 word 文档下 table header 中的 2 个单元格。

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);

我得到这样的结果:

但我需要这样:

虽然您发布的代码无法通过编译,但看起来问题出在这里:

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 };
}

我假设 j 是列的循环索引。您的代码表示在第 0 列开始新的水平合并并继续到第 1 列,然后在第 2 列重新开始合并并继续到第 3 列。这正是我们在您提供的输出图像中看到的。如果要合并所有四个单元格,则第一个单元格应具有 MergedCellValues.Restart,其他三个单元格应具有 MergedCellValues.Continue.

我还注意到您正在创建一个全新的 TableCellProperties 来设置 HorizontalMerge,而不是将其添加到您之前在代码中为相同的代码创建的 TableCellProperties细胞。因此,这意味着您之前设置的属性(例如 TableCellVerticalAlignment)将在这些单元格中丢失。

我认为如果将上面的代码部分更改为以下代码,它将解决这两个问题:

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