How to use C# to convert a data table to CSV

Published: 2022-02-03 08:10:46

---
canonical_url: https://ironsoftware.com/csharp/excel/docs/questions/csharp-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/](https://ironsoftware.com/csharp/excel/docs/)

or

- In Visual Studio select the Project menu
- Click Manage NuGet Packages
- Search for IronXL.Excel
- Click Install

`PM> Install-Package IronPdf`

![Image 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ca9wmi4vf7fuz7xrblv.png)

**Figure 1** - _Iron.XL.Excel NuGet Package_

---

**How to Tutorial**

## 2. Create and Export Datatable to CSV

Now you are ready.

First import the IronXL namespace

**C#:**
```
using IronXL;
```

**VB:**
```
Imports IronXL
```

Then, add the following code:

**C#:**
```
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
}
```

**VB:**
```
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
```

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.

You can download the software product from this [link](https://ironsoftware.com/csharp/excel/downloads/csharp-datatable-excel.zip).

The output Excel Worksheet looks as follows:

![Image 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h97xptat0av3lm3v9xwk.png)

**Figure 2** - _Datatable output to CSV_

---