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
$vbLabelText   $csharpLabel

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
$vbLabelText   $csharpLabel

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
Kannaopat Udonpant

Kannapat Udonpant

Software Engineer

 LinkedIn

Before becoming a Software Engineer, Kannapat completed a Environmental Resources PhD from Hokkaido University in Japan. While pursuing his degree, Kannapat also became a member of the Vehicle Robotics Laboratory, which is part of the Department of Bioproduction Engineering. In 2022, he leveraged his C# skills to join Iron Software's engineering team, where he focuses on IronPDF. Kannapat values his job because he learns directly from the developer who writes most of the code used in IronPDF. In addition to peer learning, Kannapat enjoys the social aspect of working at Iron Software. When he's not writing code or documentation, Kannapat can usually be found gaming on his PS5 or rewatching The Last of Us.
< PREVIOUS
How to Generate an Excel File on Razor Pages
NEXT >
Read CSV File Using C# (Code Example Tutorial)