Insert DataTable to Word file in C# and VB.NET

GemBox.Document provides various ways to create a Table element in your Word documents programmatically.

You can create it by adding new rows to Table.Rows and adding new cells to TableRow.Cells, as shown in the Simple Table example.

Or you can create it by using a Table constructor that takes the CreateTableCell delegate. With this delegate, you can generate a table from any object with a tabular representation, for instance, from grid controls.

The following example shows how you can easily create a table from a DataTable object in C# and VB.NET.

Word document with Table element generated from DataTable object
Screenshot of Table created from DataTable
using GemBox.Document;
using GemBox.Document.Tables;
using System.Data;
using System.Linq;

class Program
{
    static void Main()
    {
        // If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        int rowCount = %RowCount%;
        int columnCount = %ColumnCount%;

        // Create DataTable with some sample data.
        var dataTable = new DataTable();
        for (int c = 0; c < columnCount; c++)
            dataTable.Columns.Add($"Column {c + 1}");
        for (int r = 0; r < rowCount; r++)
            dataTable.Rows.Add(Enumerable.Range(0, columnCount).Select(c => $"Cell ({r + 1},{c + 1})").ToArray());

        // Create new document.
        var document = new DocumentModel();

        // Create Table element from DataTable object.
        Table table = new Table(document, rowCount, columnCount,
            (int r, int c) => new TableCell(document, new Paragraph(document, dataTable.Rows[r][c].ToString())));

        // Insert first row as Table's header.
        table.Rows.Insert(0, new TableRow(document, dataTable.Columns.Cast<DataColumn>().Select(
            dataColumn => new TableCell(document, new Paragraph(document, dataColumn.ColumnName)))));

        table.TableFormat.PreferredWidth = new TableWidth(100, TableWidthUnit.Percentage);

        document.Sections.Add(new Section(document, table));

        document.Save("Insert DataTable.%OutputFileType%");
    }
}
Imports GemBox.Document
Imports GemBox.Document.Tables
Imports System.Data
Imports System.Linq

Module Program

    Sub Main()

        ' If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim rowCount As Integer = %RowCount%
        Dim columnCount As Integer = %ColumnCount%

        ' Create DataTable with some sample data.
        Dim dataTable As New DataTable()
        For c As Integer = 0 To columnCount - 1
            dataTable.Columns.Add($"Column {c + 1}")
        Next
        For i As Integer = 0 To rowCount - 1
            Dim r = i
            dataTable.Rows.Add(Enumerable.Range(0, columnCount).Select(Function(c) $"Cell ({r + 1},{c + 1})").ToArray())
        Next

        ' Create new document.
        Dim document As New DocumentModel()

        ' Create Table element from DataTable object.
        Dim table As New Table(document, rowCount, columnCount,
            Function(r, c) New TableCell(document, New Paragraph(document, dataTable.Rows(r)(c).ToString())))

        ' Insert first row as Table's header.
        table.Rows.Insert(0, New TableRow(document, dataTable.Columns.Cast(Of DataColumn)().Select(
            Function(dataColumn) New TableCell(document, New Paragraph(document, dataColumn.ColumnName)))))

        table.TableFormat.PreferredWidth = New TableWidth(100, TableWidthUnit.Percentage)

        document.Sections.Add(New Section(document, table))

        document.Save("Insert DataTable.%OutputFileType%")

    End Sub
End Module

See also


Next steps

GemBox.Document is a .NET component that enables you to read, write, edit, convert, and print document files from your .NET applications using one simple API. How about testing it today?

Download Buy