DEV Community

IronSoftware
IronSoftware

Posted on • Originally published at ironsoftware.com

How to use C# to convert a data table 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

PM> Install-Package IronPdf

Image 1

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;
Enter fullscreen mode Exit fullscreen mode

VB:

Imports IronXL
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

The output Excel Worksheet looks as follows:

Image 2

Figure 2 - Datatable output to CSV


Top comments (2)

Collapse
 
jayjeckel profile image
Jay Jeckel

This article doesn't explain how to convert a DataTable to CSV, it explains how to download a library to convert a DataTable to Excel worksheet and convert that worksheet to CSV.

In other words, this article doesn't at all provide what is promised by the title.

The sad part is, this isn't that complicated of a thing to do and definitely doesn't require a separate library and allocation of an intermediate excel worksheet. Proper solutions can be found with a simple googling (eg stackoverflow), but the gist is easy to lay out.

  1. Join the table's column names with a comma delimiter and write line to file.
  2. Join each row's items with a comma delimiter and write line to file.

Boom. Simple as that. No unneeded excel object or external dependency.

Collapse
 
ironsoftware profile image
IronSoftware

You should write an article on it then :)