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#
Create CSV Parser for .NET
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
What is this C# library for handling Excel files?
IronXL is a C# library that simplifies working with Excel files, including creating CSV parsers without using Interop.
How do I install the C# library 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 the C# library?
Use the WorkBook object's Load method in IronXL 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?
You can export the parsed data into a new CSV file using IronXL's 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 the C# library?
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 this C# library 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 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 the C# library?
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 in IronXL to parse and export CSV files. Review the current version of IronXL for any updates that might affect the code.
How does the C# library 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.