DEV Community

IronSoftware
IronSoftware

Posted on • Originally published at ironsoftware.com

How to Read a CSV 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.

Steps to Read a CSV File in C

  1. Download and install Read CSV in C# Library
  2. Add code to read a CSV file programmatically
  3. Specify the name of the CSV file to be read
  4. 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");
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Image 1
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;
Enter fullscreen mode Exit fullscreen mode

VB:

Imports IronXL
Enter fullscreen mode Exit fullscreen mode

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");
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Image 2
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.

Image 3
Figure 3 - The CSV file opened in Excel


Top comments (0)