How to Select a Range in Excel Using C#

In this tutorial, learn how to select and manipulate cell ranges in Excel using the IronXL library. Begin by setting up a project in Visual Studio and ensure the IronXL library is installed via the NuGet package manager. Import the IronXL namespace in your code. Load an existing Excel file using the Workbook.Load method and access the first worksheet with the Worksheets.First property. To select a specific range of cells, use the indexing syntax like A2:B5. Extract entire rows or columns using the GetRow and GetColumn methods respectively. Combine ranges with the plus operator, demonstrated by merging A2:B5 with A6:B6, and print the combined range to the console. Execute the program to observe the output, showcasing IronXL's capability to simplify Excel manipulation. Explore more of IronXL's functionalities for complex tasks. Visit the website through the link in the description for a trial subscription and experience the powerful features of IronXL.

using IronXL; // Import the IronXL namespace

class ExcelManipulator
{
    static void Main()
    {
        // Load the workbook from an existing Excel file
        WorkBook workbook = WorkBook.Load("Sample.xlsx");

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

        // Select a specific range of cells using the indexing syntax
        var range = worksheet["A2:B5"];

        // Extract entire row, e.g., the second row
        var row2 = worksheet.GetRow(1); // Rows and columns are zero-indexed

        // Extract entire column, e.g., the first column
        var column1 = worksheet.GetColumn(0);

        // Combine ranges with the plus operator
        var combinedRange = range + worksheet["A6:B6"];

        // Print the combined range to the console
        Console.WriteLine("Combined Range:");
        foreach (var cell in combinedRange)
        {
            Console.WriteLine($"{cell.Text} ");
        }
    }
}
using IronXL; // Import the IronXL namespace

class ExcelManipulator
{
    static void Main()
    {
        // Load the workbook from an existing Excel file
        WorkBook workbook = WorkBook.Load("Sample.xlsx");

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

        // Select a specific range of cells using the indexing syntax
        var range = worksheet["A2:B5"];

        // Extract entire row, e.g., the second row
        var row2 = worksheet.GetRow(1); // Rows and columns are zero-indexed

        // Extract entire column, e.g., the first column
        var column1 = worksheet.GetColumn(0);

        // Combine ranges with the plus operator
        var combinedRange = range + worksheet["A6:B6"];

        // Print the combined range to the console
        Console.WriteLine("Combined Range:");
        foreach (var cell in combinedRange)
        {
            Console.WriteLine($"{cell.Text} ");
        }
    }
}
Imports IronXL ' Import the IronXL namespace

Friend Class ExcelManipulator
	Shared Sub Main()
		' Load the workbook from an existing Excel file
		Dim workbook As WorkBook = WorkBook.Load("Sample.xlsx")

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

		' Select a specific range of cells using the indexing syntax
		Dim range = worksheet("A2:B5")

		' Extract entire row, e.g., the second row
		Dim row2 = worksheet.GetRow(1) ' Rows and columns are zero-indexed

		' Extract entire column, e.g., the first column
		Dim column1 = worksheet.GetColumn(0)

		' Combine ranges with the plus operator
		Dim combinedRange = range + worksheet("A6:B6")

		' Print the combined range to the console
		Console.WriteLine("Combined Range:")
		For Each cell In combinedRange
			Console.WriteLine($"{cell.Text} ")
		Next cell
	End Sub
End Class
$vbLabelText   $csharpLabel

This code demonstrates how to load an Excel workbook and access a specific worksheet, allowing manipulation of cell ranges. By selecting ranges, extracting rows and columns, and combining cell ranges, you can effectively manage Excel data. The code uses IronXL's simple API to interact with Excel files without needing Excel installed on the machine.

Further Reading: How to Select Range

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 Set a Password for a Workbook in C#
NEXT >
How to Read and Create Excel Files in .NET MAUI Using C#