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

The Table element can be created programmatically in various ways.

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 which has a tabular representation, for instance from grid controls.

The following example shows how you can easily create a table from a DataTable object (or you could import the DataTable by using Merge Ranges).

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

class Program
{
    static void Main()
    {
        // If using 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 System.Data
Imports System.Linq
Imports GemBox.Document
Imports GemBox.Document.Tables

Module Program

    Sub Main()

        ' If using 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

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Document yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.