USING IRONXL

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, convert and export it to Excel.

  • [c# csv parser] Please help check correctness of this article if the code example is accurate. The 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

Alternatively, you can also use the Developer Command Prompt:

dotnet add 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 C# project and add the IronXL namespace to access its functionality:

using IronXL;
using IronXL;
Imports IronXL
$vbLabelText   $csharpLabel

3. Load an 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 various Excel file formats, such as XLSX, XLS, XLSM, XLTX, and CSV
    WorkBook workBook = WorkBook.Load("Normal_Excel_File.xlsx");

    // Export the loaded workbook to a CSV file named Parsed_CSV.csv
    workBook.SaveAs("Parsed_CSV.csv");
}
private void button4_Click(object sender, EventArgs e)
{
    // Import various Excel file formats, such as XLSX, XLS, XLSM, XLTX, and CSV
    WorkBook workBook = WorkBook.Load("Normal_Excel_File.xlsx");

    // Export the loaded workbook to a CSV file named Parsed_CSV.csv
    workBook.SaveAs("Parsed_CSV.csv");
}
Private Sub button4_Click(ByVal sender As Object, ByVal e As EventArgs)
	' Import various Excel file formats, such as XLSX, XLS, XLSM, XLTX, and CSV
	Dim workBook As WorkBook = WorkBook.Load("Normal_Excel_File.xlsx")

	' Export the loaded workbook to a CSV file named Parsed_CSV.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

Frequently Asked Questions

What is IronXL?

IronXL is a C# library that simplifies working with Excel files, including creating CSV parsers without using Interop.

How do I install IronXL for creating a CSV parser?

You can install IronXL via the NuGet Package Manager in Visual Studio by searching for 'IronXL.Excel' or use the command 'dotnet add package IronXL.Excel' in the Developer Command Prompt.

How can I load a CSV file using IronXL?

Use the WorkBook object's Load method to load a CSV file into Excel, which allows you to inspect and modify the data in C#.

How do I export a parsed Excel file to CSV using IronXL?

You can export the parsed data into a new CSV file using the SaveAs method, which will save each Excel sheet into a separate CSV file.

What are the steps to create a CSV parser in .NET using IronXL?

First, download and install IronXL. Create a new C# project and add the IronXL namespace. Load your CSV file into Excel using the Load method, then parse and export it using the SaveAs method.

Can I use IronXL for free?

IronXL is free to use in development and debugging environments, allowing you to test its functionality before purchasing.

Where can I find the IronXL API Reference Documentation?

The IronXL API Reference Documentation is available on the Iron Software website, offering detailed information on using the library's features.

What file formats can be loaded using IronXL?

IronXL can load various Excel file formats, including XLSX, XLS, XLSM, XLTX, and CSV.

Is the code example in the article correct for parsing a CSV file?

The article provides a code example that uses the Load and SaveAs methods of the WorkBook object to parse and export CSV files. Review the current version of IronXL for any updates that might affect the code.

How does IronXL handle multiple sheets in an Excel workbook when exporting to CSV?

When exporting an Excel workbook with multiple sheets to CSV, IronXL saves each sheet as a separate CSV file.

Regan Pun
Software Engineer
Regan graduated from the University of Reading, with a BA in Electronic Engineering. Before joining Iron Software, his previous job roles had him laser-focused on single tasks; and what he most enjoys at Iron Software is the spectrum of work he gets to undertake, whether it’s adding value to sales, technical support, product development or marketing. He enjoys understanding the way developers are using the Iron Software library, and using that knowledge to continually improve documentation and develop the products.
< PREVIOUS
How to Generate an Excel File on Razor Pages
NEXT >
Read CSV File Using C# (Code Example Tutorial)