How to Use a CSV Parser in C#

It's simple to create a CSV parser. With only two lines of code you can load a CSV document and convert it to Excel.


Step 1

1. Download IronXL

Before I show you how to create a CSV parser, we'll first install IronXL to your project (free for development).

You can download directly from the Iron Software website 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

Simply, you can also use the Developer Command Prompt:

PM> Install-Package IronXL.Excel
IronXL.Excel NuGet Package
Figure 1 - IronXL.Excel NuGet Package

How to Tutorial

2. Create a New Project

After you have installed IronXL, create a new project and add the IronXL namespace

using IronXL;
using IronXL;
Imports IronXL
VB   C#

3. Load a CSV File into Excel

The following code uses the Workbook object’s Load method to load a CSV file into Excel. This file is then parsed. Lastly, it uses the SaveAs method to save the file in the csv format.

/**
Parse CSV File
anchor-load-a-csv-file-into-excel
**/
private void button4_Click(object sender, EventArgs e)
{
    WorkBook wb = WorkBook.Load("Normal_Excel_File.xlsx"); //Import .xls, .csv, or .tsv file
    wb.SaveAs("Parsed_CSV.csv"); //Exported as : Parsed_CSV.Sheet1.csv
}
/**
Parse CSV File
anchor-load-a-csv-file-into-excel
**/
private void button4_Click(object sender, EventArgs e)
{
    WorkBook wb = WorkBook.Load("Normal_Excel_File.xlsx"); //Import .xls, .csv, or .tsv file
    wb.SaveAs("Parsed_CSV.csv"); //Exported as : Parsed_CSV.Sheet1.csv
}
'''
'''Parse CSV File
'''anchor-load-a-csv-file-into-excel
'''*
Private Sub button4_Click(ByVal sender As Object, ByVal e As EventArgs)
	Dim wb As WorkBook = WorkBook.Load("Normal_Excel_File.xlsx") 'Import .xls, .csv, or .tsv file
	wb.SaveAs("Parsed_CSV.csv") 'Exported as : Parsed_CSV.Sheet1.csv
End Sub
VB   C#

Don’t forget to create an Excel Workbook named Normal_Excel_File.xlsx containing the following data:

Excel data to be converted to CSV
Figure 2 - Excel data to be converted to CSV

4. Export the Parsed CSV

Interestingly, the exported CSV file will be saved as Parsed_CSV.Sheet1.csv because the data is on Sheet1 inside of the Excel Workbook. Below is what the file would look like in File Explorer when selected.

Parsed_CSV.Sheet1.csv in File Explorer
Figure 3 - Parsed_CSV.Sheet1.csv in File Explorer

You can download the software product from this link.


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