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.

Reading CSV Files in .NET Applications

  • Install a C# library for Reading CSV Files (IronXL)
  • Read CSV files in C#
  • Specify file format and delimiter
How To Work related to Read a CSV File in C#

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#

:path=/static-assets/excel/content-code-examples/how-to/csharp-read-csv-read.cs
using IronXL;
// 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");
}
$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 can I read a CSV file in C# without using Interop?

You can read a CSV file in C# without using Interop by utilizing the IronXL library. First, install IronXL via the NuGet Package Manager in Visual Studio. Then, use the WorkBook.LoadCSV method to load the CSV file into a WorkBook object.

What steps are involved in reading a CSV file using IronXL in C#?

To read a CSV file using IronXL, first install the IronXL library. Then, create a new C# project, add the using IronXL; namespace, and use the WorkBook.LoadCSV method to load and manipulate the CSV file data.

Can IronXL handle CSV files with different delimiters?

Yes, IronXL allows you to specify different delimiters when reading CSV files. You can adjust the delimiter by setting it in the LoadCSV method options.

How do I save a CSV file as an Excel file in C#?

After loading the CSV file into a WorkBook object with IronXL, you can use the SaveAs method to save it as an Excel file in your desired format, such as XLSX.

Why use IronXL instead of Microsoft Interop for CSV operations in C#?

IronXL is preferred over Microsoft Interop for CSV operations in C# because it does not require Excel to be installed on the server. It provides a more efficient and simpler way to handle CSV files within C# applications.

How do I specify the worksheet to manipulate in a CSV file using IronXL?

When you load a CSV file using IronXL, it is stored in a WorkBook object. You can access and manipulate the data using the default WorkSheet object, which represents the contents of the CSV file.

Is it possible to integrate CSV reading functionalities into ASP.NET applications using IronXL?

Yes, you can integrate CSV reading functionalities into ASP.NET or .NET Core applications using IronXL by installing the library and utilizing its methods to load, read, and manipulate CSV data.

Where can I find the IronXL API Reference Documentation?

The IronXL API Reference Documentation is available on the Iron Software website. It provides detailed information on all available methods, including those for reading and manipulating CSV files.

What is the purpose of the LoadCSV method in IronXL?

The LoadCSV method in IronXL is used to import CSV data into a WorkBook object, allowing you to manipulate and interact with the data as if it were in an Excel format.

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.