Create a table and set its Border in Doc Document

If you're working with a request for proposal, a standard operating procedure file, or another document type that dictates your format, a Word document may be page after page of just text. Your document would be cumbersome and unclear. So your document need a table to record your information which you need show. A table not only provides a visual grouping of information, but also makes writer more convenient to modify and query data in table. In particular when you have a table with colorful and diverse borders, your document would be stunning.

Spire.Doc for .NET, the professional .NET Word component to operate Word document, provides a Table class to perform tasks on table, for example, including rows, columns, cells, borders and table layout operation. This guide introduces a solution to create Word table with data and set its border in C#.NET via Spire.Doc for .NET. To finish this guide, you should Download and install Spire.Doc for .NET at first.

The following will show a specified example to create Word table and set its border step by step. The screenshot below presents result of this example.

Create a table and set its Border in Doc Document

We suppose that you have finished the tutorial Spire.Doc Quick Start before this example.

  • Firstly, create a new Document instance. Invoke AddTable method to create a table into the Section, passing a parameter value true to indicate the new table having border. Invoke ResetCells method to set the numbers of rows and columns of table.
  • Secondly, set the table's header and cells format.
  • Thirdly, set table's border this step, you can set the bottom, top, left, right border's format. For example, you can set the Color, the LineWidth, the border whether exist and BorderType which you have many types to choice such as Dot, Double, Hairline and so on.
  • Finally, you can set Vertical and Horizontal border such as step 3.

The full code:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace Table
Public class table
   static void Main(string[] args)
 //Open a blank word document as template
      Document document  = new Document(Blank.doc");
      //Save doc file.

      //Launching the MS Word file.
      System.Diagnostics.Process.Start ("Sample.doc");
   private void addTable(Section section)
//create a table with border
Spire.Doc.Table table = section.AddTable(true);
    String[] header = { "Name", "Capital", "Continent", "Area", "Population" };
    String[][] data =
        {   new String[]{"Argentina", "Buenos Aires", "South America", "2777815", "32300003"},
            new String[]{"Bolivia", "La Paz", "South America", "1098575", "7300000"},
            new String[]{"Brazil", "Brasilia", "South America", "8511196", "150400000"},
            new String[]{"Canada", "Ottawa", "North America", "9976147", "26500000"},
new String[]{"Paraguay", "Asuncion", "South America", "406576", "4660000"},
            new String[]{"Peru", "Lima", "South America", "1285215", "21600000"},
            new String[]{"United States of America", "Washington", "North America", "9363130", "249200000"},
    table.ResetCells(data.Length + 1, header.Length);

    // ***************** First Row *************************
     TableRow Frow = table.Rows[0];
     Frow.IsHeader = true;
     Frow.Height = 20;    //unit: point, 1point = 0.3528 mm
     Frow.HeightType = TableRowHeightType.Exactly;
     Frow.RowFormat.BackColor = Color.Pink;
     for (int i = 0; i < header.Length; i++)
       Frow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
       Paragraph p = Frow.Cells[i].AddParagraph(); 
p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Left;
        TextRange txtRange = p.AppendText(header[i]);
        txtRange.CharacterFormat.Bold = true;
   for (int r = 0; r < data.Length; r++)
       TableRow dataRow = table.Rows[r + 1];
       dataRow.Height = 20;
       dataRow.HeightType = TableRowHeightType.Exactly;
       dataRow.RowFormat.BackColor = Color.Empty;
       for (int c = 0; c < data[r].Length; c++)
          dataRow.Cells[c].CellFormat.VerticalAlignment =VerticalAlignment.Middle;

    //set right border of table
    table.TableFormat.Borders.Right.BorderType = Spire.Doc.Documents.BorderStyle.Hairline;
    table.TableFormat.Borders.Right. LineWidth = 2.0F;
table.TableFormat.Borders.Right.Color = Color.GreenYellow;
//set top border of table
    table.TableFormat.Borders.Top.BorderType = Spire.Doc.Documents.BorderStyle.Hairline;
    table.TableFormat.Borders.Top.LineWidth = 4.0F;
table.TableFormat.Borders.Top.Color = Color.GreenYellow;
//set left border of table
    table.TableFormat.Borders.Left.BorderType = Spire.Doc.Documents.BorderStyle.Hairline;
    table.TableFormat.Borders.Left.LineWidth = 2.0F;
table.TableFormat.Borders.Left.Color = Color.GreenYellow;
//set bottom border is none
    table.TableFormat.Borders.Bottom.BorderType = Spire.Doc.Documents.BorderStyle.None;
//set vertical and horizontal border 
    table.TableFormat.Borders.Vertical.BorderType = Spire.Doc.Documents.BorderStyle.Dot;
table.TableFormat.Borders.Horizontal.BorderType = Spire.Doc.Documents.BorderStyle.None;
table.TableFormat.Borders.Vertical.Color = Color.Orange;