【问题标题】:Remove cell borders in table in Word document (OpenXml.Wordprocessing)删除 Word 文档中表格中的单元格边框 (OpenXml.Wordprocessing)
【发布时间】:2019-04-10 19:35:01
【问题描述】:

我正在使用DocumentFormat.OpenXml.Wordprocessing 在 Word 文档中添加表格。我需要的是删除表格最后 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;

但是,我尝试过的所有操作都返回 System.NullReferenceException。删除单元格边框的正确方法是什么?

【问题讨论】:

  • 可能是因为边框是在您的样式表下定义的..
  • 有两种方法可以在 Word 文档中定义表格边框。一种方法是在 Table 元素中定义边框,另一种方法是在 TableCell 元素中定义边框。
  • 我想为每个 TableCell 定义它们,因为它们彼此不同。我怎样才能做到这一点?

标签: c# openxml wordprocessingml


【解决方案1】:

您可以像这样在word中创建一个没有边框的表格:

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);
    }
}

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

【讨论】:

  • 实际上,问题是我必须使用Nil 而不是Null。我知道为什么,但这就是解决我的问题的原因。但是,我会标记您的答案,因为它确实帮助了我并将我推向了正确的方向!谢谢! new RightBorder() { Val = new EnumValue&lt;BorderValues&gt;(BorderValues.Nil), }