How to Use C# to Convert Datatable to CSV

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.


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;
// This function demonstrates how to create a DataTable and export it to a CSV file using IronXL.
private void button6_Click(object sender, EventArgs e)
{
    // 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
}
$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

Frequently Asked Questions

How can I convert a DataTable to CSV in C#?

You can use IronXL to convert a DataTable to CSV in C# by first installing the IronXL library, creating and populating a DataTable, and then using the SaveAsCsv method to export the data to a CSV file.

What are the steps to convert a DataTable to CSV using IronXL?

First, install the IronXL library via NuGet or from their website. Import the IronXL namespace, create and populate a DataTable, and then create an Excel workbook to hold the data. Finally, use the SaveAsCsv method to export the workbook as a CSV file.

Do I need Microsoft Excel installed to use IronXL for converting DataTable to CSV?

No, you do not need Microsoft Excel installed. IronXL allows you to convert a DataTable to CSV without relying on Excel Interop.

How do I install IronXL in my C# project?

You can install IronXL by downloading it from their website or through the NuGet Package Manager in Visual Studio. Search for 'IronXL.Excel' and click Install.

Can you provide a code example for converting a DataTable to CSV with IronXL?

Certainly! First, create and populate a DataTable. Then, import the IronXL namespace, create an Excel workbook, fill it with data from the DataTable, and use the SaveAsCsv method to export it as a CSV file.

What is the 'SaveAsCsv' method used for in IronXL?

The SaveAsCsv method in IronXL is used to export a populated Excel workbook to a CSV file format, allowing easy data sharing and manipulation.

Where can I find the IronXL API Reference Documentation?

The IronXL API Reference Documentation is available on their website, providing detailed information on how to use the library's functionalities, such as merging, unmerging, and working with Excel spreadsheet cells.

Is it possible to automate DataTable to CSV conversion in C#?

Yes, by using IronXL in your C# project, you can automate the process of converting DataTables to CSV files using simple and efficient code, including the SaveAsCsv method.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.