Удалить границы ячеек в таблице в документе Word (OpenXml.Wordprocessing) - PullRequest
0 голосов
/ 07 ноября 2018

Я использую DocumentFormat.OpenXml.Wordprocessing, чтобы добавить таблицу в документ Word. Что мне нужно, так это удалить границу первых 4 (/ 6) ячеек в последних 3 (/ N) строках таблицы. Эти строки добавляются как:

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. Как правильно удалить границы ячейки?

1 Ответ

0 голосов
/ 07 ноября 2018

Вы можете создать таблицу в слове без границ, как это:

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

Настройте и оптимизируйте его под свои нужды:)

...