Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
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
In the above code:
WorkBook.Load
by specifying the path to the file.workbook.WorkSheets.First()
.Further Reading: How to Read Excel Files in C# (Developer Tutorial)