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.


Get started with IronXL

Start using IronXL in your project today with a free trial.

First Step:
green arrow pointer


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)
$vbLabelText   $csharpLabel

Visit the How to Load Existing Spreadsheets article to learn more about importing spreadsheets 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()
$vbLabelText   $csharpLabel

Visit the How to Save or Export Spreadsheets article to learn more about exporting spreadsheets to various file formats.

Frequently Asked Questions

How can I import a DataSet into a spreadsheet in C#?

You can use IronXL's LoadWorkSheetsFromDataSet method to import a DataSet into a spreadsheet. This requires both the DataSet and Workbook objects, which you can initiate using the Create method.

What is the process to export a workbook as a DataSet using C#?

To export a workbook as a DataSet in C#, use the ToDataSet method provided by IronXL. This method converts each worksheet into a DataTable within the DataSet.

Can I use the first row as column names when converting a workbook to a DataSet?

Yes, when using the ToDataSet method in IronXL, you can set the useFirstRowAsColumnNames parameter to true to use the first row as column names.

How do I download the IronXL library for managing DataSets in C#?

You can download the IronXL library for managing DataSets in C# from NuGet by visiting https://nuget.org/packages/IronXL.Excel/.

What steps should be followed to import a DataSet using IronXL?

First, download IronXL, then prepare your DataSet. Use the LoadWorkSheetsFromDataSet method to import it into the workbook and verify the conversion.

Is it possible to save an imported DataSet as an Excel file?

Yes, once the DataSet is loaded into a workbook using IronXL, you can save it as an Excel file by using the SaveAs method, specifying the required file name and format.

What are the advantages of using a DataSet in C#?

A DataSet allows for an in-memory representation of multiple related tables, relationships, and constraints, making it versatile for handling data from various sources such as databases and XML.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.