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.
Steps to Read a CSV File in C
- Download and install Read CSV in C# Library
- Add code to read a CSV file programmatically
- Specify the name of the CSV file to be read
- File saved as a new name and format
C#:
private void button2_Click(object sender, EventArgs e)
{
WorkBook workbook = WorkBook.LoadCSV("Read_CSV_Ex.csv", fileFormat: ExcelFileFormat.XLSX, ListDelimiter: ",");
WorkSheet ws = workbook.DefaultWorkSheet;
workbook.SaveAs("Csv_To_Excel.xlsx");
}
VB:
Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim workbook As WorkBook = WorkBook.LoadCSV("Read_CSV_Ex.csv", fileFormat:= ExcelFileFormat.XLSX, ListDelimiter:= ",")
Dim ws As WorkSheet = workbook.DefaultWorkSheet
workbook.SaveAs("Csv_To_Excel.xlsx")
End Sub
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

Figure 1 - IronXL.Excel NuGet Package
Or Download from the Iron Software website, here:
How to Tutorial
2. Read CSV Files Programmatically
Now for the project!
Add the IronXL Namespace
C#:
using IronXL;
VB:
Imports IronXL
Add code to read a CSV file programmatically with IronXL and C#
C#:
private void button2_Click(object sender, EventArgs e)
{
WorkBook workbook = WorkBook.LoadCSV("Read_CSV_Ex.csv", fileFormat: ExcelFileFormat.XLSX, ListDelimiter: ",");
WorkSheet ws = workbook.DefaultWorkSheet;
workbook.SaveAs("Csv_To_Excel.xlsx");
}
VB:
Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim workbook As WorkBook = WorkBook.LoadCSV("Read_CSV_Ex.csv", fileFormat:= ExcelFileFormat.XLSX, ListDelimiter:= ",")
Dim ws As WorkSheet = workbook.DefaultWorkSheet
workbook.SaveAs("Csv_To_Excel.xlsx")
End Sub

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, in 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.

Figure 3 - The CSV file opened in Excel
Top comments (0)