在 Word 文档 (OpenXml.Wordprocessing) 中删除 table 中的单元格边框

Remove cell borders in table in Word document (OpenXml.Wordprocessing)

我正在使用 DocumentFormat.OpenXml.Wordprocessing 在 Word 文档中添加 table。我需要的是删除 table 的最后 3(/N) 行中前 4(/6) 个单元格的边框。这些行添加如下:

t.Append(new TableRow(
    new TableCell(new Paragraph(new Run(new Text()))),
    new TableCell(new Paragraph(new Run(new Text()))),
    new TableCell(new Paragraph(new Run(new Text()))),
    new TableCell(new Paragraph(new Run(new Text()))),
    new TableCell(new Paragraph(new Run(new Text("Total:")))),
    new TableCell(new Paragraph(new Run(new Text(priceTotal.ToString()))))
    ));

如何设置 TableCellBorders?我尝试了一些方法,例如:

TableCell cell = new TableCell();
cell.TableCellProperties.TableCellBorders.LeftBorder.Size.Value = 0;
cell.TableCellProperties.TableCellBorders.RightBorder.Size.Value = 0;
cell.TableCellProperties.TableCellBorders.TopBorder.Size.Value = 0;
cell.TableCellProperties.TableCellBorders.BottomBorder.Size.Value = 0;

然而,我已经尝试过的一切 returns System.NullReferenceException。去除单元格边框的正确方法是什么?

您可以像这样在 word 中创建一个无边框的 table:

public static void CreateTable(string fileName)
{
    // Use the file name and path passed in as an argument 
    // to open an existing Word 2007 document.

    using (WordprocessingDocument doc
        = WordprocessingDocument.Open(fileName, true))
    {
        // Create an empty table.
        Table table = new Table();

        // Create a TableProperties object and specify its border information.
        TableProperties tblProp = new TableProperties(
            new TableBorders(
                new TopBorder()
                {
                    Val =
                    new EnumValue<BorderValues>(BorderValues.None),
                },
                new BottomBorder()
                {
                    Val =
                        new EnumValue<BorderValues>(BorderValues.None),
                },
                new LeftBorder()
                {
                    Val =
                        new EnumValue<BorderValues>(BorderValues.None),
                },
                new RightBorder()
                {
                    Val =
                        new EnumValue<BorderValues>(BorderValues.None),
                },
                new InsideHorizontalBorder()
                {
                    Val =
                        new EnumValue<BorderValues>(BorderValues.None),
                },
                new InsideVerticalBorder()
                {
                    Val =
                        new EnumValue<BorderValues>(BorderValues.None),
                }
            )
        );

        // Append the TableProperties object to the empty table.
        table.AppendChild<TableProperties>(tblProp);

        // Create a row.
        TableRow tr = new TableRow();

        // Create a cell.
        TableCell tc1 = new TableCell();

        // Specify the width property of the table cell.
        tc1.Append(new TableCellProperties(
            new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));

        // Specify the table cell content.
        tc1.Append(new Paragraph(new Run(new Text("some text"))));

        // Append the table cell to the table row.
        tr.Append(tc1);

        // Create a second table cell by copying the OuterXml value of the first table cell.
        TableCell tc2 = new TableCell(tc1.OuterXml);

        // Append the table cell to the table row.
        tr.Append(tc2);

        // Append the table row to the table.
        table.Append(tr);

        // Append the table to the document.
        doc.MainDocumentPart.Document.Body.Append(table);
    }
}

根据您的需要自定义和优化它:)