How to Import & Export DataSet/DataTable in C#
IronXL converts between Excel workbooks and DataSets with single method calls - use LoadWorkSheetsFromDataSet() to import DataSets into workbooks and ToDataSet() to export workbooks as DataSets with automatic sheet-to-table mapping.
A DataSet is an in-memory representation of data containing multiple related tables, relationships, and constraints. Used for working with data from databases, XML, and other sources. When working with Excel files in C#, DataSets provide a familiar .NET structure that integrates with data-driven applications.
A DataTable represents a single table with rows and columns within a DataSet. IronXL automatically maps each Excel worksheet to a corresponding DataTable, preserving column headers and data types during conversion.
Quickstart: Export Workbook as System.Data.DataSet Instantly
Converting a workbook into a DataSet takes one method call. This example exports your entire workbook—each sheet as a DataTable—using ToDataSet with optional first-row header recognition.
Get started making PDFs with NuGet now:
Install IronXL with NuGet Package Manager
Copy and run this code snippet.
DataSet ds = WorkBook.Create().ToDataSet(useFirstRowAsColumnNames: true);Deploy to test on your live environment
Minimal Workflow (5 steps)
- Download the C# library for importing and exporting as a DataSet
- Prepare the DataSet to import into a spreadsheet object
- Use the
LoadWorkSheetsFromDataSetmethod to import the DataSet into the workbook - Use the
ToDataSetmethod to export the workbook as a DataSet - Check the conversion result
Get started with IronXL
Start using IronXL in your project today with a free trial.
How Do I Load a DataSet into a Workbook?
Use the static LoadWorkSheetsFromDataSet method to import a DataSet into a workbook. This method requires both DataSet and Workbook objects. Create the workbook first using the Create method. Pass the DataSet object and workbook object to the method.
:path=/static-assets/excel/content-code-examples/how-to/export-dataset-datatable-load.csusing IronXL;
using System.Data;
// Create dataset
DataSet dataSet = new DataSet();
// Create workbook
WorkBook workBook = WorkBook.Create();
// Load DataSet to workBook
WorkBook.LoadWorkSheetsFromDataSet(dataSet, workBook);Imports IronXL
Imports System.Data
' Create dataset
Private dataSet As New DataSet()
' Create workbook
Private workBook As WorkBook = WorkBook.Create()
' Load DataSet to workBook
WorkBook.LoadWorkSheetsFromDataSet(dataSet, workBook)Each DataTable within the DataSet becomes a separate worksheet. The table name becomes the worksheet name, maintaining data organization. For multiple data sources, combine this with creating new spreadsheets or managing existing worksheets.
Here's a comprehensive example loading a DataSet with multiple tables:
using IronXL;
using System.Data;
// Create a DataSet with multiple tables
DataSet salesData = new DataSet("CompanySales");
// Create and populate a products table
DataTable productsTable = new DataTable("Products");
productsTable.Columns.Add("ProductID", typeof(int));
productsTable.Columns.Add("ProductName", typeof(string));
productsTable.Columns.Add("Price", typeof(decimal));
// Add sample data
productsTable.Rows.Add(1, "Laptop", 999.99m);
productsTable.Rows.Add(2, "Mouse", 19.99m);
productsTable.Rows.Add(3, "Keyboard", 49.99m);
// Create and populate a sales table
DataTable salesTable = new DataTable("Sales");
salesTable.Columns.Add("SaleID", typeof(int));
salesTable.Columns.Add("ProductID", typeof(int));
salesTable.Columns.Add("Quantity", typeof(int));
salesTable.Columns.Add("Date", typeof(DateTime));
// Add sample sales data
salesTable.Rows.Add(1, 1, 5, DateTime.Now);
salesTable.Rows.Add(2, 2, 25, DateTime.Now.AddDays(-1));
salesTable.Rows.Add(3, 3, 10, DateTime.Now.AddDays(-2));
// Add tables to DataSet
salesData.Tables.Add(productsTable);
salesData.Tables.Add(salesTable);
// Create workbook and load DataSet
WorkBook workBook = WorkBook.Create();
WorkBook.LoadWorkSheetsFromDataSet(salesData, workBook);
// Save the workbook with all imported data
workBook.SaveAs("SalesReport.xlsx");using IronXL;
using System.Data;
// Create a DataSet with multiple tables
DataSet salesData = new DataSet("CompanySales");
// Create and populate a products table
DataTable productsTable = new DataTable("Products");
productsTable.Columns.Add("ProductID", typeof(int));
productsTable.Columns.Add("ProductName", typeof(string));
productsTable.Columns.Add("Price", typeof(decimal));
// Add sample data
productsTable.Rows.Add(1, "Laptop", 999.99m);
productsTable.Rows.Add(2, "Mouse", 19.99m);
productsTable.Rows.Add(3, "Keyboard", 49.99m);
// Create and populate a sales table
DataTable salesTable = new DataTable("Sales");
salesTable.Columns.Add("SaleID", typeof(int));
salesTable.Columns.Add("ProductID", typeof(int));
salesTable.Columns.Add("Quantity", typeof(int));
salesTable.Columns.Add("Date", typeof(DateTime));
// Add sample sales data
salesTable.Rows.Add(1, 1, 5, DateTime.Now);
salesTable.Rows.Add(2, 2, 25, DateTime.Now.AddDays(-1));
salesTable.Rows.Add(3, 3, 10, DateTime.Now.AddDays(-2));
// Add tables to DataSet
salesData.Tables.Add(productsTable);
salesData.Tables.Add(salesTable);
// Create workbook and load DataSet
WorkBook workBook = WorkBook.Create();
WorkBook.LoadWorkSheetsFromDataSet(salesData, workBook);
// Save the workbook with all imported data
workBook.SaveAs("SalesReport.xlsx");IRON VB CONVERTER ERROR developers@ironsoftware.comThis approach works well when exporting data from SQL databases or consolidating data from multiple sources into Excel format.
Visit How to Load Existing Spreadsheets to learn about importing spreadsheets from various file formats.
How Do I Export a Workbook as a DataSet?
The ToDataSet method converts the workbook to a System.Data.DataSet, where each worksheet becomes a System.Data.DataTable. Call this method on the Excel workbook to convert it to a DataSet object. The useFirstRowAsColumnNames parameter determines whether to use the first row as column names.
:path=/static-assets/excel/content-code-examples/how-to/export-dataset-datatable-export.csusing IronXL;
using System.Data;
// Create new Excel WorkBook document
WorkBook workBook = WorkBook.Create();
// Create a blank WorkSheet
WorkSheet workSheet = workBook.CreateWorkSheet("new_sheet");
// Export as DataSet
DataSet dataSet = workBook.ToDataSet();Imports IronXL
Imports System.Data
' Create new Excel WorkBook document
Private workBook As WorkBook = WorkBook.Create()
' Create a blank WorkSheet
Private workSheet As WorkSheet = workBook.CreateWorkSheet("new_sheet")
' Export as DataSet
Private dataSet As DataSet = workBook.ToDataSet()IronXL automatically handles data type conversion and maintains spreadsheet structure when exporting to DataSets. This helps when integrating Excel data with SQL databases or using data in other .NET applications.
Advanced example demonstrating workbook export and DataSet processing:
using IronXL;
using System;
using System.Data;
// Load an existing Excel file
WorkBook workBook = WorkBook.Load("FinancialData.xlsx");
// Export to DataSet with column headers from first row
DataSet financialDataSet = workBook.ToDataSet(useFirstRowAsColumnNames: true);
// Process each DataTable in the DataSet
foreach (DataTable table in financialDataSet.Tables)
{
Console.WriteLine($"Processing table: {table.TableName}");
Console.WriteLine($"Columns: {table.Columns.Count}, Rows: {table.Rows.Count}");
// Iterate through columns
foreach (DataColumn column in table.Columns)
{
Console.WriteLine($" Column: {column.ColumnName} ({column.DataType})");
}
// Process first 5 rows as example
int rowCount = 0;
foreach (DataRow row in table.Rows)
{
if (rowCount++ >= 5) break;
// Access data by column name
foreach (DataColumn col in table.Columns)
{
Console.WriteLine($" {col.ColumnName}: {row[col]}");
}
}
}
// You can also export specific worksheets as DataTables
WorkSheet specificSheet = workBook.WorkSheets["Q1Sales"];
DataTable q1Data = specificSheet.ToDataTable(useFirstRowAsColumnNames: true);
// Use the DataTable with other .NET components
// For example, bind to a DataGridView or save to databaseusing IronXL;
using System;
using System.Data;
// Load an existing Excel file
WorkBook workBook = WorkBook.Load("FinancialData.xlsx");
// Export to DataSet with column headers from first row
DataSet financialDataSet = workBook.ToDataSet(useFirstRowAsColumnNames: true);
// Process each DataTable in the DataSet
foreach (DataTable table in financialDataSet.Tables)
{
Console.WriteLine($"Processing table: {table.TableName}");
Console.WriteLine($"Columns: {table.Columns.Count}, Rows: {table.Rows.Count}");
// Iterate through columns
foreach (DataColumn column in table.Columns)
{
Console.WriteLine($" Column: {column.ColumnName} ({column.DataType})");
}
// Process first 5 rows as example
int rowCount = 0;
foreach (DataRow row in table.Rows)
{
if (rowCount++ >= 5) break;
// Access data by column name
foreach (DataColumn col in table.Columns)
{
Console.WriteLine($" {col.ColumnName}: {row[col]}");
}
}
}
// You can also export specific worksheets as DataTables
WorkSheet specificSheet = workBook.WorkSheets["Q1Sales"];
DataTable q1Data = specificSheet.ToDataTable(useFirstRowAsColumnNames: true);
// Use the DataTable with other .NET components
// For example, bind to a DataGridView or save to databaseIRON VB CONVERTER ERROR developers@ironsoftware.comThe exported DataSet maintains relationships and constraints defined in Excel, perfect for working with complex Excel data structures without Excel Interop dependencies.
Additional DataSet/DataTable Features
IronXL provides advanced features when working with DataSets and DataTables:
Handling Data Types
IronXL intelligently maps data types during Excel-DataSet conversion. Numeric cells become appropriate numeric types (int, decimal, double), date cells become DateTime objects, text cells become strings. Customize this by setting cell data formats before conversion.
Performance Optimization
IronXL optimizes memory usage and processing speed for large datasets. The library efficiently streams data rather than loading everything into memory. Suitable for enterprise applications processing large data volumes.
Integration with Other Features
DataSet/DataTable functionality integrates with other IronXL features:
- Apply formulas and calculations before exporting to DataSet
- Use conditional formatting to highlight data before conversion
- Combine with chart creation for visual reporting
Visit How to Save or Export Spreadsheets to learn about exporting spreadsheets to various file formats.
Frequently Asked Questions
How do I convert an Excel workbook to a DataSet in C#?
With IronXL, you can convert an Excel workbook to a DataSet using the ToDataSet() method. Simply call WorkBook.ToDataSet(useFirstRowAsColumnNames: true) to export your entire workbook where each sheet becomes a DataTable in the resulting DataSet.
Can I import a DataSet into an Excel workbook programmatically?
Yes, IronXL provides the LoadWorkSheetsFromDataSet() method to import a DataSet into a workbook. Create a workbook using WorkBook.Create(), then pass both the DataSet and workbook to this static method. Each DataTable becomes a separate worksheet.
What happens to DataTable names when importing into Excel?
When using IronXL's LoadWorkSheetsFromDataSet() method, each DataTable name automatically becomes the worksheet name in Excel, maintaining your data organization and making it easy to identify different data sources.
Does the library preserve column headers during DataSet conversion?
Yes, IronXL preserves column headers and data types during conversion between Excel and DataSets. When using ToDataSet() with useFirstRowAsColumnNames set to true, the first row of each worksheet is recognized as column headers.
How are multiple tables handled when converting between Excel and DataSets?
IronXL automatically maps each Excel worksheet to a corresponding DataTable when converting to DataSet, and vice versa. When importing a DataSet with multiple tables, each DataTable creates a separate worksheet in the workbook.
What is the minimal code needed to export Excel to DataSet?
The minimal code to export an Excel workbook to DataSet using IronXL is just one line: DataSet ds = WorkBook.Create().ToDataSet(useFirstRowAsColumnNames: true); This converts your entire workbook with automatic sheet-to-table mapping.






