Search code examples
c#openxmlwordprocessingml

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


I'm using DocumentFormat.OpenXml.Wordprocessing to add a table in a Word document. What I need is to remove the border of the first 4(/6) cells in the last 3(/N) rows of the table. These rows are added like:

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

How do I set the TableCellBorders? I've tried a few things like:

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;

However, everything that I've tried returns System.NullReferenceException. What is the proper way of removing the cell borders?


Solution

  • You can create a table in word with no borders like this:

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

    Customize and optimize it, to your needs :)