Export DataTable into Excel sheet in C# and VB.NET

The following example demonstrates how to export a DataTable object into an Excel file with C# and VB.NET at the specific cell range in the Excel worksheet.

Use InsertDataTableOptions to set various options for exporting data from DataTable to ExcelWorksheet. Set the ColumnHeaders property to true to insert DataTable column names above the data.

Export DataTable to Excel Screenshot
using System.Data;
using GemBox.Spreadsheet;

class Program
{
    static void Main(string[] args)
    {
        // If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        ExcelFile ef = new ExcelFile();
        ExcelWorksheet ws = ef.Worksheets.Add("DataTable to Sheet");

        DataTable dt = new DataTable();

        dt.Columns.Add("ID", typeof(int));
        dt.Columns.Add("FirstName", typeof(string));
        dt.Columns.Add("LastName", typeof(string));

        dt.Rows.Add(new object[] { 100, "John", "Doe" });
        dt.Rows.Add(new object[] { 101, "Fred", "Nurk" });
        dt.Rows.Add(new object[] { 103, "Hans", "Meier" });
        dt.Rows.Add(new object[] { 104, "Ivan", "Horvat" });
        dt.Rows.Add(new object[] { 105, "Jean", "Dupont" });
        dt.Rows.Add(new object[] { 106, "Mario", "Rossi" });

        ws.Cells[0, 0].Value = "DataTable insert example:";

        // Insert DataTable into an Excel worksheet.
        ws.InsertDataTable(dt,
            new InsertDataTableOptions()
            {
                ColumnHeaders = true,
                StartRow = 2
            });

        ef.Save("DataTable to Sheet.%OutputFileType%");
    }
}
Imports System.Data
Imports GemBox.Spreadsheet

Module Program

    Sub Main()

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

        Dim ef As ExcelFile = New ExcelFile
        Dim ws As ExcelWorksheet = ef.Worksheets.Add("DataTable to Sheet")

        Dim dt As DataTable = New DataTable

        dt.Columns.Add("ID", Type.GetType("System.Int32"))
        dt.Columns.Add("FirstName", Type.GetType("System.String"))
        dt.Columns.Add("LastName", Type.GetType("System.String"))

        dt.Rows.Add(New Object() {100, "John", "Doe"})
        dt.Rows.Add(New Object() {101, "Fred", "Nurk"})
        dt.Rows.Add(New Object() {103, "Hans", "Meier"})
        dt.Rows.Add(New Object() {104, "Ivan", "Horvat"})
        dt.Rows.Add(New Object() {105, "Jean", "Dupont"})
        dt.Rows.Add(New Object() {106, "Mario", "Rossi"})

        ws.Cells(0, 0).Value = "DataTable insert example:"

        ' Insert DataTable into an Excel worksheet.
        ws.InsertDataTable(dt,
            New InsertDataTableOptions() With
            {
                .ColumnHeaders = True,
                .StartRow = 2
            })

        ef.Save("DataTable to Sheet.%OutputFileType%")

    End Sub

End Module

Check next example or download examples from GitHub.