Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
The CSV (Comma Separated Values) file format is primarily used for data exchange between different platforms. It is a common file extension used with spreadsheets and is a plain text file that stores data separated by commas. Many websites provide data for download in CSV format because it is easy and does not require complex formatting. Importing and exporting data in CSV format has become an essential part of software applications.
However, reading CSV files in programs can be challenging at times due to different delimiters, quotes, or new lines used. C# developers may need to integrate a CSV reader in their software applications as businesses often require importing and exporting data in this format for analysis purposes.
In this article, we will explore how to create a C# CSVReader using the IronXL library.
IronXL is a powerful Excel library that provides C# developers with the ability to create, load, read, and edit Excel spreadsheets in various formats. Designed specifically for .NET, IronXL prioritizes speed, accuracy, and ease of use. It allows for saving Excel files in different formats and loading various spreadsheet formats into Excel for efficient data reading.
IronXL supports Excel workbook formats with different file extensions, including CSV and TSV, XLS and XLSX, XSLT and XLSM. It is compatible with the latest version of the .NET Framework, as well as all previous versions up to 2.0. IronXL can be used on various platforms, including Linux, MacOS, Azure, Docker, and AWS.
To convert data from CSV file to Excel file and read it in C#, we require following tools:
Create Console App: Follow the steps to create a simple console application.
Open Visual Studio and click "Create a Project".
Choose "console app" from the list of options available. Make sure the language selected in C#.
Next, name your project whatever you want.
IronXL - It is the .NET Excel library which is designed to work with Excel spreadsheets. It must be installed in your C# application before using it. You can download and install the package from multiple sources:
Once you have the prerequisites, we need to add an assembly reference for using IronXL in our program. Add the following namepsace on top of the source code in the main.cs file:
using IronXL;
using IronXL;
Imports IronXL
IronXL provides an easy way to read CSV files in C#. First we need to open a CSV file for reading. It is a file type which is based on rows and columns. Here, we will use the WorkBook
class along with its LoadCSV
method to open a CSV file. The code is as follows:
var csv = WorkBook.LoadCSV("color_srgb.csv", fileFormat: ExcelFileFormat.XLSX, ListDelimiter: ",");
var csv = WorkBook.LoadCSV("color_srgb.csv", fileFormat: ExcelFileFormat.XLSX, ListDelimiter: ",");
Dim csv = WorkBook.LoadCSV("color_srgb.csv", fileFormat:= ExcelFileFormat.XLSX, ListDelimiter:= ",")
The LoadCSV
method in IronXL allows you to open a CSV file and convert it to .xlsx format. This method provides options to specify the list delimiter used in the CSV file. In this example, we are using the default delimiter, which is a comma. The resulting file is opened and can be further processed using IronXL's features for reading and manipulating Excel spreadsheets.
In the previous step, the CSV file was opened as an Excel workbook using IronXL. Now, we will get the default worksheet from the workbook using the WorkSheet
class. The following example demonstrates how to get the worksheet for reading CSV data:
WorkSheet ws = workbook.DefaultWorkSheet;
WorkSheet ws = workbook.DefaultWorkSheet;
Dim ws As WorkSheet = workbook.DefaultWorkSheet
For more information on working with excel worksheets, you can visit this code examples page.
Once the CSV is loaded successfully and the data is available as a worksheet, we can then read data from the CSV file very easily in a C# DataTable.
First, we will create a DataTable
instance and get convert the worksheet data to a table using the ToDataTable
method. The following code helps to achieve this task:
DataTable dt = ws.ToDataTable(true);
DataTable dt = ws.ToDataTable(true);
Dim dt As DataTable = ws.ToDataTable(True)
Now, we can iterate through all the records using the DataTable
instance. The data is received in rows and columns. First we will iterate through rows and then move through each column to get its value. To get all the records along with header row, use the following code snippet:
foreach (DataRow row in dt.Rows) //access rows
{
for (int i = 0; i < dt.Columns.Count; i++) //access columns of corresponding row
{
Console.Write(row[i] + " "); //format output
}
Console.WriteLine();
}
foreach (DataRow row in dt.Rows) //access rows
{
for (int i = 0; i < dt.Columns.Count; i++) //access columns of corresponding row
{
Console.Write(row[i] + " "); //format output
}
Console.WriteLine();
}
For Each row As DataRow In dt.Rows 'access rows
For i As Integer = 0 To dt.Columns.Count - 1 'access columns of corresponding row
Console.Write(row(i) & " ") 'format output
Next i
Console.WriteLine()
Next row
In the above code, a foreach
loop is used to get a single record from a collection of rows. Then inside a nested for loop, the number of columns is counted and finally the data from each row is printed on the screen. The output is formatted similar to a CSV file.
In this article, we learned how to create our own CSV reader in C# using the IronXL library. Loading CSV files is made easy with IronXL as it supports the CSV format in Excel spreadsheets. We utilized C# DataTable
to create an elegant CSV reader and formatted the output to match the original file.
IronXL also provides convenient conversion between different file formats and allows for creating Excel files from scratch without requiring Interop and MS Excel to be installed. It is also compatible with C# DataSet
and DataTable
, providing developers with flexibility in interconverting data without relying on third-party applications. With its powerful features, IronXL is a valuable tool for C# developers to work with Excel spreadsheets and handle CSV data efficiently.
IronXL is free for development. However, for commercial use you need to purchase its license starting from $599. You can also use its free trial to test its compiled .dll compatibility with your project in production mode.
9 .NET API products for your office documents