You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Open XML SDK创建不等宽单元格表格在Word中失效的问题

Open XML SDK创建表格在Word中不遵循预设宽度的问题

我用Open XML SDK创建表格时遇到了问题:需要创建的表格第一行包含16个不同宽度占比的单元格,第二行有8个不同宽度占比的单元格,第三行的单元格数量和宽度也各不相同。这个表格在LibreOffice中显示符合预期,但在Word里完全不遵循预设的尺寸。

LibreOffice中的表格显示效果:
LibreOffice中的表格显示效果

Word中的表格显示效果:
Word中的表格显示效果

我希望表格在Word中的显示效果和LibreOffice一致,但始终无法实现。我编写了以下代码结构来便捷填充不同文本:

public Table TemplateTable()
{
    Table table = new Table();
    // set the table width
    TableProperties tableProperties = table.GetFirstChild<TableProperties>();
    if (tableProperties == null)
    {
        tableProperties = new TableProperties();
        table.InsertAt(tableProperties, 0);
    }

    TableWidth tableWidth = new TableWidth() { Width = "7.88in", Type = TableWidthUnitValues.Dxa };
    tableProperties.TableWidth = tableWidth;

    // set the left spacing
    TableIndentation tableIndentation = tableProperties.GetFirstChild<TableIndentation>();
    if (tableIndentation == null)
    {
        tableIndentation = new TableIndentation() { Type = TableWidthUnitValues.Dxa };
        tableProperties.InsertBefore(tableIndentation, tableProperties.TableWidth);
    }

    string strValue = "-72";
    Int32Value intValue = new Int32Value();
    intValue = Int32Value.FromInt32(int.Parse(strValue));
    tableIndentation.Width = intValue; // -0.05 inches in twentieths of a point (Dxa)
    return table;
}



public TableCell TemplateTableCell(
        string text,
        string width,
        JustificationValues justificationValues = JustificationValues.Center,
        string fontSize = "12",
        string colorHex = "#000000",
        string borderColorHex = "#000000"
        )
{
    EndMargin EndMargin = new EndMargin() { Width = "200" };
    TableCellMargin margin = new TableCellMargin(
        new LeftMargin() { Width = "0.05%", Type = TableWidthUnitValues.Dxa },
        new RightMargin() { Width = "0.05%", Type = TableWidthUnitValues.Dxa }
    );
    var tableCellBorders = new TableCellBorders();
    var topBorder = new TopBorder { Val = BorderValues.Single, Size = 6, Color = borderColorHex };
    var leftBorder = new LeftBorder { Val = BorderValues.Single, Size = 6, Color = borderColorHex };
    var bottomBorder = new BottomBorder { Val = BorderValues.Single, Size = 6, Color = borderColorHex };
    var rightBorder = new RightBorder { Val = BorderValues.Single, Size = 6, Color = borderColorHex };
    var insideHorizontalBorder = new InsideHorizontalBorder { Val = BorderValues.Single, Size = 6, Color = borderColorHex };
    var insideVerticalBorder = new InsideVerticalBorder { Val = BorderValues.Single, Size = 6, Color = borderColorHex };

    tableCellBorders.AppendChild(topBorder);
    tableCellBorders.AppendChild(leftBorder);
    tableCellBorders.AppendChild(bottomBorder);
    tableCellBorders.AppendChild(rightBorder);
    tableCellBorders.AppendChild(insideHorizontalBorder);
    tableCellBorders.AppendChild(insideVerticalBorder);

    TableCellProperties cellProperties = new TableCellProperties(
        new TableCellWidth { Width = width, Type = TableWidthUnitValues.Dxa },
        new NoWrap { Val = OnOffOnlyValues.Off },
        new SpacingBetweenLines { LineRule = LineSpacingRuleValues.Exact, Before = "10", After = "10" },
        margin,
        tableCellBorders
    );

    Paragraph paragraph = new Paragraph(new Run(new Text(text == "" ? " " : text))
    {
        RunProperties = new RunProperties(new FontSize { Val = fontSize }, new RunFonts { Ascii = "Calibri", HighAnsi = "Calibri" }, new Color() { Val = colorHex }),
    });
    //cellProperties.Append(new Color() { Val = colorHex });
    paragraph.ParagraphProperties = new ParagraphProperties(new Justification { Val = justificationValues });

    TableCell tableCell = new TableCell(cellProperties);

    tableCell.AppendChild(paragraph);


    return tableCell;
}

我开始怀疑Word中根本无法实现这种效果,请问有人成功做到过吗?如果有,能否告诉我失败的原因以及解决方案?非常感谢!

内容的提问来源于stack exchange,提问作者Agostinho Tinho

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 15:54:55