Create Word Table in C#, VB.NET

Word Table can save and display data to enable readers to get information more clearly. Users can insert table in Word document with specified rows, columns and format cells to have a good appearance. This guide introduces a solution to create Word table with data and format cells in C# and VB.NET via Spire.Doc for .NET. The screenshot below presents result after creating and formatting Word table.

Create Word Table

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 and table layout operation. The following will show a specified example to create Word table step by step.

Download and install Spire.Doc for .NET at the beginning and follow steps.

  • Firstly, create a new Document class instance and add a new Section in this instance. Invoke Section.AddTable(bool ShowBorder) method to create a table with or without borders.
  • Secondly, initialize two String array instances, Header and data to fill table soon.
  • Thirdly, Invoke Table.ResetCells(int rowsNum, int columnsNum) method to reset rows and columns numbers.
  • Fourthly, fill and format data in table. Create header row at first. Initialize a TableRow instance FRow and set its IsHeader property as true, Height property as a float value and BackColor of RowFormat property as a specified color.
  • Use for loop to fill Header in this TableRow instance and format each cell. Set VerticalAlignment of CellFormat property of FRow.
  • Then, initialize a Paragraph instance p for Cells of FRow. Set HorizontalAlignment of Format property of p.
  • Next, Initialize a TextRange instance TR which stores String array Header.
  • Set CharacterFormat property, including FontName, FontSize, TextColor and Bold for TR.
  • Create data row now. Initialize another TableRow instance DataRow and set Height property.
  • Then, fill this instance with String array data and format cells as operating on FRow.
using System;
using System.Data;
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace WordTable
    class Program
        static void Main(string[] args)
            //Create Table
            Document doc = new Document();
            Section s = doc.AddSection();
            Table table =s.AddTable(true);

            //Create Header and Data
            String[] Header ={"Item","Description", "Qty","Unit Price","Price"};  
            String[][] data = {
                                  new String[]{ "Spire.Doc for .NET",".NET Word Component","1","$799.00","$799.00"},
                                  new String[]{"Spire.XLS for .NET",".NET Excel Component","2","$799.00","$1,598.00"},
                                  new String[]{"Spire.Office for .NET",".NET Office Component","1","$1,899.00","$1,899.00"},
                                  new String[]{"Spire.PDF for .NET",".NET PDFComponent","2","$599.00","$1,198.00"},
            //Add Cells
            table.ResetCells(data.Length + 1, Header.Length);

            //Header Row
            TableRow FRow= table.Rows[0];
            FRow.IsHeader = true;
            //Row Height
            FRow.Height = 23;
            //Header Format
            FRow.RowFormat.BackColor = Color.AliceBlue;
            for (int i = 0; i < Header.Length; i++)
                //Cell Alignment
                Paragraph p = FRow.Cells[i].AddParagraph();
                FRow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
                p.Format.HorizontalAlignment = HorizontalAlignment.Center;
                //Data Format
                TextRange TR = p.AppendText(Header[i]);
                TR.CharacterFormat.FontName = "Calibri";
                TR.CharacterFormat.FontSize = 14;
                TR.CharacterFormat.TextColor = Color.Teal;
                TR.CharacterFormat.Bold = true;

            //Data Row
            for (int r = 0; r < data.Length; r++)
                TableRow DataRow = table.Rows[r + 1];

                //Row Height
                DataRow.Height = 20;

                //C Represents Column.
                for (int c = 0; c < data[r].Length; c++)
                    //Cell Alignment
                    DataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
                    //Fill Data in Rows
                    Paragraph p2 =DataRow.Cells[c].AddParagraph();
                    TextRange TR2=p2.AppendText(data[r][c]);
                    //Format Cells
                    p2.Format.HorizontalAlignment = HorizontalAlignment.Center;
                    TR2.CharacterFormat.FontName = "Calibri";
                    TR2.CharacterFormat.FontSize = 12;
                    TR2.CharacterFormat.TextColor = Color.Brown;

            //Save and Launch
Imports System
Imports System.Data
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields

Namespace WordTable
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            'Create Table
            Dim doc As New Document()
            Dim s As Section = doc.AddSection()
            Dim table As Table = s.AddTable(True)

            'Create Header and Data
            Dim Header() As String = {"Item", "Description", "Qty", "Unit Price", "Price"}
            Dim data()() As String = {New String() {"Spire.Doc for .NET", ".NET Word Component", "1", "$799.00", "$799.00"},
                                      New String() {"Spire.XLS for .NET", ".NET Excel Component", "2", "$799.00", "$1,598.00"},
                                      New String() {"Spire.Office for .NET", ".NET Office Component", "1", "$1,899.00", "$1,899.00"},
                                      New String() {"Spire.PDF for .NET", ".NET PDFComponent", "2", "$599.00", "$1,198.00"}}
            'Add Cells
            table.ResetCells(data.Length + 1, Header.Length)

            'Header Row
            Dim FRow As TableRow = table.Rows(0)
            FRow.IsHeader = True
            'Row Height
            FRow.Height = 23
            'Header Format
            FRow.RowFormat.BackColor = Color.AliceBlue
            For i As Integer = 0 To Header.Length - 1
                'Cell Alignment
                Dim p As Paragraph = FRow.Cells(i).AddParagraph()
                FRow.Cells(i).CellFormat.VerticalAlignment = VerticalAlignment.Middle
                p.Format.HorizontalAlignment = HorizontalAlignment.Center
                'Data Format
                Dim TR As TextRange = p.AppendText(Header(i))
                TR.CharacterFormat.FontName = "Calibri"
                TR.CharacterFormat.FontSize = 14
                TR.CharacterFormat.TextColor = Color.Teal
                TR.CharacterFormat.Bold = True
            Next i

            'Data Row
            For r As Integer = 0 To data.Length - 1
                Dim DataRow As TableRow = table.Rows(r + 1)

                'Row Height
                DataRow.Height = 20

                'C Represents Column.
                For c As Integer = 0 To data(r).Length - 1
                    'Cell Alignment
                    DataRow.Cells(c).CellFormat.VerticalAlignment = VerticalAlignment.Middle
                    'Fill Data in Rows
                    Dim p2 As Paragraph = DataRow.Cells(c).AddParagraph()
                    Dim TR2 As TextRange = p2.AppendText(data(r)(c))
                    'Format Cells
                    p2.Format.HorizontalAlignment = HorizontalAlignment.Center
                    TR2.CharacterFormat.FontName = "Calibri"
                    TR2.CharacterFormat.FontSize = 12
                    TR2.CharacterFormat.TextColor = Color.Brown
                Next c
            Next r

            'Save and Launch
        End Sub
    End Class
End Namespace

Spire.Doc, an easy-to-use component to operate Word document, allows developers to fast generate, write, edit and save Word (Word 97-2003, Word 2007, Word 2010) in C# and VB.NET for .NET, Silverlight and WPF.