Syncfusion Feedback

Import Data from a DataTable to an Excel Document with the Syncfusion .NET Excel Library

Overview

The Syncfusion® .NET Excel Library (XlsIO) enables you to create, read, and edit Excel documents programmatically without Microsoft Excel or interop dependencies. Using this library, you can import data from a DataTable to an Excel document using C#.

Watch this video to see how to import data from a DataTable to an Excel document using the Syncfusion .NET Excel Library:

Watch the video

Import data from a DataTable to an Excel document

Follow these steps to import data from a DataTable to an Excel document using the Syncfusion .NET Excel Library.

Step 1: Create a new project

Start by creating a new C# Console Application project.

Step 2: Install the NuGet package

Add the Syncfusion.XlsIO.Net.Core package to your project from NuGet.org.

Step 3: Include namespace

Add the following namespace to your Program.cs file:

using Syncfusion.XlsIO;

Step 4: Add code to import data from a DataTable to an Excel document

Use the following code in the Program.cs file to create an Excel document file and import data to it from a DataTable.

Populate DataTable

private static DataTable SampleDataTable()
    {
        //Create a DataTable with four columns.
        DataTable table = new DataTable();
        table.Columns.Add("Dosage", typeof(int));
        table.Columns.Add("Drug", typeof(string));
        table.Columns.Add("Patient", typeof(string));
        table.Columns.Add("Date", typeof(DateTime));

        //Add five DataRows
        table.Rows.Add(25, "Indocin", "David", DateTime.Now);
        table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
        table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
        table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
        table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);

        return table;
    }
}

Import data from DataTable to Excel

Run

class Program
{
    static void Main(string[] args)
    {
        using (ExcelEngine excelEngine = new ExcelEngine())
        {
            IApplication application = excelEngine.Excel;
            application.DefaultVersion = ExcelVersion.Xlsx;
            IWorkbook workbook = application.Workbooks.Create(1);
            IWorksheet worksheet = workbook.Worksheets[0];

            #region Import from Data Table
            //Initialize the DataTable.
            DataTable table = SampleDataTable();
            //Import DataTable to the worksheet
            worksheet.ImportDataTable(table, true, 1, 1);
		#endregion

            #region Save
            //Saving the workbook.
            FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/ImportDataTable.xlsx"), FileMode.Create, FileAccess.Write);
            workbook.SaveAs(outputStream);
            #endregion

            //Dispose streams.
            outputStream.Dispose();
        }
    }

NuGet installation

Nuget Installation image Syncfusion.XlsIO.Net.Core

Get started quickly by downloading the installer and checking license information on the Downloads page.

Syncfusion .NET Excel Library resources

Explore these resources for comprehensive guides, knowledge base articles, insightful blogs, and ebooks.