Combine Multiple Excel Ranges in C#

IronXL is a C# library that allows for efficient manipulation of Excel files. One of its features is the ability to combine IronXL.Ranges.Range objects using the + operator. Below, we'll detail how to effectively use this functionality with an example.

Combining IronXL Ranges

Here's how you can combine two or more IronXL.Ranges.Range objects:

using System;
using IronXL;

namespace IronXLRangeExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Load an existing Excel workbook
            WorkBook workbook = WorkBook.Load("sample.xlsx");

            // Select a worksheet
            WorkSheet sheet = workbook.GetWorkSheet("Sheet1");

            // Define two ranges within the worksheet
            Range range1 = sheet["A1:A10"]; // The first range from A1 to A10
            Range range2 = sheet["B1:B10"]; // The second range from B1 to B10

            // Combine the two ranges using the '+' operator
            Range combinedRange = range1 + range2;

            // Iterate through each cell in the combined range and perform an action
            foreach (var cell in combinedRange)
            {
                Console.WriteLine(cell.Text); // Output each cell's text content
            }
        }
    }
}
using System;
using IronXL;

namespace IronXLRangeExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Load an existing Excel workbook
            WorkBook workbook = WorkBook.Load("sample.xlsx");

            // Select a worksheet
            WorkSheet sheet = workbook.GetWorkSheet("Sheet1");

            // Define two ranges within the worksheet
            Range range1 = sheet["A1:A10"]; // The first range from A1 to A10
            Range range2 = sheet["B1:B10"]; // The second range from B1 to B10

            // Combine the two ranges using the '+' operator
            Range combinedRange = range1 + range2;

            // Iterate through each cell in the combined range and perform an action
            foreach (var cell in combinedRange)
            {
                Console.WriteLine(cell.Text); // Output each cell's text content
            }
        }
    }
}
Imports System
Imports IronXL

Namespace IronXLRangeExample
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			' Load an existing Excel workbook
			Dim workbook As WorkBook = WorkBook.Load("sample.xlsx")

			' Select a worksheet
			Dim sheet As WorkSheet = workbook.GetWorkSheet("Sheet1")

			' Define two ranges within the worksheet
			Dim range1 As Range = sheet("A1:A10") ' The first range from A1 to A10
			Dim range2 As Range = sheet("B1:B10") ' The second range from B1 to B10

			' Combine the two ranges using the '+' operator
			Dim combinedRange As Range = range1 + range2

			' Iterate through each cell in the combined range and perform an action
			For Each cell In combinedRange
				Console.WriteLine(cell.Text) ' Output each cell's text content
			Next cell
		End Sub
	End Class
End Namespace
$vbLabelText   $csharpLabel

Explanation of the Code

  1. Loading the Workbook: The code starts by loading an existing Excel file named sample.xlsx. Assuming this file is located in the same directory as the executable, it will be opened for further operations.
  2. Access the Worksheet: We then select a specific worksheet by name, in this case, "Sheet1".
  3. Define Ranges: By using the sheet[] indexer, we define two ranges: range1, which includes cells from A1 to A10, and range2, which includes cells from B1 to B10.
  4. Combine Ranges: The two ranges are combined using the + operator into a single combinedRange. This new range now includes all cells from A1 to A10 and B1 to B10.
  5. Iterate Over Range: We loop over each cell in the combinedRange, printing out the content of each cell. The cell.Text property is used to retrieve the text contained in each cell.

Using the + operator to combine IronXL.Ranges.Range objects is a versatile way to handle multiple ranges as a single entity. This can be incredibly useful in scenarios requiring the aggregation of data from various sections of a worksheet.