How to Import and Export as DataSet

This article was translated from English: Does it need improvement?
Translated
View the article in English

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.

Quickstart: Export Workbook as System.Data.DataSet Instantly

With IronXL, converting a workbook into a DataSet takes just one method call. This example shows how effortlessly you can export your entire workbook—each sheet as a DataTable—by using ToDataSet with optional first-row header recognition.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronXL with NuGet Package Manager

    PM > Install-Package IronXL.Excel

  2. Copy and run this code snippet.

    DataSet ds = WorkBook.Create().ToDataSet(useFirstRowAsColumnNames: true);
  3. Deploy to test on your live environment

    Start using IronXL in your project today with a free trial
    arrow pointer


Get started with IronXL

Comience a usar IronXL en su proyecto hoy con una prueba gratuita.

Primer Paso:
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.

Preguntas Frecuentes

¿Cómo puedo importar un DataSet en una hoja de cálculo en C#?

Puede usar el método LoadWorkSheetsFromDataSet de IronXL para importar un DataSet en una hoja de cálculo. Esto requiere tanto los objetos DataSet como Workbook, que puede iniciar usando el método Create.

¿Cuál es el proceso para exportar un libro de trabajo como un DataSet usando C#?

Para exportar un libro de trabajo como un DataSet en C#, use el método ToDataSet provisto por IronXL. Este método convierte cada hoja en un DataTable dentro del DataSet.

¿Puedo usar la primera fila como nombres de columna al convertir un libro de trabajo a un DataSet?

Sí, al usar el método ToDataSet en IronXL, puede establecer el parámetro useFirstRowAsColumnNames en verdadero para usar la primera fila como nombres de columna.

¿Cómo descargo la biblioteca IronXL para manejar DataSets en C#?

Puede descargar la biblioteca IronXL para manejar DataSets en C# desde NuGet visitando https://nuget.org/packages/IronXL.Excel/.

¿Qué pasos deben seguirse para importar un DataSet usando IronXL?

Primero, descargue IronXL, luego prepare su DataSet. Use el método LoadWorkSheetsFromDataSet para importarlo al libro de trabajo y verifique la conversión.

¿Es posible guardar un DataSet importado como un archivo Excel?

Sí, una vez que el DataSet se ha cargado en un libro de trabajo usando IronXL, puede guardarlo como un archivo Excel utilizando el método SaveAs, especificando el nombre y formato de archivo requerido.

¿Cuáles son las ventajas de usar un DataSet en C#?

Un DataSet permite una representación en memoria de múltiples tablas relacionadas, relaciones y restricciones, haciéndolo versátil para manejar datos de diversas fuentes como bases de datos y XML.

Chaknith Bin
Ingeniero de Software
Chaknith trabaja en IronXL e IronBarcode. Tiene un profundo conocimiento en C# y .NET, ayudando a mejorar el software y apoyar a los clientes. Sus conocimientos derivados de las interacciones con los usuarios contribuyen a mejores productos, documentación y experiencia en general.
¿Listo para empezar?
Nuget Descargas 1,686,155 | Versión: 2025.11 recién lanzado