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 on 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
VB   C#

Then, add the following code:

/**
Save a datatable to CSV
anchor-create-and-export-datatable-to-csv
**/
private void button6_Click(object sender, EventArgs e)
{
    DataTable table = new DataTable();
    table.Columns.Add("Example_DataSet", typeof(string));

    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");

    WorkBook wb = WorkBook.Create(ExcelFileFormat.XLS);
    wb.Metadata.Author = "OJ";
    WorkSheet ws = wb.DefaultWorkSheet;

    int rowCount = 1;
    foreach (DataRow row in table.Rows)
    {

        ws ["A" + (rowCount)].Value = row [0].ToString();

        rowCount++;
    }

    wb.SaveAsCsv("Save_DataTable_CSV.csv", ";"); // Saved as : Save_DataTable_CSV.Sheet1.csv
}
/**
Save a datatable to CSV
anchor-create-and-export-datatable-to-csv
**/
private void button6_Click(object sender, EventArgs e)
{
    DataTable table = new DataTable();
    table.Columns.Add("Example_DataSet", typeof(string));

    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");

    WorkBook wb = WorkBook.Create(ExcelFileFormat.XLS);
    wb.Metadata.Author = "OJ";
    WorkSheet ws = wb.DefaultWorkSheet;

    int rowCount = 1;
    foreach (DataRow row in table.Rows)
    {

        ws ["A" + (rowCount)].Value = row [0].ToString();

        rowCount++;
    }

    wb.SaveAsCsv("Save_DataTable_CSV.csv", ";"); // Saved as : Save_DataTable_CSV.Sheet1.csv
}
'''
'''Save a datatable to CSV
'''anchor-create-and-export-datatable-to-csv
'''*
Private Sub button6_Click(ByVal sender As Object, ByVal e As EventArgs)
	Dim table As New DataTable()
	table.Columns.Add("Example_DataSet", GetType(String))

	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")

	Dim wb As WorkBook = WorkBook.Create(ExcelFileFormat.XLS)
	wb.Metadata.Author = "OJ"
	Dim ws As WorkSheet = wb.DefaultWorkSheet

	Dim rowCount As Integer = 1
	For Each row As DataRow In table.Rows

		ws ("A" & (rowCount)).Value = row (0).ToString()

		rowCount += 1
	Next row

	wb.SaveAsCsv("Save_DataTable_CSV.csv", ";") ' Saved as : Save_DataTable_CSV.Sheet1.csv
End Sub
VB   C#

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