Updated February 12, 2024
How to Export DataTable to Excel in the Fastest Way
This article will show you how to create a DataTable
in C# and then export data to an Excel document using the IronXL .NET Excel library.
IronXL .NET Excel Library
IronXL is a C# .NET library, which helps create Excel files with ease. Multiple sheets can be added to the newly created Excel workbook. It provides almost all Microsoft Excel features to work with Excel files in C#. It helps developers open CSV files along with Excel files and modify existing data in Excel worksheets.
Most importantly, IronXL works without Microsoft Office Interop. This means no need to install Microsoft Office or any other special dependencies. IronXL works on .NET Core and .NET Framework 5, 6 and 7.
With IronXL, you can save or export data in different formats like XLS and XLSX, CSV data and TSV, JSON, XML and HTML, Binary and Byte Array. It helps edit metadata, workbook permissions and passwords, edit formulas, edit layout, work with cell range and cell styles and many more Excel features.
Steps to Export Data from Datatable to Excel File
Prerequisites
To use IronXL in exporting data from DataTable
to an Excel file in C#, the following components need to be installed on the local computer. Let's have a look at them one by one.
Visual Studio - Visual Studio is the IDE for C# programming and must be installed. You can download and install the latest version from the Visual Studio website.
- Once the IDE is set, a Console Application/Windows Forms needs to be created which will help to export
DataTable
to Excel. The following screenshots show how to create a project.
Create a Project
Console App
Name project
.NET Framework
After clicking Create in the last screenshot, the project with the name "DemoApp"** is created.
- Once the IDE is set, a Console Application/Windows Forms needs to be created which will help to export
IronXL library - the library must be downloaded and installed in the Visual Studio project. There are multiple ways to do it.
- Using Visual Studio - It provides NuGet Package Manager to install IronXL. You can access it via the Tools menu or Solution Explorer. The following screenshots help to install IronXL.
Solution Explorer
Tools - NuGet Package Manager
Browse IronXL
- Developer Command Prompt - Open the Developer Command Prompt either from the Visual Studio Tools menu or from the Visual Studio folder. Type the following command to download and install IronXL in your project:
Install-Package IronXL.Excel
- Directly from NuGet Package Manager - Navigate to this URL https://www.nuget.org/packages/ironxl.excel/ to download IronXL.
Add Necessary Namespaces - To create a
DataTable
and use IronXL, both should be referenced on top of the Program.cs file.using IronXL; //add reference... using System.Data;
using IronXL; //add reference... using System.Data;
Imports IronXL 'add reference... Imports System.Data
VB C#
Once all the prerequisites are fulfilled, it's time to export data from DataTable
to an Excel sheet.
Create a DataTable in C#
The following code helps to create a data table with two column headers and multiple rows:
//new datatable dt...
DataTable dt = new DataTable();
//add column names...
dt.Columns.Add("Animal");
dt.Columns.Add("Sound");
dt.Rows.Add("Lion", "Roars"); // first row...
dt.Rows.Add("Dog", "Barks");
dt.Rows.Add("Cat", "Meows");
dt.Rows.Add("Goat", "Bleats");
dt.Rows.Add("Wolf", "Howls");
dt.Rows.Add("Cheetah", "Purrs");
//new datatable dt...
DataTable dt = new DataTable();
//add column names...
dt.Columns.Add("Animal");
dt.Columns.Add("Sound");
dt.Rows.Add("Lion", "Roars"); // first row...
dt.Rows.Add("Dog", "Barks");
dt.Rows.Add("Cat", "Meows");
dt.Rows.Add("Goat", "Bleats");
dt.Rows.Add("Wolf", "Howls");
dt.Rows.Add("Cheetah", "Purrs");
'new datatable dt...
Dim dt As New DataTable()
'add column names...
dt.Columns.Add("Animal")
dt.Columns.Add("Sound")
dt.Rows.Add("Lion", "Roars") ' first row...
dt.Rows.Add("Dog", "Barks")
dt.Rows.Add("Cat", "Meows")
dt.Rows.Add("Goat", "Bleats")
dt.Rows.Add("Wolf", "Howls")
dt.Rows.Add("Cheetah", "Purrs")
First, a DataTable
object is created and then added columns with column names "Animal" and "Sound". After that, using this dt
object, multiple rows are added. Similarly, multiple column headers can be added along with multiple rows.
Create Excel File using IronXL in C#
Creating an Excel file using IronXL is a two-step process and very easy to implement in C#. An Excel file is a combination of a workbook having multiple worksheets, IronXL first creates an Excel WorkBook
and then helps add a WorkSheet
to it. The following sample code will help create a workbook along with the worksheet:
//create new workbook...
WorkBook wb = WorkBook.Create(ExcelFileFormat.XLSX);
WorkSheet ws = wb.DefaultWorkSheet;
//create new workbook...
WorkBook wb = WorkBook.Create(ExcelFileFormat.XLSX);
WorkSheet ws = wb.DefaultWorkSheet;
'create new workbook...
Dim wb As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
Dim ws As WorkSheet = wb.DefaultWorkSheet
Exporting Data from DataTable to Excel Worksheet using IronXL
IronXL facilitates adding values to worksheets in very easy syntax. This how-to will show how to export data from datatable created in the previous section to the newly created Excel worksheet. Let's have a look at the code step by step.
Add column header to Excel worksheet
ws["A1"].Value = table.Columns[0].ToString();
ws["B1"].Value = table.Columns[1].ToString();
int rowCount = 2;
ws["A1"].Value = table.Columns[0].ToString();
ws["B1"].Value = table.Columns[1].ToString();
int rowCount = 2;
ws("A1").Value = table.Columns(0).ToString()
ws("B1").Value = table.Columns(1).ToString()
Dim rowCount As Integer = 2
In the above code, the Excel sheet column "A1" is assigned the value from the DataTable
column 1 at index 0 and the next Excel column "B1" value is assigned from column 2 at index 1 in the DataTable
. The rowCount variable is set to a value of 2 for reading rows from DataTable
.
Add Rows to Excel Worksheet
The following code will read each row from the DataTable
and assign it to a new row in the Excel file:
foreach (DataRow row in table.Rows)
{
ws["A" + (rowCount)].Value = row[0].ToString();
ws["B" + (rowCount)].Value = row[1].ToString();
rowCount++;
}
foreach (DataRow row in table.Rows)
{
ws["A" + (rowCount)].Value = row[0].ToString();
ws["B" + (rowCount)].Value = row[1].ToString();
rowCount++;
}
For Each row As DataRow In table.Rows
ws("A" & (rowCount)).Value = row(0).ToString()
ws("B" & (rowCount)).Value = row(1).ToString()
rowCount += 1
Next row
The rowCount
variable is incremented every time so that a new row is read from the datatable to Excel worksheet cells in the above code.
Save Excel File
Finally, save the Excel file using the SaveAs
method.
wb.SaveAs("DataTable_to_Excel_IronXL.xlsx");
wb.SaveAs("DataTable_to_Excel_IronXL.xlsx");
wb.SaveAs("DataTable_to_Excel_IronXL.xlsx")
The file can be saved in other formats as well e.g. CSV (Comma Separated Values), JSON, XML.
wb.SaveAsCsv("DataTable_to_Excel_IronXL.csv");
wb.SaveAsJson("DataTable_to_Excel_IronXL.json");
wb.SaveAsXml("DataTable_to_Excel_IronXL.xml");
wb.SaveAsCsv("DataTable_to_Excel_IronXL.csv");
wb.SaveAsJson("DataTable_to_Excel_IronXL.json");
wb.SaveAsXml("DataTable_to_Excel_IronXL.xml");
wb.SaveAsCsv("DataTable_to_Excel_IronXL.csv")
wb.SaveAsJson("DataTable_to_Excel_IronXL.json")
wb.SaveAsXml("DataTable_to_Excel_IronXL.xml")
You can also save it with a custom delimiter.
The final output of the file looks like this:
DataTable to Excel Output
Summary
This article demonstrated how to create a DataTable
with columns and rows in C# and then create an Excel workbook with a default worksheet using IronXL and export tabular data from DataTable
to Excel and save in XLSX file format.
IronXL is an intuitive C# Excel library that helps developers work with Excel files even without an Excel application. It allows developers to export from different formats like CSV data format to Excel for data manipulation and calculation. You can explore more powerful features of IronXL from the code examples page.
IronXL is free for individual development purposes and can be licensed for commercial use. It also provides all features in a free trial to ensure compatibility with one's needs. You can get more information on IronXL licenses from this licensing page.
Download the software product.