How to Use C# to Convert Datatable to CSV

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

You can export a datatable to CSV with IronXL by taking an existing data table and converting it to CSV in just a few steps. This article aims to show you a quick example of this.

Quickstart: One-Line Export of DataTable to CSV

Use IronXL to instantly convert a filled DataTable into a CSV file with just one easy call—no loops, no Interop, no fuss. You’ll only need a WorkBook and its DefaultWorkSheet to export in seconds using SaveAsCsv.

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.

    IronXL.WorkBook.Create().DefaultWorkSheet.SaveAsCsv("output.csv", ",");
  3. Deploy to test on your live environment

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

Step 1

1. Add IronXL Free

You need IronXL installed first before you can use it in your applications. Luckily, they provide many options for installing IronXL into your projects.

Download from their site by using the following link: https://ironsoftware.com/csharp/excel/docs/

or

  • In Visual Studio, select the Project menu
  • Click Manage NuGet Packages
  • Search for IronXL.Excel
  • Click Install
Install-Package IronXL.Excel
IronXL.Excel NuGet Package
Figure 1 - IronXL.Excel NuGet Package

How to Tutorial

2. Create and Export Datatable to CSV

Now you are ready.

First, import the IronXL namespace.

using IronXL;
using IronXL;
Imports IronXL
$vbLabelText   $csharpLabel

Then, add the following code:

:path=/static-assets/excel/content-code-examples/how-to/csharp-database-to-csv-datatable.cs
using IronXL;
using System;
using System.Data;

// Create a new DataTable object
DataTable table = new DataTable();

// Add a single column named "Example_DataSet" of type string
table.Columns.Add("Example_DataSet", typeof(string));

// Add rows to the DataTable
table.Rows.Add("0");
table.Rows.Add("1");
table.Rows.Add("2");
table.Rows.Add("3");
table.Rows.Add("1");
table.Rows.Add("2");
table.Rows.Add("3");

// Create a new Excel workbook and set its author metadata
WorkBook wb = WorkBook.Create(ExcelFileFormat.XLS);
wb.Metadata.Author = "OJ";

// Get the default worksheet
WorkSheet ws = wb.DefaultWorkSheet;

// Initialize rowCounter for Excel sheet rows
int rowCount = 1;

// Loop through each row in the DataTable and add the data to the Excel worksheet
foreach (DataRow row in table.Rows)
{
    // Populate worksheet cells with data from DataTable
    ws["A" + (rowCount)].Value = row[0].ToString();
    rowCount++;
}

// Save the workbook as a CSV file
wb.SaveAsCsv("Save_DataTable_CSV.csv", ";"); // Will be saved as: Save_DataTable_CSV.Sheet1.csv
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

The above code creates a data table, then creates a new workbook specifying ‘OJ’ as its owner/creator. A foreach loop follows that inserts the data from the data table into the Excel Worksheet. Lastly, the SaveAsCsv method is used to export the datatable to CSV.

The output Excel Worksheet looks as follows:

Datatable output to CSV
Figure 2 - Datatable output to CSV

Library Quick Access

IronXL API Reference Documentation

Learn more and share how to merge, unmerge, and work with cells in Excel spreadsheets using the handy IronXL API Reference Documentation.

IronXL API Reference Documentation
Documentation related to Library Quick Access

常见问题解答

如何在 C# 中将 DataTable 转换为 CSV?

您可以使用 IronXL 在 C# 中将 DataTable 转换为 CSV,方法是首先安装 IronXL 库,创建并填充数据表,然后使用 SaveAsCsv 方法将数据导出到 CSV 文件。

使用 IronXL 将 DataTable 转换为 CSV 的步骤是什么?

首先,通过 NuGet 或从他们的网站安装 IronXL 库。导入 IronXL 命名空间,创建并填充数据表,然后创建一个 Excel 工作簿来保存数据。最后,使用 SaveAsCsv 方法将工作簿导出为 CSV 文件。

使用 IronXL 将 DataTable 转换为 CSV 是否需要安装 Microsoft Excel?

不,您不需要安装 Microsoft Excel。IronXL 允许您在不依赖 Excel Interop 的情况下将 DataTable 转换为 CSV。

如何在我的 C# 项目中安装 IronXL?

您可以通过从他们的网站下载或通过 Visual Studio 的 NuGet 包管理器安装 IronXL。搜索 'IronXL.Excel' 并点击安装。

您可以提供一个使用 IronXL 将 DataTable 转换为 CSV 的代码示例吗?

当然!首先,创建并填充一个 DataTable。然后,导入 IronXL 命名空间,创建一个 Excel 工作簿,用 DataTable 的数据填充它,并使用 SaveAsCsv 方法将其导出为 CSV 文件。

IronXL 中的 'SaveAsCsv' 方法用于什么?

IronXL 中的 SaveAsCsv 方法用于将填充的 Excel 工作簿导出为 CSV 文件格式,便于数据共享和操作。

我在哪里可以找到 IronXL API 参考文档?

IronXL API 参考文档在他们的网站上提供,提供了有关如何使用库功能的详细信息,例如合并、拆分和处理 Excel 电子表格单元。

在 C# 中可以自动化 DataTable 到 CSV 的转换吗?

是的,通过在 C# 项目中使用 IronXL,您可以使用简单而有效的代码自动化数据表到 CSV 文件的转换过程,包括 SaveAsCsv 方法。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 1,686,155 | 版本: 2025.11 刚刚发布