USING IRONXL

How to Use a CSV Parser in C#

Updated January 29, 2024
Share:

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

  • [c# csv parser] Please help check correct of this article if code example is correct. Article is old
  • How to use CSV Parser in C#

Step 1

1. Download IronXL

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

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:

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 Excel into CSV File

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.

private void button4_Click(object sender, EventArgs e)
{
    // Import XLSX, XLS, XLSM, XLTX, CSV and TSV
    WorkBook workBook = WorkBook.Load("Normal_Excel_File.xlsx");

    //Exported as : Parsed_CSV.Sheet1.csv
    workBook.SaveAs("Parsed_CSV.csv");
}
private void button4_Click(object sender, EventArgs e)
{
    // Import XLSX, XLS, XLSM, XLTX, CSV and TSV
    WorkBook workBook = WorkBook.Load("Normal_Excel_File.xlsx");

    //Exported as : Parsed_CSV.Sheet1.csv
    workBook.SaveAs("Parsed_CSV.csv");
}
Private Sub button4_Click(ByVal sender As Object, ByVal e As EventArgs)
	' Import XLSX, XLS, XLSM, XLTX, CSV and TSV
	Dim workBook As WorkBook = WorkBook.Load("Normal_Excel_File.xlsx")

	'Exported as : Parsed_CSV.Sheet1.csv
	workBook.SaveAs("Parsed_CSV.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

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
Documentation related to Library Quick Access
< PREVIOUS
How to Generate an Excel File on Razor Pages
NEXT >
Read CSV File Using C# (Code Example Tutorial)

Ready to get started? Version: 2024.12 just released

Free NuGet Download Total downloads: 1,132,445 View Licenses >