Table 打开的单元格边距 XML

Table cell margin with Open XML

我想在 table 级别定义单元格边距,就像这里一样:OOXML

使用以下代码:

var mainDocumentPart = wordDocument.AddMainDocumentPart();

mainDocumentPart.Document = new Document(
    new Body(
        new Paragraph(
            new Run(
                new Text("Create text in body - CreateWordprocessingDocument")))));

// Create an empty table.
Table table = new Table();

// Create a TableProperties object and specify its border information.
TableProperties tblProp = new TableProperties(
    new TableCellSpacing() { Width = "200", Type = TableWidthUnitValues.Dxa },
    new TableCellMargin(
        new TopMargin() { Width = "50", Type = TableWidthUnitValues.Dxa },
        new StartMargin() { Width = "200", Type = TableWidthUnitValues.Dxa },
        new BottomMargin() { Width = "0", Type = TableWidthUnitValues.Dxa },
        new EndMargin() { Width = "0", Type = TableWidthUnitValues.Dxa })
);

单元格间距有效,但单元格边距无效。我做错了什么?

使用 TableCellMarginDefault 代替 TableCellMargin.

  • TableCellMarginDefault 影响 table.
  • TableCellMargin 影响单个单元格。

您的新代码应如下所示:

var mainDocumentPart = wordDocument.AddMainDocumentPart();

mainDocumentPart.Document = new Document(
    new Body(
        new Paragraph(
            new Run(
                new Text("Create text in body - CreateWordprocessingDocument")))));

// Create an empty table.
Table table = new Table();

// Create a TableProperties object and specify its border information.
TableProperties tblProp = new TableProperties(
    new TableCellSpacing() { Width = "200", Type = TableWidthUnitValues.Dxa },
    new TableCellMarginDefault(
        new TopMargin() { Width = "50", Type = TableWidthUnitValues.Dxa },
        new StartMargin() { Width = "200", Type = TableWidthUnitValues.Dxa },
        new BottomMargin() { Width = "0", Type = TableWidthUnitValues.Dxa },
        new EndMargin() { Width = "0", Type = TableWidthUnitValues.Dxa })
        );