我如何使用 OpenXML C# 水平合并 Microsoft Word table 单元格

How i can merge Microsoft Word table cells horizontally using OpenXML C#

我在 Microsoft Word 中有一个 table。 我需要合并 table 行中的两个单元格。 我得到了我需要的细胞:

Wordprocessing.TableRow row = table.Elements<Wordprocessing.TableRow>().ElementAt(i);

 Wordprocessing.TableCell cell1 = row.Elements<Wordprocessing.TableCell>().ElementAt(j);

 Wordprocessing.TableCell cell2 = row.Elements<Wordprocessing.TableCell>().ElementAt(j+1);

如何水平合并这些单元格?

您需要将 HorizontalMerge 对象附加到单元格的 TableProperties

TableCellProperties cellOneProperties = new TableCellProperties();
cellOneProperties.Append(new HorizontalMerge()
{
    Val = MergedCellValues.Restart
});

TableCellProperties cellTwoProperties = new TableCellProperties();
cellTwoProperties.Append(new HorizontalMerge()
{
    Val = MergedCellValues.Continue
});

cell1.Append(cellOneProperties);
cell2.Append(cellTwoProperties);