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

今天在您的项目中使用 IronXL,免费试用。

第一步:
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.

常见问题解答

如何在 C# 中将 DataSet 导入电子表格?

您可以使用 IronXL 的 LoadWorkSheetsFromDataSet 方法将 DataSet 导入电子表格。这需要 DataSet 和 Workbook 对象,您可以使用 Create 方法进行初始化。

使用 C# 将工作簿导出为 DataSet 的过程是什么?

要在 C# 中将工作簿导出为 DataSet,请使用 IronXL 提供的 ToDataSet 方法。该方法将每个工作表转换为 DataSet 中的 DataTable。

在将工作簿转换为 DataSet 时可以使用第一行作为列名吗?

是的,使用 IronXL 的 ToDataSet 方法时,您可以设置 useFirstRowAsColumnNames 参数为 true,以使用第一行作为列名。

如何下载 IronXL 库以管理 C# 中的 DataSet?

您可以通过访问 https://nuget.org/packages/IronXL.Excel/ 从 NuGet 下载用于管理 C# 中 DataSet 的 IronXL 库。

使用 IronXL 导入 DataSet 应遵循哪些步骤?

首先,下载 IronXL,然后准备您的 DataSet。使用 LoadWorkSheetsFromDataSet 方法将其导入工作簿并验证转换。

是否可以将导入的 DataSet 保存为 Excel 文件?

是的,一旦 DataSet 使用 IronXL 加载到工作簿中,您可以使用 SaveAs 方法将其保存为 Excel 文件,指定所需的文件名和格式。

在 C# 中使用 DataSet 的优势是什么?

DataSet 允许内存中表示多个相关表、关系和约束,使其在处理来自各种来源(如数据库和 XML)的数据时非常灵活。

Chaknith Bin
软件工程师
Chaknith 在 IronXL 和 IronBarcode 工作。他在 C# 和 .NET 方面有着深厚的专业知识,帮助改进软件并支持客户。他从用户互动中获得的见解有助于更好的产品、文档和整体体验。
准备开始了吗?
Nuget 下载 1,686,155 | 版本: 2025.11 刚刚发布