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.
How to Convert DataTable to CSV in C#
- Download and install convert DataTable to CSV C# Library
- Utilize
DataTable
class to store new data - Create new or import existing spreadsheet
- Loop through table rows and populate cells in worksheet accordingly
- Export to CSV file with
SaveAsCsv
C# function
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
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
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
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:
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