Insert DataTable to Word in C# and VB.NET

Table can be constructed programmatically in a variety of ways with GemBox.Document.

Following sample shows how simple it is to construct a table from a DataTable.

For importing DataTable or any other data to a document, see Mail Merge examples.

Screenshot

Insert DataTable Screenshot

See the full code below, use RUN EXAMPLE to execute.


1using System;
2using System.Data;
3using GemBox.Document;
4using GemBox.Document.Tables;
5
6class Sample
7{
8    [STAThread]
9    static void Main(string[] args)
10    {
11        // If using Professional version, put your serial key below.
12        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
13
14        DocumentModel document = new DocumentModel();
15
16        int tableRowCount = 10;
17        int tableColumnCount = 5;
18
19        // Initialize DataTable
20        DataTable dt = new DataTable();
21
22        for (int i = 0; i < tableColumnCount; i++)
23            dt.Columns.Add();
24
25        for (int i = 0; i < tableRowCount; i++)
26        {
27            DataRow row = dt.NewRow();
28            for (int j = 0; j < tableColumnCount; j++)
29            {
30                row[j] = string.Format("Cell {0}-{1}", i + 1, j + 1);
31            }
32            dt.Rows.Add(row);
33        }
34
35        // Initialize Table
36        Table table = new Table(document, tableRowCount, tableColumnCount,
37            (int rowIndex, int columnIndex) => new TableCell(document, new Paragraph(document, dt.Rows[rowIndex][columnIndex].ToString())));
38
39        table.TableFormat.PreferredWidth = new TableWidth(100, TableWidthUnit.Percentage);
40
41        document.Sections.Add(new Section(document, table));
42
43        document.Save("Insert DataTable.docx");
44    }
45}
1Imports System
2Imports System.Data
3Imports GemBox.Document
4Imports GemBox.Document.Tables
5
6Module Samples
7
8    Sub Main()
9
10        ' If using Professional version, put your serial key below.
11        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
12
13        Dim document As DocumentModel = New DocumentModel
14
15        Dim tableRowCount As Integer = 10
16        Dim tableColumnCount As Integer = 5
17
18        ' Initialize DataTable
19        Dim dt As New DataTable()
20
21        For i As Integer = 0 To tableColumnCount - 1
22            dt.Columns.Add()
23        Next
24
25        For i As Integer = 0 To tableRowCount - 1
26            Dim row As DataRow = dt.NewRow()
27            For j As Integer = 0 To tableColumnCount - 1
28                row(j) = String.Format("Cell {0}-{1}", i + 1, j + 1)
29            Next
30            dt.Rows.Add(row)
31        Next
32
33        ' Initialize Table
34        Dim table As New Table(document, tableRowCount, tableColumnCount,
35                               Function(rowIndex As Integer, columnIndex As Integer) New TableCell(document, New Paragraph(document, dt.Rows(rowIndex)(columnIndex).ToString())))
36
37        table.TableFormat.PreferredWidth = New TableWidth(100, TableWidthUnit.Percentage)
38
39        document.Sections.Add(New Section(document, table))
40
41        document.Save("Insert DataTable.docx")
42
43    End Sub
44
45End Module

Check next sample or find out more about GemBox.Document and GemBox Software.