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#, DataSet 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.
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.
-
1Install IronXL with NuGet Package Manager
-
2Copy and run this code snippet.
DataSet ds = WorkBook.Create().ToDataSet(useFirstRowAsColumnNames: true);C# -
3Deploy to test on your live environment
Start using IronXL in your project today with a free trial
Minimal Workflow (5 steps)
- Download the C# library for importing and exporting as a DataSet
- Prepare the
DataSetto import into a spreadsheet object - Use the
LoadWorkSheetsFromDataSetmethod to import theDataSetinto the workbook - Use the
ToDataSetmethod to export the workbook as aDataSet - Check the conversion result
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. It also accepts optional settings via DataSet. Create the workbook first using the Create method. Pass the DataSet object and workbook object to the method.
using 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");Imports IronXL
Imports System.Data
' Create a DataSet with multiple tables
Dim salesData As New DataSet("CompanySales")
' Create and populate a products table
Dim productsTable As New DataTable("Products")
productsTable.Columns.Add("ProductID", GetType(Integer))
productsTable.Columns.Add("ProductName", GetType(String))
productsTable.Columns.Add("Price", GetType(Decimal))
' Add sample data
productsTable.Rows.Add(1, "Laptop", 999.99D)
productsTable.Rows.Add(2, "Mouse", 19.99D)
productsTable.Rows.Add(3, "Keyboard", 49.99D)
' Create and populate a sales table
Dim salesTable As New DataTable("Sales")
salesTable.Columns.Add("SaleID", GetType(Integer))
salesTable.Columns.Add("ProductID", GetType(Integer))
salesTable.Columns.Add("Quantity", GetType(Integer))
salesTable.Columns.Add("Date", GetType(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
Dim workBook As WorkBook = WorkBook.Create()
WorkBook.LoadWorkSheetsFromDataSet(salesData, workBook)
' Save the workbook with all imported data
workBook.SaveAs("SalesReport.xlsx")This 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.
using 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.GetWorkSheet("Q1Sales");
DataTable q1Data = specificSheet.ToDataTable(useFirstRowAsColumnNames: true);
// Use the DataTable with other .NET components
// For example, bind to a DataGridView or save to database
The 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 DataSet and DataTable:
Handling Data Types
IronXL intelligently maps data types during Excel-DataSet conversion. Numeric cells become appropriate numeric types (Int32, Double, Decimal), 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 a
DataSet - Use conditional formatting to highlight data before conversion
- Combine with chart creation for visual reporting
- Support custom serialization using
int - Include data validation rules via
decimal - Handle missing values with
double - Manage worksheet metadata through
DateTime - Preserve cell formatting types using
string
Visit How to Save or Export Spreadsheets to learn about exporting spreadsheets to various file formats.
Frequently Asked Questions
How can I import a DataSet into an Excel workbook using C#?
You can use the IronXL method LoadWorkSheetsFromDataSet to import a DataSet into an Excel workbook in C#. This involves creating both DataSet and Workbook objects and passing them to the method. Each DataTable in the DataSet becomes a worksheet in the workbook.
What is the process for exporting an Excel workbook as a DataSet in C#?
To export an Excel workbook as a DataSet using C#, IronXL offers the ToDataSet method. This method converts each worksheet into a DataTable and returns the DataSet, ensuring data types and structures are preserved.
What are the benefits of using IronXL for DataSet and DataTable operations?
IronXL provides seamless conversion between Excel files and DataSets/DataTables with automatic mapping and type conversion. It optimizes performance for large datasets and integrates with other features like formulas, conditional formatting, and charting.
Can IronXL handle different data types during Excel conversion?
Yes, IronXL intelligently maps data types during the Excel-DataSet conversion. Numeric cells are converted to Int32, Double, or Decimal, date cells to DateTime objects, and text cells to Strings.
Is it possible to use Excel DataSet integration with SQL databases in C#?
Indeed, IronXL facilitates this by allowing export of Excel data to DataSets, which can then be used with SQL databases. This is useful for data manipulation and integration in .NET applications.
How can I apply formulas and calculations before exporting a workbook to a DataSet?
With IronXL, you can edit formulas and apply calculations to your Excel workbook before using the ToDataSet method. This ensures that your calculations are included in the exported DataSet.
What performance optimizations does IronXL include for handling large datasets?
IronXL is optimized for high performance, using efficient streaming techniques to manage memory and speed when working with large data volumes, making it suitable for enterprise-level applications.
How does IronXL maintain data structure when converting a workbook to a DataSet?
IronXL maintains the organizational structure by converting each worksheet to a DataTable. The relationships, constraints, column headers, and data types are preserved during conversion, allowing for complex data interactions.
Can I use IronXL to create visual reports using Excel data?
Yes, IronXL supports creating charts and visual reports by integrating its DataSet/DataTable functionality with its chart creation features, allowing for comprehensive data visualization directly from Excel data.
Does IronXL support the integration of custom serialization in DataSet operations?
IronXL allows for custom serialization, which can enhance DataSet and DataTable manipulation according to specific application requirements, providing greater flexibility in handling serialized data.

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.