在使用 openXML 生成 word 文档时尝试合并 table 个单元格

trying to merge table cells while generating a word document using openXML

我正在尝试生成一个带有 table 的 word 文档,如下面的代码将合并第四列的单元格以生成 table.

当我如图所示合并单元格时,不知何故添加了一个额外的 space。

var rowHeaderLabels = new List<string> { "Space", "Lighting Density" };                       
var columnHeaderLabels = new List<string> { "Occupants per 1000sf", "Sensible BTUH", "Latent BTUH" };
var assumedLoadsTable = RenderTableWithMergedHeader(rowHeaderLabels, "Equipment Density", "Occupant", columnHeaderLabels, assumedHeatingAndCoolingData);

这是RenderTableWithMergedHeader方法的代码

 private static Table RenderTableWithMergedHeader(List<string> rowsHeaderLabel,string lastColumnName,string mergedColumnLabel, List<string> columnsHeaderLabel, List<List<string>> contentData)
    {
        var table = new Table();
        var tableProperties = RenderTableProperties();
        table.AppendChild(tableProperties);

        TableRow tableHeaderRowA = new TableRow();
        TableRow tableHeaderRowB = new TableRow();

        foreach (var colName in rowsHeaderLabel)
        {
            tableHeaderRowA.Append(RenderTableHeaderCell(colName));
        }
        table.Append(tableHeaderRowA);
        // problem is here with tableHeaderRowA
        tableHeaderRowA.Append(RenderTableHeaderCell(lastColumnName, 1, MergedCellValues.Restart));
        tableHeaderRowB.Append(RenderTableHeaderCell(null, 3, MergedCellValues.Continue));

        tableHeaderRowA.Append(RenderTableHeaderCell(mergedColumnLabel,columnsHeaderLabel.Count));
        foreach (var colName in columnsHeaderLabel)
        {
            tableHeaderRowB.Append(RenderTableHeaderCell(colName));
        }
        table.Append(tableHeaderRowB);
        // Content binding code
        return table;
    }

这是RenderTableHeaderCell方法的代码

    public static TableCell RenderTableHeaderCell(string columnName, int horizontalSpan = 1, MergedCellValues? verticalMerge = null)
    {
        TableCell tableCell = new TableCell();
        TableCellVerticalAlignment tableCellVerticalAlignment = new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center };
        Paragraph paragraph = new Paragraph();
        TableCellProperties tableCellProperties = RenderTablecellProperties();
        tableCellProperties.Append(tableCellVerticalAlignment);
        if (horizontalSpan != 1)
        {
            tableCellProperties.Append(new GridSpan() { Val = horizontalSpan });
        }
        if (verticalMerge != null)
        {
            VerticalMerge vm = new VerticalMerge() { Val = verticalMerge };
            tableCellProperties.Append(vm);
        }
        var paragraphProperties = RenderParagraphJustification();
        paragraph.Append(paragraphProperties);
        Run newChild = RenderRun(columnName);
        paragraph.AppendChild(newChild);
        tableCell.Append(tableCellProperties);
        tableCell.Append(paragraph);

        return tableCell;
    }

这是生成 table 如下图

我正在寻找下图中的table

我不确定我在上面的代码哪里做错了,任何人都可以提出任何关于如何删除第一张图片中显示的 space 的想法。

非常感谢

我已经通过修改以下方法解决了我的问题

  private static Table RenderTableWithMergedHeader(List<string> rowsHeaderLabel, string mergedColumnName, List<string> columnsHeaderLabel, List<List<string>> contentData)
  {
        var table = new Table();
        var tableProperties = RenderTableProperties();
        table.AppendChild(tableProperties);

        TableRow tableHeaderRowA = new TableRow();
        TableRow tableHeaderRowB = new TableRow();

        foreach (var colName in rowsHeaderLabel)
        {
            tableHeaderRowA.Append(RenderTableHeaderCell(colName, 1, MergedCellValues.Restart));
            tableHeaderRowB.Append(RenderTableHeaderCell(null, 1, MergedCellValues.Continue));
        }
        tableHeaderRowA.Append(RenderTableHeaderCell(mergedColumnName, columnsHeaderLabel.Count));

        foreach (var colName in columnsHeaderLabel)
        {
            tableHeaderRowB.Append(RenderTableHeaderCell(colName));
        }

        table.Append(tableHeaderRowA);
        table.Append(tableHeaderRowB);
        foreach (var rowData in contentData)
        {
            var row = new TableRow();
            foreach (var cellData in rowData)
            {
                row.Append(RenderContentCell(cellData));
            }
            table.Append(row);
        }

        return table;
  }