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

您可以使用 IronXL 將 datatable 匯出到 CSV,只需幾個步驟即可將現有的數據表轉換為 CSV。 本文旨在向您展示一個快速的示例。

快速入門:一行導出 DataTable 到 CSV

使用 IronXL 即可通過簡單的一次調用輕鬆將填充的 DataTable 轉換為 CSV 文件——無需循環,無需 Interop,無需麻煩。 您只需要一個 W或者kBook 及其 DefaultW或者kSheet,即可使用 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.W或者kBook.Create().DefaultW或者kSheet.SaveAsCsv("output.csv", ",");
  3. Deploy to test on your live environment

    Start using IronXL in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最少工作流程(5 步)

  1. 下載並安裝將 DataTable 轉換為 CSV 的 C# 庫
  2. 使用 DataTable 類儲存新數據
  3. 建立新電子表格或匯入現有電子表格
  4. 循環遍歷表格行並相應地填充工作表中的單元格
  5. 使用 SaveAsCsv C# 函數匯出到 CSV 文件

class="main-content__segment-title">步驟 1

1. 添加 IronXL Free

您需要先安裝 IronXL 然後才能在應用程式中使用它。 幸運的是,他們提供了許多安裝 IronXL 到專案中的選項。

使用以下鏈接從他們的網站下載: https://ironsoftware.com/csharp/excel/docs/

或者

  • 在 Visual Studio 中,選擇專案菜單
  • 點擊管理 NuGet 包
  • Search f或者 IronXL.Excel
  • 點擊安裝
Install-Package IronXL.Excel
class="center-image-wrapper"> IronXL.Excel NuGet Package
class="image-description"> class="image-description-text_strong"> 圖 1 class="image-description-text_regular"> - class="image-description-text_italic"> IronXL.Excel NuGet Package

How to Tut或者ial

2. Create and Exp或者t Datatable to CSV

現在您已經準備好。

First, imp或者t the IronXL namespace.

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

然後,添加以下代碼:

: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 w或者kbook specifying ‘OJ’ as its owner/creat或者. A f或者each loop follows that inserts the data from the data table into the Excel W或者ksheet. Lastly, the SaveAsCsv method is used to exp或者t the datatable to CSV.

The output Excel W或者ksheet looks as follows:

class="center-image-wrapper"> Datatable output to CSV
class="image-description"> class="image-description-text_strong"> 圖 2 class="image-description-text_regular"> - class="image-description-text_italic"> Datatable output to CSV

Library Quick Access

IronXL API Reference Documentation

Learn m或者e and share how to merge, unmerge, and w或者k with cells in Excel spreadsheets using the handy IronXL API Reference Documentation.

IronXL API Reference Documentation
Documentation related to Library Quick Access

常見問題解答

如何在 C# 中將資料表轉換為 CSV?

您可以使用 IronXL 在 C# 中將資料表轉換為 CSV。首先安裝 IronXL 函式庫,創建和填充資料表,然後使用 SaveAsCsv 方法將數據導出為 CSV 文件。

使用 IronXL 將資料表轉換為 CSV 的步驟是什麼?

首先,通過 NuGet 或從他們的網站安裝 IronXL 函式庫。導入 IronXL 命名空間,創建和填充資料表,然後創建 Excel 工作簿以承載數據。最後,使用 SaveAsCsv 方法將工作簿導出為 CSV 文件。

我需要安裝 Microsoft Excel 才能使用 IronXL 將資料表轉換為 CSV 嗎?

不,您不需要安裝 Microsoft Excel。IronXL 允許您在不依賴 Excel Interop 的情況下將資料表轉換為 CSV。

如何在我的 C# 項目中安裝 IronXL?

您可以通過從他們的網站下載 IronXL 或通過 Visual Studio 的 NuGet 套件管理器安裝。搜索 'IronXL.Excel' 並點擊安裝。

您能提供一個使用 IronXL 將資料表轉換為 CSV 的代碼示例嗎?

當然!首先,創建和填充資料表。然後,導入 IronXL 命名空間,創建 Excel 工作簿,用資料表中的數據填充它,並使用 SaveAsCsv 方法將其導出為 CSV 文件。

IronXL 中的 'SaveAsCsv' 方法用於做什麼?

IronXL 中的 SaveAsCsv 方法用於將填充的 Excel 工作簿導出為 CSV 文件格式,方便數據分享和操作。

我在哪裡可以找到 IronXL API 參考文件?

IronXL API 參考文件可在他們的網站上找到,提供詳細資訊說明如何使用函式庫的功能,如合併、取消合併和處理 Excel 試算表單元格。

在 C# 中自動化資料表到 CSV 的轉換是否可能?

是的,通過在 C# 專案中使用 IronXL,您可以自動化將資料表轉換為 CSV 文件的過程,使用簡單而有效的代碼,包括 SaveAsCsv 方法。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 1,686,155 | 版本: 2025.11 剛剛發布