How to Get Cell Value From Excel File in C#

C# is a popular programming language used for developing various types of applications, including those that work with Microsoft Excel. Excel is a widely used spreadsheet program that allows users to store, organize, and manipulate data in a tabular format. One common task when working with Excel using C# is to retrieve the value of a specific cell.

In this context, "get cell value" refers to the process of accessing the data stored in a particular cell of an Excel worksheet using C#. By retrieving cell values programmatically, C# developers can automate tasks that involve Excel data and create more efficient and reliable applications. Understanding how to retrieve cell values in C# can help you manipulate and analyze data in Excel more efficiently and effectively.

In this article, we will discuss how you can get a specific cell's value from Excel file using IronXL.

1. 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, data analysis tool, or simply need to read and write Excel files in your application, IronXL provides a flexible and reliable solution.

2. Prerequisites

If you want to use the IronXL library to extract the value of a specific cell of an Excel file, you must fulfil certain prerequisites, which include:

  • Installing Visual Studio on your computer as it's necessary to create a C# project.
  • Installing ASP.NET on your system.
  • Installing the IronXL library on your system to export data using it. You can obtain it by downloading the IronXL NuGet package from the NuGet package manager in Visual Studio.

3. Creating a New Project in Visual Studio

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. In this tutorial, we will use the Console Application project template to illustrate how to work with IronXL.

How to Get Cell Value From Excel File in C#: Figure 1 - 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.

How to Get Cell Value From Excel File in C#: Figure 2 - Project configuration

Once the solution is created, the program.cs file will be opened, enabling you to enter code and construct/run the application.

How to Get Cell Value From Excel File in C#: Figure 3 - Project with code open

With our new Visual Studio project now created, let's install IronXL.

4. Install IronXL

The IronXL Library can be downloaded and installed using a few different methods. but we will only use two of these in this article.

These are:

  • Using NuGet packages in Visual Studio.
  • Using the Visual Studio Command Line.

4.1 Using Visual Studio

To install the IronXL library, we can use 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 we can open the NuGet Package Manager in Visual Studio.

How to Get Cell Value From Excel File in C#: Figure 4 - NuGet Package Manager

The following shows IronXL in search results:

How to Get Cell Value From Excel File in C#: Figure 5 - IronXL search result

4.2 Using the Visual Studio Command-Line

Many developers prefer to install packages using a command line interface. To install IronXL using the command line, follow these steps:

  • In Visual Studio, go to Tools-> NuGet Package manager -> Package manager console.
  • Enter the following line in the package manager console tab:

        Install-Package IronXL.Excel

Now the package will be downloaded and installed to the current project and will be ready to use.

How to Get Cell Value From Excel File in C#: Figure 6 - Installing via command line

5. Get Specific Cell Values from an Excel File using IronXL

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. In the following code example, we will demonstrate how to use IronXL to obtain the value property with a cell address.

using IronXL;
using System;
using System.Linq;

WorkBook workBook = WorkBook.Load("sample.xlsx");
// Gice sheet name and data types
WorkSheet workSheet = workBook.WorkSheets.First();
var range = workSheet["B2:B2"];
var columnA = workSheet.GetColumn(0);
var row1 = workSheet.GetRow(0);
// Iterate over the range rows to get String Value
foreach (var cell in range)
{
    Console.WriteLine($"{cell.Value}");
}
using IronXL;
using System;
using System.Linq;

WorkBook workBook = WorkBook.Load("sample.xlsx");
// Gice sheet name and data types
WorkSheet workSheet = workBook.WorkSheets.First();
var range = workSheet["B2:B2"];
var columnA = workSheet.GetColumn(0);
var row1 = workSheet.GetRow(0);
// Iterate over the range rows to get String Value
foreach (var cell in range)
{
    Console.WriteLine($"{cell.Value}");
}
Imports IronXL
Imports System
Imports System.Linq

Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
' Gice sheet name and data types
Private workSheet As WorkSheet = workBook.WorkSheets.First()
Private range = workSheet("B2:B2")
Private columnA = workSheet.GetColumn(0)
Private row1 = workSheet.GetRow(0)
' Iterate over the range rows to get String Value
For Each cell In range
	Console.WriteLine($"{cell.Value}")
Next cell
VB   C#

In the above code example, we are getting a value from the cell A2 which we print to the terminal.

How to Get Cell Value From Excel File in C#: Figure 7 - OutPut Console

5.1. Read Range of Values from an Excel Worksheet

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.


    var range = workSheet["B2:B3"];

    var range = workSheet["B2:B3"];
Dim range = workSheet("B2:B3")
VB   C#

Here we have changed the range from [B2 , B2] to [B2 , B3] this will print two values instead of just one in the console.

How to Get Cell Value From Excel File in C#: Figure 8 - Output Console 2

6. Conclusion

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. To learn more about detailed operations on cell addresses, please visit the following link. Additionally, to see more code examples on cell addresses and ranges, please visit the following link.

Users of IronPDF can also benefit from the Iron Suite, a collection of software development tools that includes IronPDF, IronOCR, IronXL, IronBarcode, and IronWebscraper.