How to Read CSV files in C# using IronXL

In this tutorial, we explore how to read CSV files in C# using the IronXL library, a free tool for development. The process begins with installing IronXL via the package manager console. After installation, we integrate it into our project by adding the necessary namespace. We then demonstrate reading data from a CSV file using the workbook.LoadCSV method, specifying the file path and name. By accessing the default worksheet, we can loop through the data cells, extracting values from a specified range (A1 to A10) and displaying them in the console.

Additionally, the tutorial covers performing aggregation functions on the data, such as calculating the sum and minimum values within the range. This is achieved using LINQ with Enumerable.Sum and Enumerable.Min functions, showcasing the flexibility and power of IronXL for data manipulation. The tutorial concludes with a successful demonstration of these operations, encouraging users to reach out to the support team for further assistance if needed.

Here is an example code snippet that demonstrates how to achieve these tasks:

// Make sure to install IronXL via NuGet package manager before running this code.
// This namespace is necessary for the IronXL library functionalities.
using IronXL;
using System;
using System.Linq;

class CSVReader
{
    static void Main()
    {
        // Load the CSV file into a workbook. Replace 'yourfile.csv' with your actual file path.
        WorkBook workbook = WorkBook.LoadCSV("yourfile.csv");

        // Access the default worksheet.
        WorkSheet sheet = workbook.DefaultWorkSheet;

        // Define the range to read from (A1 to A10).
        var range = sheet["A1:A10"];

        // Iterate over the range and print the cell values.
        Console.WriteLine("Reading values from the CSV file:");
        foreach (var cell in range)
        {
            Console.WriteLine(cell.Text);
        }

        // Perform aggregation functions using LINQ.
        Console.WriteLine("\nPerforming aggregation functions on the selected range:");

        // Convert the cell range values to decimal and calculate the sum.
        decimal sum = range.Select(cell => Convert.ToDecimal(cell.Text)).Sum();

        // Find the minimum value in the range.
        decimal min = range.Select(cell => Convert.ToDecimal(cell.Text)).Min();

        // Display aggregation results.
        Console.WriteLine($"Sum: {sum}");
        Console.WriteLine($"Minimum Value: {min}");
    }
}
// Make sure to install IronXL via NuGet package manager before running this code.
// This namespace is necessary for the IronXL library functionalities.
using IronXL;
using System;
using System.Linq;

class CSVReader
{
    static void Main()
    {
        // Load the CSV file into a workbook. Replace 'yourfile.csv' with your actual file path.
        WorkBook workbook = WorkBook.LoadCSV("yourfile.csv");

        // Access the default worksheet.
        WorkSheet sheet = workbook.DefaultWorkSheet;

        // Define the range to read from (A1 to A10).
        var range = sheet["A1:A10"];

        // Iterate over the range and print the cell values.
        Console.WriteLine("Reading values from the CSV file:");
        foreach (var cell in range)
        {
            Console.WriteLine(cell.Text);
        }

        // Perform aggregation functions using LINQ.
        Console.WriteLine("\nPerforming aggregation functions on the selected range:");

        // Convert the cell range values to decimal and calculate the sum.
        decimal sum = range.Select(cell => Convert.ToDecimal(cell.Text)).Sum();

        // Find the minimum value in the range.
        decimal min = range.Select(cell => Convert.ToDecimal(cell.Text)).Min();

        // Display aggregation results.
        Console.WriteLine($"Sum: {sum}");
        Console.WriteLine($"Minimum Value: {min}");
    }
}
' Make sure to install IronXL via NuGet package manager before running this code.
' This namespace is necessary for the IronXL library functionalities.
Imports Microsoft.VisualBasic
Imports IronXL
Imports System
Imports System.Linq

Friend Class CSVReader
	Shared Sub Main()
		' Load the CSV file into a workbook. Replace 'yourfile.csv' with your actual file path.
		Dim workbook As WorkBook = WorkBook.LoadCSV("yourfile.csv")

		' Access the default worksheet.
		Dim sheet As WorkSheet = workbook.DefaultWorkSheet

		' Define the range to read from (A1 to A10).
		Dim range = sheet("A1:A10")

		' Iterate over the range and print the cell values.
		Console.WriteLine("Reading values from the CSV file:")
		For Each cell In range
			Console.WriteLine(cell.Text)
		Next cell

		' Perform aggregation functions using LINQ.
		Console.WriteLine(vbLf & "Performing aggregation functions on the selected range:")

		' Convert the cell range values to decimal and calculate the sum.
		Dim sum As Decimal = range.Select(Function(cell) Convert.ToDecimal(cell.Text)).Sum()

		' Find the minimum value in the range.
		Dim min As Decimal = range.Select(Function(cell) Convert.ToDecimal(cell.Text)).Min()

		' Display aggregation results.
		Console.WriteLine($"Sum: {sum}")
		Console.WriteLine($"Minimum Value: {min}")
	End Sub
End Class
$vbLabelText   $csharpLabel

This code snippet loads a CSV file into a workbook and reads values from a specified cell range. It then performs aggregation operations like calculating the sum and minimum value of the range using LINQ and showcases how to interact with the IronXL library efficiently.

Regan Pun
Software Engineer
Regan graduated from the University of Reading, with a BA in Electronic Engineering. Before joining Iron Software, his previous job roles had him laser-focused on single tasks; and what he most enjoys at Iron Software is the spectrum of work he gets to undertake, whether it’s adding value to sales, technical support, product development or marketing. He enjoys understanding the way developers are using the Iron Software library, and using that knowledge to continually improve documentation and develop the products.
< PREVIOUS
A Comparison Between IronXL and ClosedXML
NEXT >
How to Read Excel Data and Insert to Database Table in C#