How to Use Excel Interop in C# Alternatively

In this tutorial, we explore using IronXL to access Excel files without relying on interop. The process begins by loading an Excel file into a workbook object, demonstrated with a sample file named 'sample.xlsx'. The tutorial focuses on accessing a specific worksheet, titled '2022 budget', using the GetWorkSheet method. This allows users to interact directly with the data within the selected worksheet.

The following C# code snippet illustrates how to extract a single cell's value by accessing cell A5 and converting its value to a string for console output:

using IronXL;

// Load the workbook from a file
WorkBook workbook = WorkBook.Load("sample.xlsx");

// Access the specific worksheet titled '2022 budget'
WorkSheet worksheet = workbook.GetWorkSheet("2022 budget");

// Access cell A5 and convert its value to a string to print to the console
string cellValue = worksheet["A5"].StringValue;
Console.WriteLine($"The value of cell A5 is: {cellValue}");
using IronXL;

// Load the workbook from a file
WorkBook workbook = WorkBook.Load("sample.xlsx");

// Access the specific worksheet titled '2022 budget'
WorkSheet worksheet = workbook.GetWorkSheet("2022 budget");

// Access cell A5 and convert its value to a string to print to the console
string cellValue = worksheet["A5"].StringValue;
Console.WriteLine($"The value of cell A5 is: {cellValue}");
$vbLabelText   $csharpLabel

For retrieving multiple cell values, a loop is employed with the range function to specify a range, such as B2 to B10. Iterating over this range with a for-each loop efficiently prints each cell's value to the console. This method is particularly useful for accessing and displaying multiple values from an Excel worksheet:

// Define the range from B2 to B10
var range = worksheet[$"B2:B10"];

// Iterate over each cell in the specified range and print its value
foreach (var cell in range)
{
    Console.WriteLine($"Cell {cell.Address}: {cell.StringValue}");
}
// Define the range from B2 to B10
var range = worksheet[$"B2:B10"];

// Iterate over each cell in the specified range and print its value
foreach (var cell in range)
{
    Console.WriteLine($"Cell {cell.Address}: {cell.StringValue}");
}
$vbLabelText   $csharpLabel

The tutorial concludes by running the program to display all extracted values, highlighting IronXL's capabilities for Excel data handling. Viewers are encouraged to like, subscribe, and explore the software via a provided link.

Further Reading: C# Excel Interop Workaround

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 Export Excel Data Using C#
NEXT >
How to Set Cell Borders and Alignment in Excel