Read a CSV File in C#

When you need to read CSV files in C#, IronXL is an easy answer. You can read a CSV file with commas, or any other delimiter, as seen in the code segments below.


Step 1

1. Install the IronXL Library

Before you can make use of IronXL to read CSV files in MVC or ASP or dotnet core, you need to install it first. Here is a quick walk-through.

  • In Visual Studio, select the Project menu
  • Manage NuGet Packages
  • Search for IronXL.Excel
  • Install
IronXL.Excel NuGet Package
Figure 1 - IronXL.Excel NuGet Package

Or Download from the Iron Software website, here: https://ironsoftware.com/csharp/excel/packages/IronXL.zip


How to Tutorial

2. Read CSV Files Programmatically

Now for the project!

Add the IronXL Namespace

// This namespace is required to access the IronXL functionalities
using IronXL;
// This namespace is required to access the IronXL functionalities
using IronXL;
' This namespace is required to access the IronXL functionalities
Imports IronXL
$vbLabelText   $csharpLabel

Add code to read a CSV file programmatically with IronXL and C#

// Method to handle the button click event for reading a CSV file
private void button2_Click(object sender, EventArgs e)
{
    // Load the CSV file into a WorkBook object, specifying the file path, format, and delimiter
    WorkBook workbook = WorkBook.LoadCSV("Read_CSV_Ex.csv", fileFormat: ExcelFileFormat.XLSX, ListDelimiter: ",");

    // Access the default worksheet within the loaded workbook
    WorkSheet ws = workbook.DefaultWorkSheet;

    // Save the workbook as an Excel file with a specified name
    workbook.SaveAs("Csv_To_Excel.xlsx");
}
// Method to handle the button click event for reading a CSV file
private void button2_Click(object sender, EventArgs e)
{
    // Load the CSV file into a WorkBook object, specifying the file path, format, and delimiter
    WorkBook workbook = WorkBook.LoadCSV("Read_CSV_Ex.csv", fileFormat: ExcelFileFormat.XLSX, ListDelimiter: ",");

    // Access the default worksheet within the loaded workbook
    WorkSheet ws = workbook.DefaultWorkSheet;

    // Save the workbook as an Excel file with a specified name
    workbook.SaveAs("Csv_To_Excel.xlsx");
}
' Method to handle the button click event for reading a CSV file
Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)
	' Load the CSV file into a WorkBook object, specifying the file path, format, and delimiter
	Dim workbook As WorkBook = WorkBook.LoadCSV("Read_CSV_Ex.csv", fileFormat:= ExcelFileFormat.XLSX, ListDelimiter:= ",")

	' Access the default worksheet within the loaded workbook
	Dim ws As WorkSheet = workbook.DefaultWorkSheet

	' Save the workbook as an Excel file with a specified name
	workbook.SaveAs("Csv_To_Excel.xlsx")
End Sub
$vbLabelText   $csharpLabel
A CSV file opened in Notepad
Figure 2 - A CSV file opened in Notepad

A Workbook object is created. The LoadCSV method of the Workbook object is then used to specify the name of the CSV file to be read, the format to read it into, and what the delimiter is for the file. In this case, a comma is used as a separator.

A Worksheet object is then created. This is where the contents of the CSV file will be placed. Then the file is saved under a new name and format.

The CSV file opened in Excel
Figure 3 - The CSV file opened in Excel

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 2. Read CSV Files Programmatically

Frequently Asked Questions

How do I install the IronXL library in C#?

To install the IronXL library in C#, open Visual Studio, select the Project menu, then Manage NuGet Packages. Search for IronXL.Excel and install it.

Can I use IronXL to read CSV files with delimiters other than commas?

Yes, IronXL allows you to specify any delimiter for reading CSV files, not just commas.

What is the first step to read a CSV file using IronXL in C#?

The first step is to download and install the IronXL library for C#.

How do I specify the file format when reading a CSV file with IronXL?

You can specify the file format by using the WorkBook.LoadCSV method and setting the fileFormat parameter, such as ExcelFileFormat.XLSX.

What namespace is required to access IronXL functionalities?

You need to add the 'using IronXL;' namespace to access IronXL functionalities in your C# project.

How can I save a CSV file as an Excel file using IronXL?

After loading the CSV file into a WorkBook object, you can use the SaveAs method to save it as an Excel file with a specified name.

Where can I find the IronXL API Reference Documentation?

The IronXL API Reference Documentation is available on the Iron Software website, where you can learn about merging, unmerging, and working with cells in Excel spreadsheets.

What is the purpose of the WorkSheet object in IronXL?

The WorkSheet object is used to access and manipulate the contents of the CSV file once it is loaded into a WorkBook.

Is it possible to use IronXL in ASP.NET or .NET Core applications?

Yes, IronXL can be used to read CSV files in MVC, ASP.NET, and .NET Core applications after installation.

How do I handle CSV file reading events in a C# application with IronXL?

You can handle CSV file reading events using methods like button click events, where you specify file paths, formats, and delimiters to load and manipulate the CSV data.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.