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#
How to use CSV Parser in C#
- Install C# library to parse CSV file
- Use
Load
method to parse various file formats including CSV - Inspect and modify the data in C#
- Export the data into a new CSV file
- Each Excel sheet will be exported into a separate CSV file
Create CSV Parser for .NET
- Download IronXL Free
- Create an Excel C# Project
- Load your CSV file into Excel
- Parse and export the file
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
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
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
Don’t forget to create an Excel Workbook named Normal_Excel_File.xlsx containing the following data:
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.
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 DocumentationFrequently Asked Questions
How can I parse a CSV file in C# without using Interop?
You can parse a CSV file in C# without using Interop by utilizing the IronXL library. Simply use the WorkBook.Load
method to load the CSV file and then handle the data within your application.
What are the benefits of using IronXL for CSV parsing in C#?
IronXL simplifies the process of CSV parsing by allowing you to work with Excel files directly in C#. It eliminates the need for complex Interop code and provides straightforward methods like Load
and SaveAs
for importing and exporting data.
How do I export an Excel file to CSV in C#?
To export an Excel file to CSV in C#, you can use IronXL's SaveAs
method. This method allows you to save each sheet of an Excel workbook as a separate CSV file.
What is the process to install the IronXL library in a C# project?
You can install the IronXL library in your C# project through the NuGet Package Manager in Visual Studio by searching for 'IronXL.Excel' or using the command dotnet add package IronXL.Excel
in the Developer Command Prompt.
Can I work with multiple Excel file formats using IronXL?
Yes, IronXL supports multiple Excel file formats including XLSX, XLS, XLSM, XLTX, and CSV, allowing you to load and manipulate these files seamlessly in C#.
Is there a free version of IronXL available for developers?
IronXL offers a free version that developers can use in development and debugging environments, enabling them to test the library's functionality before purchasing a full license.
Where can I access the IronXL API Reference Documentation?
The IronXL API Reference Documentation is available on Iron Software's website, providing detailed information and guidance on utilizing the library's comprehensive features.
How does IronXL handle multiple worksheets when converting Excel to CSV?
When converting an Excel workbook with multiple worksheets to CSV, IronXL will save each worksheet as a separate CSV file using the SaveAs
method.
Is it possible to parse and manipulate CSV data with minimal code in C#?
Yes, using IronXL, you can parse and manipulate CSV data with minimal code. The library simplifies the process with methods like Load
and SaveAs
, enabling efficient data handling in just a few lines of code.