How to Import and Export as DataSet
A DataSet is an in-memory representation of data that can hold multiple related tables, relationships, and constraints. It's often used for working with data from various sources like databases, XML, and more.
A DataTable is a fundamental building block within a DataSet. It represents a single table with rows and columns, much like a table in a database. It's used to organize and manipulate data in a tabular form.
You can import a DataSet into a spreadsheet object and export it back into a DataSet using IronXL.
How to Import and Export as a DataSet
- Download the C# library for importing and exporting as a DataSet
- Prepare the DataSet to import into a spreadsheet object
- Use the
LoadWorkSheetsFromDataSet
method to import the DataSet into the workbook - Use the
ToDataSet
method 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.
Load DataSet
Utilize the static LoadWorkSheetsFromDataSet
method of the WorkBook to import a DataSet into the workbook. This method requires both the DataSet and WorkBook objects. Consequently, you should initiate the workbook or spreadsheet beforehand using the Create
method. In the provided code example, provide the DataSet object you wish to convert to the method, alongside the workbook object.
:path=/static-assets/excel/content-code-examples/how-to/export-dataset-datatable-load.cs
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)
Visit the How to Load Existing Spreadsheets article to learn more about importing spreadsheet from various file formats.
Export DataSet
The ToDataSet
method converts the workbook to a System.Data.DataSet, where each worksheet represents a System.Data.DataTable. Use this method on the current Excel workbook to convert it from a workbook to a DataSet object. The useFirstRowAsColumnNames parameter of the method configures whether to use the first row as column names.
:path=/static-assets/excel/content-code-examples/how-to/export-dataset-datatable-export.cs
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()
Visit the How to Save or Export Spreadsheets article to learn more about exporting spreadsheet to various file formats.