使用 DocumentFormat.OpenXml.Wordprocessing 时 TableStyle 不工作

TableStyle not working when using DocumentFormat.OpenXml.Wordprocessing

我正在尝试使用 OpenXML 将 table 添加到 Word 文档。我找到了一些例子,它们似乎工作得很好,除了 TableStyle。我试过 Appending,Appending as a Child(不确定何时使用什么,但都尝试过)但无论我做什么 - 样式都不会应用。正在应用宽度。

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
public static void InsertTableInDoc(string filepath)
{
    // Open a WordprocessingDocument for editing using the filepath.
    using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(filepath, true))
    {
        // Assign a reference to the existing document body.
        Body body = wordprocessingDocument.MainDocumentPart.Document.Body;

        // Create a table.
        Table tbl = new Table();

        // Set the style and width for the table.
        TableProperties tableProp = new TableProperties();
        TableStyle tableStyle = new TableStyle() { Val = "TableGrid" };

        // Make the table width 100% of the page width.
        TableWidth tableWidth = new TableWidth() { Width = "5000", Type = TableWidthUnitValues.Pct };

        // Apply
        tableProp.Append(tableStyle, tableWidth);

        // Add 3 columns to the table.
        TableGrid tg = new TableGrid(new GridColumn(), new GridColumn(), new GridColumn());

        tbl.AppendChild(tg);

        // Create 1 row to the table.
        TableRow tr1 = new TableRow();

        // Add a cell to each column in the row.
        TableCell tc1 = new TableCell(new Paragraph(new Run(new Text("1"))));
        TableCell tc2 = new TableCell(new Paragraph(new Run(new Text("2"))));
        TableCell tc3 = new TableCell(new Paragraph(new Run(new Text("3"))));
        tr1.Append(tc1, tc2, tc3);

        // Add row to the table.
        tbl.AppendChild(tr1);

        // Add the table to the document
        body.AppendChild(tbl);
    }
}
public static void CreateWordprocessingDocument(string fileName)
{

    string[,] data = {
        {"Texas", "TX"},
        {"California", "CA"},
        {"New York", "NY"},
        {"Massachusetts", "MA"}
    };

    using (var wordDocument = WordprocessingDocument.Open(fileName, true))
    {

        // We need to change the file type from template to document.
        wordDocument.ChangeDocumentType(WordprocessingDocumentType.Document);

        var body = wordDocument.MainDocumentPart.Document;

        Table table = new Table();

        TableProperties props = new TableProperties();
        TableStyle tableStyle = new TableStyle { Val = "LightShading-Accent1" };
        props.Append(tableStyle);
        table.AppendChild(props);

        for (var i = 0; i <= data.GetUpperBound(0); i++)
        {
            var tr = new TableRow();
            for (var j = 0; j <= data.GetUpperBound(1); j++)
            {
                var tc = new TableCell();
                tc.Append(new Paragraph(new Run(new Text(data[i, j]))));
                tc.Append(new TableCellProperties(new TableCellWidth { Type = TableWidthUnitValues.Auto }));
                tr.Append(tc);
            }
            table.Append(tr);
        }
        body.Append(table);
        wordDocument.MainDocumentPart.Document.Save();
    }
}

我试过将 TableProperties 与 TableBorders example and that seems to work fine, I've also tried playing with TableLook using this example 一起使用,但 TableStyle 还是没有得到应用。我遗漏了一些无法正常工作的 TableStyles。

经过一些研究,当您创建 OpenXML 文档时,它似乎基本上是空的。没有样式,没有 table 样式,什么都没有,Word 只是没有神奇地让它工作。您要使用的每种样式都需要在文档中定义,这意味着您需要将 table 添加到具有正确样式的文档中才能显示出来。因此,在使用 OpenXML SDK 2.5 Productivity Tool 之后,我能够从 tables.

新创建的文档中提取 tables 的样式

当您使用 OpenXML SDK 2.5 生产力工具时,您会获得代码反射,因此您可以让它为您自动生成代码。

所以我只是将样式复制到文档中,完成后引用 Table 中的 table 样式本身应该可以工作!

private void GenerateStyleDefinitionsPart1Content(StyleDefinitionsPart styleDefinitionsPart1)
... TOO LONG TO add it all

我花了一些时间才理解它,但是有了 OpenXML SDK 2.5 生产力工具,它就容易多了 - 强烈推荐。