How to Read Excel Files in C# using IronXL

In this tutorial, we delve into reading Excel files in C# using the IronXL library, which is free for development use. We begin by installing the IronXL package through the package manager console. First, add the namespace 'using IronXL' to your code. Next, load the desired Excel workbook with the 'Workbook.Load' method, specifying the file path and name. Once loaded, you can select the worksheet using 'Workbook.GetWorksheet'. A loop is employed to read data from cells A1 to A10, displaying the values in the console. Additionally, the tutorial demonstrates using aggregate functions to calculate sum and max values from the data range using LINQ. The tutorial concludes by highlighting that similar methods can be employed for other aggregate functions like min and average. For further assistance, the support team is available to help users facing any challenges.

Code Example

// Import the IronXL namespace to use its functionalities
using IronXL;
using System;
using System.Linq;

class ExcelReader
{
   static void Main(string[] args)
   {
       // Load the Excel workbook from the specified file path
       WorkBook workbook = WorkBook.Load("path/to/your/excelFile.xlsx");

       // Select the first worksheet in the workbook
       WorkSheet sheet = workbook.WorkSheets.First();

       // Loop through the cells A1 to A10 and print their values to the console
       for (int row = 1; row <= 10; row++)
       {
           // Retrieve the value of the current cell
           string cellValue = sheet[$"A{row}"].StringValue;

           // Output the cell value to the console
           Console.WriteLine($"Value of A{row}: {cellValue}");
       }

       // Use LINQ to calculate the sum of values in cells A1 to A10
       double sum = sheet["A1:A10"].Select(cell => cell.DoubleValue).Sum();
       Console.WriteLine($"Sum of values in A1 to A10: {sum}");

       // Use LINQ to find the maximum value in cells A1 to A10
       double max = sheet["A1:A10"].Select(cell => cell.DoubleValue).Max();
       Console.WriteLine($"Maximum value in A1 to A10: {max}");
   }
}
// Import the IronXL namespace to use its functionalities
using IronXL;
using System;
using System.Linq;

class ExcelReader
{
   static void Main(string[] args)
   {
       // Load the Excel workbook from the specified file path
       WorkBook workbook = WorkBook.Load("path/to/your/excelFile.xlsx");

       // Select the first worksheet in the workbook
       WorkSheet sheet = workbook.WorkSheets.First();

       // Loop through the cells A1 to A10 and print their values to the console
       for (int row = 1; row <= 10; row++)
       {
           // Retrieve the value of the current cell
           string cellValue = sheet[$"A{row}"].StringValue;

           // Output the cell value to the console
           Console.WriteLine($"Value of A{row}: {cellValue}");
       }

       // Use LINQ to calculate the sum of values in cells A1 to A10
       double sum = sheet["A1:A10"].Select(cell => cell.DoubleValue).Sum();
       Console.WriteLine($"Sum of values in A1 to A10: {sum}");

       // Use LINQ to find the maximum value in cells A1 to A10
       double max = sheet["A1:A10"].Select(cell => cell.DoubleValue).Max();
       Console.WriteLine($"Maximum value in A1 to A10: {max}");
   }
}
' Import the IronXL namespace to use its functionalities
Imports IronXL
Imports System
Imports System.Linq

Friend Class ExcelReader
   Shared Sub Main(ByVal args() As String)
	   ' Load the Excel workbook from the specified file path
	   Dim workbook As WorkBook = WorkBook.Load("path/to/your/excelFile.xlsx")

	   ' Select the first worksheet in the workbook
	   Dim sheet As WorkSheet = workbook.WorkSheets.First()

	   ' Loop through the cells A1 to A10 and print their values to the console
	   For row As Integer = 1 To 10
		   ' Retrieve the value of the current cell
		   Dim cellValue As String = sheet($"A{row}").StringValue

		   ' Output the cell value to the console
		   Console.WriteLine($"Value of A{row}: {cellValue}")
	   Next row

	   ' Use LINQ to calculate the sum of values in cells A1 to A10
	   Dim sum As Double = sheet("A1:A10").Select(Function(cell) cell.DoubleValue).Sum()
	   Console.WriteLine($"Sum of values in A1 to A10: {sum}")

	   ' Use LINQ to find the maximum value in cells A1 to A10
	   Dim max As Double = sheet("A1:A10").Select(Function(cell) cell.DoubleValue).Max()
	   Console.WriteLine($"Maximum value in A1 to A10: {max}")
   End Sub
End Class
$vbLabelText   $csharpLabel

In the above code:

  • We load an Excel workbook using WorkBook.Load by specifying the path to the file.
  • We access the first worksheet using workbook.WorkSheets.First().
  • A loop is used to iterate over cells A1 through A10 to print their values to the console.
  • The LINQ queries are used to calculate the sum and maximum value in the range A1 to A10.

Further Reading: How to Read Excel Files in C# (Developer Tutorial)

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
How to Manage Excel Worksheets in C#
NEXT >
How to Write an Excel File in C#