Description:

In this blog you will see how to add borders to the table using Open XML SDK 2.0

Assembly:

DocumentFormat.OpenXml.dll

Namesapces:

using DocumentFormat.OpenXml;

using DocumentFormat.OpenXml.Packaging;

using DocumentFormat.OpenXml.Wordprocessing;

Code:


C#
 using System;using System.Collections.Generic;using System.Linq;using System.Text;using DocumentFormat.OpenXml;using DocumentFormat.OpenXml.Packaging;using DocumentFormat.OpenXml.Wordprocessing;using System.IO.Packaging; namespace OpenXMLConsole{    class Program    {        static void Main(string[] args)        {                     string path = @"E:\OpenXMLTable.docx";                       //// Creates the new instance of the WordprocessingDocument class from the specified file            //// WordprocessingDocument.Open(String, Boolean) method            //// Parameters:            //// string path - path is a string which contains the path of the document            //// bool isEditable            using (WordprocessingDocument doc = WordprocessingDocument.Create(path, WordprocessingDocumentType.Document))             {                //// Defines the MainDocumentPart                            MainDocumentPart mainDocumentPart = doc.AddMainDocumentPart();                mainDocumentPart.Document = new Document();                Body body = mainDocumentPart.Document.AppendChild(new Body());                 //// Create a new table                Table tbl = new Table();                  //// Create a new row                TableRow tr = new TableRow();                 //// Create the table properties                TableProperties tblProperties = new TableProperties();                 //// Create Table Borders                TableBorders tblBorders = new TableBorders();                                TopBorder topBorder = new TopBorder();                topBorder.Val = new EnumValue<BorderValues>(BorderValues.Thick);                topBorder.Color = "CC0000";                tblBorders.AppendChild(topBorder);                 BottomBorder bottomBorder = new BottomBorder();                bottomBorder.Val = new EnumValue<BorderValues>(BorderValues.Thick);                bottomBorder.Color = "CC0000";                tblBorders.AppendChild(bottomBorder);                 RightBorder rightBorder = new RightBorder();                rightBorder.Val = new EnumValue<BorderValues>(BorderValues.Thick);                rightBorder.Color = "CC0000";                tblBorders.AppendChild(rightBorder);                 LeftBorder leftBorder = new LeftBorder();                leftBorder.Val = new EnumValue<BorderValues>(BorderValues.Thick);                leftBorder.Color = "CC0000";                tblBorders.AppendChild(leftBorder);                 InsideHorizontalBorder insideHBorder = new InsideHorizontalBorder();                insideHBorder.Val = new EnumValue<BorderValues>(BorderValues.Thick);                insideHBorder.Color = "CC0000";                tblBorders.AppendChild(insideHBorder);                 InsideVerticalBorder insideVBorder = new InsideVerticalBorder();                insideVBorder.Val = new EnumValue<BorderValues>(BorderValues.Thick);                insideVBorder.Color = "CC0000";                tblBorders.AppendChild(insideVBorder);                 //// Add the table borders to the properties                tblProperties.AppendChild(tblBorders);                 //// Add the table properties to the table                tbl.AppendChild(tblProperties);                 //// Add a cell to each column in the row                TableCell tcName1 = new TableCell(new Paragraph(new Run(new Text("Vijai"))));                TableCell tcId1 = new TableCell(new Paragraph(new Run(new Text("1"))));                 //// Add the cells to the row                tr.Append(tcName1, tcId1);                 // Create a new row                TableRow tr1 = new TableRow();                TableCell tcName2= new TableCell(new Paragraph(new Run(new Text("Anand"))));                TableCell tcId2 = new TableCell(new Paragraph(new Run(new Text("2"))));                 //// Add the cells to the row                tr1.Append(tcName2, tcId2);                 //// Add the rows to the table                tbl.AppendChild(tr);                tbl.AppendChild(tr1);                 //// Add the table to the body                body.AppendChild(tbl);                mainDocumentPart.Document.Save();            }        }    }}



XML:


HTML / XML
 <w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">  <w:body>    <w:tbl>      <w:tblPr>        <w:tblBorders>          <w:top w:val="thick" w:color="CC0000" />          <w:bottom w:val="thick" w:color="CC0000" />          <w:right w:val="thick" w:color="CC0000" />          <w:left w:val="thick" w:color="CC0000" />          <w:insideH w:val="thick" w:color="CC0000" />          <w:insideV w:val="thick" w:color="CC0000" />        </w:tblBorders>      </w:tblPr>      <w:tr>        <w:tc>          <w:p>            <w:r>              <w:t>Vijai</w:t>            </w:r>          </w:p>        </w:tc>        <w:tc>          <w:p>            <w:r>              <w:t>1</w:t>            </w:r>          </w:p>        </w:tc>      </w:tr>      <w:tr>        <w:tc>          <w:p>            <w:r>              <w:t>Anand</w:t>            </w:r>          </w:p>        </w:tc>        <w:tc>          <w:p>            <w:r>              <w:t>2</w:t>            </w:r>          </w:p>        </w:tc>      </w:tr>    </w:tbl>  </w:body></w:document>