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

Commencez à utiliser IronXL dans votre projet aujourd'hui avec un essai gratuit.

Première étape :
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.

Questions Fréquemment Posées

Comment puis-je importer un DataSet dans un tableur en C# ?

Vous pouvez utiliser la méthode LoadWorkSheetsFromDataSet d'IronXL pour importer un DataSet dans un tableur. Cela nécessite les objets DataSet et Workbook, que vous pouvez initier en utilisant la méthode Create.

Quel est le processus pour exporter un classeur en tant que DataSet en utilisant C# ?

Pour exporter un classeur en tant que DataSet en C#, utilisez la méthode ToDataSet fournie par IronXL. Cette méthode convertit chaque feuille de calcul en un DataTable dans le DataSet.

Puis-je utiliser la première ligne comme noms de colonnes lors de la conversion d'un classeur en DataSet ?

Oui, en utilisant la méthode ToDataSet dans IronXL, vous pouvez définir le paramètre useFirstRowAsColumnNames sur vrai pour utiliser la première ligne comme noms de colonnes.

Comment puis-je télécharger la bibliothèque IronXL pour gérer les DataSets en C# ?

Vous pouvez télécharger la bibliothèque IronXL pour gérer les DataSets en C# depuis NuGet en visitant https://nuget.org/packages/IronXL.Excel/.

Quelles étapes doivent être suivies pour importer un DataSet à l'aide de IronXL ?

D'abord, téléchargez IronXL, puis préparez votre DataSet. Utilisez la méthode LoadWorkSheetsFromDataSet pour l'importer dans le classeur et vérifiez la conversion.

Est-il possible d'enregistrer un DataSet importé en tant que fichier Excel ?

Oui, une fois que le DataSet est chargé dans un classeur en utilisant IronXL, vous pouvez l'enregistrer en tant que fichier Excel en utilisant la méthode SaveAs, en spécifiant le nom de fichier et le format requis.

Quels sont les avantages d'utiliser un DataSet en C# ?

Un DataSet permet une représentation en mémoire de plusieurs tables liées, relations et contraintes, le rendant polyvalent pour gérer des données de diverses sources telles que des bases de données et XML.

Chaknith Bin
Ingénieur logiciel
Chaknith travaille sur IronXL et IronBarcode. Il a une expertise approfondie en C# et .NET, aidant à améliorer le logiciel et à soutenir les clients. Ses idées issues des interactions avec les utilisateurs contribuent à de meilleurs produits, documentation et expérience globale.
Prêt à commencer?
Nuget Téléchargements 1,686,155 | Version : 2025.11 vient de sortir