我有一个任务需要合并超过 2 个单元格,使用下面的代码我只能合并 word 文档下表头中的 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);