User GuideFAQHow to export DataTable to Excel file in C# and VB.NET
How to export DataTable to Excel file in C# and VB.NET
EasyXLS™ library allows you to export a DataTable to an Excel file. The data in cells can be formatted using predefined formats or user-defined formats.
EasyXLS can be successfully used inclusively to export large Excel files having big volume of data with fast exporting time.
The source code samples can be integrated in:
- ASP.NET web pages - Windows applications - Windows Forms (WinForms) - Console applications - Windows service applications - ASP.NET MVC web applications
EasyXLS permits you to export Excel files without Excel installed, without Interop or any other additional software installed.
' Create and populate the datatableDim dataTable As DataTable = New DataTable
Dim columns As DataColumn() = {New DataColumn("ID"), New DataColumn("Value")}
Dim row1 As Object() = {"1", "Value1"}
Dim row2 As Object() = {"2", "Value2"}
Dim row3 As Object() = {"3", "Value3"}
dataTable.Columns.AddRange(columns)
dataTable.Rows.Add(row1)
dataTable.Rows.Add(row2)
dataTable.Rows.Add(row3)
' Create a dataset that keeps the datatableDim dataSet As DataSet = New DataSet
dataSet.Tables.Add(dataTable)
' Export the dataset and its datatable to Excel fileDim workbook As EasyXLS.ExcelDocument = New EasyXLS.ExcelDocument
workbook.easy_WriteXLSXFile_FromDataSet( _
"C:\Samples\DataTable to Excel.xlsx", _
dataSet, _
New EasyXLS.ExcelAutoFormat(EasyXLS.Constants.Styles.AUTOFORMAT_EASYXLS1), _
"DataTable")
' Dispose memory
workbook.Dispose()
dataSet.Dispose()