C#/VB.NET-Insert Table in Word and Fill Table with Data

In order to show data information more clearly, users often insert table in Word document. When writing an article to express opinions, we may need to display some data information to convince readers to agree with us. And comparing with text, readers feel better with a data table. In this post, I want to introduce a method about how to insert table in Word by using C#/VB.NET.

In my example, I will insert a table with data information in a blank document and set format for it. Also, Spire.Doc for .NET is used for realizing this function.

Detailed Step Shown as Following:            

  1. Create a new document and add section.
  2. Set data information which will fill data, separating with header and body data.
  3. Add table in section and reset cells to make row number as data.Lenght+1 and column number as header.Length.
  4. Set header format. Firstly, set row height and back color. Secondly, set cell vertical alignment and width. Thirdly, insert header string in header row. Fourthly, set character format for cells.
  5. Set body data format as step 4.
  6. Save and launch file.

Main Coding:

C#

using System;
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace InsertTable
{
    class Table
    {
        static void Main(string[] args)
        {
            //Create Document and Add Section
            Document document = new Document();
            Section section = document.AddSection();

            //Table Header and Data
            String[] header = { “Product No”, “Quantity”, “Unit Price”, “Total Sales” };
            String[][] data =
                {
                    new String[]{“KBS78001″, “200”, “$300.00″, “60000”},
                    new String[]{“KBS78002″, “280”, “$270.00″, “75600”},
                    new String[]{“KBS78003″, “410”, “$290.00″, “118900”},
                    new String[]{“KBS78004″, “350”, “$330.00″, “115500”},
                    new String[]{“KBS78005″, “360”, “$410.00″, “147600”},
                    new String[]{“KBS78006″, “440”, “$260.00″, “114400”},
                    new String[]{“KBS78007″, “330”, “$400.00″, “212000”},
                };

            //Insert Table
            Spire.Doc.Table table = section.AddTable();
            table.ResetCells(data.Length + 1, header.Length);

            //Header Format
            TableRow row = table.Rows[0];
            row.IsHeader = true;
            row.Height = 20;   
            row.HeightType = TableRowHeightType.Exactly;
            row.RowFormat.BackColor = Color.LimeGreen;
            for (int i = 0; i < header.Length; i++)
            {
                row.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
                row.Cells[i].Width = 90;
                Paragraph p = row.Cells[i].AddParagraph();
                p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
                TextRange txtRange = p.AppendText(header[i]);
                txtRange.CharacterFormat.Font = new Font(“Calibri”, 12f);
                txtRange.CharacterFormat.TextColor = Color.GhostWhite;
                txtRange.CharacterFormat.Bold = true;
            }
            for (int r = 0; r < data.Length; r++)
            {

                //Data Format
                TableRow dataRow = table.Rows[r + 1];
                dataRow.Height = 20;
                dataRow.HeightType = TableRowHeightType.Exactly;
                dataRow.RowFormat.BackColor = Color.GhostWhite;
                for (int c = 0; c < data[r].Length; c++)
                {
                    dataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
                    dataRow.Cells[c].Width = 90;
                    Paragraph p1= dataRow.Cells[c].AddParagraph();
                    TextRange txtRange2 = p1.AppendText(data[r][c]);
                    p1.Format.HorizontalAlignment = HorizontalAlignment.Center;
                    txtRange2.CharacterFormat.Font = new Font(“Calibri”, 12f);
                    txtRange2.CharacterFormat.TextColor = Color.DarkBlue; 
                }
            }

            //Save and Launch
            document.SaveToFile(“Table.docx”, FileFormat.Docx);
            System.Diagnostics.Process.Start(“Table.docx”);
        }
    }
}

VB.NET

Imports System
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields

Namespace InsertTable
    Friend Class Table
        Shared Sub Main(ByVal args() As String)
            ‘Create Document and Add Section
            Dim document As New Document()
            Dim section As Section = document.AddSection()

            ‘Table Header and Data
            Dim header() As String = {“Product No”, “Quantity”, “Unit Price”, “Total Sales”}
            Dim data()() As String = {New String() {“KBS78001″, “200”, “$300.00″, “60000”}, New String() {“KBS78002″, “280”, “$270.00″, “75600”}, New String() {“KBS78003″, “410”, “$290.00″, “118900”}, New String() {“KBS78004″, “350”, “$330.00″, “115500”}, New String() {“KBS78005″, “360”, “$410.00″, “147600”}, New String() {“KBS78006″, “440”, “$260.00″, “114400”}, New String() {“KBS78007″, “330”, “$400.00″, “212000”}}

            ‘Insert Table
            Dim table As Spire.Doc.Table = section.AddTable()
            table.ResetCells(data.Length + 1, header.Length)

            ‘Header Format
            Dim row As TableRow = table.Rows(0)
            row.IsHeader = True
            row.Height = 20
            row.HeightType = TableRowHeightType.Exactly
            row.RowFormat.BackColor = Color.LimeGreen
            For i As Integer = 0 To header.Length – 1
                row.Cells(i).CellFormat.VerticalAlignment = VerticalAlignment.Middle
                row.Cells(i).Width = 90
                Dim p As Paragraph = row.Cells(i).AddParagraph()
                p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center
                Dim txtRange As TextRange = p.AppendText(header(i))
                txtRange.CharacterFormat.Font = New Font(“Calibri”, 12.0F)
                txtRange.CharacterFormat.TextColor = Color.GhostWhite
                txtRange.CharacterFormat.Bold = True
            Next i
            For r As Integer = 0 To data.Length – 1

                ‘Data Format
                Dim dataRow As TableRow = table.Rows(r + 1)
                dataRow.Height = 20
                dataRow.HeightType = TableRowHeightType.Exactly
                dataRow.RowFormat.BackColor = Color.GhostWhite
                For c As Integer = 0 To data(r).Length – 1
                    dataRow.Cells(c).CellFormat.VerticalAlignment = VerticalAlignment.Middle
                    dataRow.Cells(c).Width = 90
                    Dim p1 As Paragraph = dataRow.Cells(c).AddParagraph()
                    Dim txtRange2 As TextRange = p1.AppendText(data(r)(c))
                    p1.Format.HorizontalAlignment = HorizontalAlignment.Center
                    txtRange2.CharacterFormat.Font = New Font(“Calibri”, 12.0F)
                    txtRange2.CharacterFormat.TextColor = Color.DarkBlue
                Next c
            Next r

            ‘Save and Launch
            document.SaveToFile(“Table.docx”, FileFormat.Docx)
            System.Diagnostics.Process.Start(“Table.docx”)
        End Sub
    End Class
End Namespace

Result Shown as Following:

Download .NET Word Component

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s