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 article, we will discuss how you can get a specific cell's value from an Excel file using IronXL.
IronXL is a powerful software library for .NET developers that provides a simple and efficient way to read, write, and manipulate Excel spreadsheets in their applications. It offers a range of features that make working with Excel files easy and efficient, including the ability to create, modify, and delete worksheets, read and write cell data, and even perform complex calculations using Excel formulas. With IronXL, developers can quickly and easily incorporate Excel functionality into their applications, saving time and streamlining their development process. Whether you're building a finance application or data analysis tool, or simply need to read and write Excel files in your application, IronXL provides a flexible and reliable solution.
If you want to use the IronXL library to extract the value of a specific cell of an Excel file, you must fulfill certain prerequisites, which include:
To use the IronXL library for Excel-related tasks, you must first create a .NET project in Visual Studio. While any version of Visual Studio can be used, it is advisable to opt for the most recent version. There are multiple project templates to choose from, including Windows Forms and ASP.NET, depending on your specific needs. This tutorial will use the Console Application project template to illustrate how to work with IronXL.
Create a new project window
After selecting the project type, provide a name for the project and choose its location along with the desired framework for the project, such as .NET Core 6.
Project configuration
Once the solution is created, the program.cs file will be opened, enabling you to enter code and construct/run the application.
Project with code open
With this new Visual Studio project now created, let's install IronXL.
The IronXL Library can be downloaded and installed using a few different methods. But these two approaches are the simplest ones.
These are:
To install the IronXL library, navigate to the NuGet Package Manager in Visual Studio. Simply open the NuGet Package Manager and search for IronXL in the Browse tab. Once you have located IronXL in the search results, select it and proceed with the installation. Once the installation is complete, you can start using the IronXL library in your project.
The below screenshot shows how to open the NuGet Package Manager in Visual Studio.
NuGet Package Manager
The following shows IronXL in search results:
IronXL search result
Many developers prefer to install packages using a command line interface. To install IronXL using the command line, follow these steps:
Enter the following line in the Package Manager Console tab:
Install-Package IronXL.Excel
Install-Package IronXL.Excel
Now the package will be downloaded and installed to the current project and will be ready to use.
Installing via command line
Retrieving the data stored in a specific cell of an Excel worksheet using IronXL is an uncomplicated process that requires only a few lines of code. With this software library, developers can easily access the desired Excel cell value within their program. The following code example will demonstrate how to use IronXL to obtain the value property with a cell address.
using IronXL;
using System;
using System.Linq;
class Program
{
static void Main()
{
// Load the Excel workbook
WorkBook workBook = WorkBook.Load("sample.xlsx");
// Access the first worksheet
WorkSheet workSheet = workBook.WorkSheets.First();
// Define a range
var range = workSheet["B2:B2"]; // This specifies the cell range to read
// Get the value stored in cell B2
foreach (var cell in range)
{
Console.WriteLine($"Value in B2: {cell.Value}");
}
}
}
using IronXL;
using System;
using System.Linq;
class Program
{
static void Main()
{
// Load the Excel workbook
WorkBook workBook = WorkBook.Load("sample.xlsx");
// Access the first worksheet
WorkSheet workSheet = workBook.WorkSheets.First();
// Define a range
var range = workSheet["B2:B2"]; // This specifies the cell range to read
// Get the value stored in cell B2
foreach (var cell in range)
{
Console.WriteLine($"Value in B2: {cell.Value}");
}
}
}
Imports IronXL
Imports System
Imports System.Linq
Friend Class Program
Shared Sub Main()
' Load the Excel workbook
Dim workBook As WorkBook = WorkBook.Load("sample.xlsx")
' Access the first worksheet
Dim workSheet As WorkSheet = workBook.WorkSheets.First()
' Define a range
Dim range = workSheet("B2:B2") ' This specifies the cell range to read
' Get the value stored in cell B2
For Each cell In range
Console.WriteLine($"Value in B2: {cell.Value}")
Next cell
End Sub
End Class
The above code example gets a value from cell B2 which will be printed to the console.
Output Console
By making some modifications to the code example provided above, it is possible to obtain a range of cell values from an Excel worksheet. This involves changing the range parameter passed to the WorkSheet
object. Specifically, the range parameter must be updated to reflect the range of cells that contain the desired data.
using IronXL;
using System;
using System.Linq;
class Program
{
static void Main()
{
// Load the Excel workbook
WorkBook workBook = WorkBook.Load("sample.xlsx");
// Access the first worksheet
WorkSheet workSheet = workBook.WorkSheets.First();
// Define a new range covering B2 to B3
var range = workSheet["B2:B3"];
// Get values stored in the defined range
foreach (var cell in range)
{
Console.WriteLine($"Value in {cell.Address}: {cell.Value}");
}
}
}
using IronXL;
using System;
using System.Linq;
class Program
{
static void Main()
{
// Load the Excel workbook
WorkBook workBook = WorkBook.Load("sample.xlsx");
// Access the first worksheet
WorkSheet workSheet = workBook.WorkSheets.First();
// Define a new range covering B2 to B3
var range = workSheet["B2:B3"];
// Get values stored in the defined range
foreach (var cell in range)
{
Console.WriteLine($"Value in {cell.Address}: {cell.Value}");
}
}
}
Imports IronXL
Imports System
Imports System.Linq
Friend Class Program
Shared Sub Main()
' Load the Excel workbook
Dim workBook As WorkBook = WorkBook.Load("sample.xlsx")
' Access the first worksheet
Dim workSheet As WorkSheet = workBook.WorkSheets.First()
' Define a new range covering B2 to B3
Dim range = workSheet("B2:B3")
' Get values stored in the defined range
For Each cell In range
Console.WriteLine($"Value in {cell.Address}: {cell.Value}")
Next cell
End Sub
End Class
The cell range is changed from [B2:B2]
to [B2:B3]
; this will print two values instead of just one in the console.
Output Console 2
Retrieving the value of a specific cell in an Excel worksheet using C# is a common task for many applications that work with Excel data. IronXL is a powerful software library for .NET developers that provides a simple and efficient way to read, write, and manipulate Excel spreadsheets in their applications. With IronXL, developers can quickly and easily incorporate Excel functionality into their applications, saving time and streamlining their development process.
By following the steps outlined in this article, developers can learn how to connect IronXL with their C# project, retrieve cell values programmatically, automate tasks that involve Excel data, and create more efficient and reliable applications. IronXL is a versatile and reliable solution for working with Excel files in C# Applications. Please visit those links to learn more about detailed operations on cell addresses or how to import Excel files.
Users of IronPDF can also benefit from the Iron Suite, a collection of software development tools that includes IronPDF, IronOCR, IronXL, IronBarcode, and IronWebscraper.
IronXL is a powerful software library for .NET developers that provides a simple and efficient way to read, write, and manipulate Excel spreadsheets in their applications.
To use IronXL, you need to have Visual Studio installed on your computer, ASP.NET, and the IronXL library from the NuGet Package Manager.
To create a new project, open Visual Studio, choose a project template like Console Application, name your project, select its location, and choose the desired framework.
You can install IronXL by navigating to the NuGet Package Manager in Visual Studio, searching for IronXL, and then following the installation instructions.
Yes, IronXL can be installed using the Visual Studio Command Line by entering 'Install-Package IronXL.Excel' in the Package Manager Console.
To get a specific cell value, load the Excel workbook, access the worksheet, define the cell range, and retrieve the value using IronXL's methods.
Yes, by changing the range parameter in the IronXL code, you can access and read a range of cell values from an Excel worksheet.
IronXL simplifies and streamlines Excel operations, allowing developers to efficiently incorporate Excel functionality into their applications, saving time and enhancing productivity.