GemBox.Spreadsheet is a .NET component that enables you to read, write, edit, convert, and print spreadsheet files from your .NET applications using one simple API.

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.

Screenshot
Export DataTable to Excel Screenshot

See the full code below, use Run Example to execute.

using System.Data;
using System.Text;
using GemBox.Spreadsheet;

class Sample
{
    [STAThread]
    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.xlsx");
    }
}
Imports System.Data
Imports System.Text
Imports GemBox.Spreadsheet

Module Samples

    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.xlsx")

    End Sub

End Module

Check next sample.