如何使用C#选择Excel中的范围 | IronXL

How to Select Range

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronXL provides a convenient way to select and manipulate ranges within an Excel Worksheet without using Office Interop.

Quickstart: Selecting a Cell Range in IronXL in One Line

Use a single call to GetRange on an IronXL worksheet to grab a rectangular range like "A1:C3"—no loops, no fuss. It’s the fastest way to start manipulating multiple cells at once.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronXL with NuGet Package Manager

    PM > Install-Package IronXL.Excel

  2. Copy and run this code snippet.

    var range = workSheet.GetRange("A1:C3");
  3. Deploy to test on your live environment

    Start using IronXL in your project today with a free trial
    arrow pointer


Select Range Example

With IronXL, you can perform various operations on selected ranges, such as sorting, calculations, and aggregations.

请注意When applying methods that modify or move cell values, the affected range, row, or column will update its values accordingly.

提示IronXL allows us to combine more than one IronXL.Ranges.Range using the '+' operator.

Select Range

To select a range from cell A2 to B8, you can use the following code:

:path=/static-assets/excel/content-code-examples/how-to/select-range-range.cs
using IronXL;
using System.Linq;

WorkBook workBook = WorkBook.Load("sample.xls");
WorkSheet workSheet = workBook.WorkSheets.First();

// Get range from worksheet
var range = workSheet["A2:B8"];
Imports IronXL
Imports System.Linq

Private workBook As WorkBook = WorkBook.Load("sample.xls")
Private workSheet As WorkSheet = workBook.WorkSheets.First()

' Get range from worksheet
Private range = workSheet("A2:B8")
$vbLabelText   $csharpLabel
Select Range

Select Row

To select the 4th row, you can use the GetRow(3) method with zero-based indexing. This will include all cells in the 4th row, even if some corresponding cells in other rows are empty.

:path=/static-assets/excel/content-code-examples/how-to/select-range-row.cs
using IronXL;
using System.Linq;

WorkBook workBook = WorkBook.Load("sample.xls");
WorkSheet workSheet = workBook.WorkSheets.First();

// Get row from worksheet
var row = workSheet.GetRow(3);
Imports IronXL
Imports System.Linq

Private workBook As WorkBook = WorkBook.Load("sample.xls")
Private workSheet As WorkSheet = workBook.WorkSheets.First()

' Get row from worksheet
Private row = workSheet.GetRow(3)
$vbLabelText   $csharpLabel
Select Row

Select Column

To select column C, you can use the GetColumn(2) method or specify the range address as workSheet ["C:C"]. Like the GetRow method, it will include all relevant cells, whether they are filled in the specified column or not.

:path=/static-assets/excel/content-code-examples/how-to/select-range-column.cs
using IronXL;
using System.Linq;

WorkBook workBook = WorkBook.Load("sample.xls");
WorkSheet workSheet = workBook.WorkSheets.First();

// Get column from worksheet
var column = workSheet.GetColumn(2);
Imports IronXL
Imports System.Linq

Private workBook As WorkBook = WorkBook.Load("sample.xls")
Private workSheet As WorkSheet = workBook.WorkSheets.First()

' Get column from worksheet
Private column = workSheet.GetColumn(2)
$vbLabelText   $csharpLabel
Select Column

提示All the row and column index positions adhere to zero-based indexing.

Combine Ranges

IronXL provides the flexibility to combine multiple IronXL.Ranges.Range objects using the '+' operator. By using the '+' operator, you can easily concatenate or merge ranges together to create a new range.

请注意Combining rows and columns directly using the '+' operator is not supported.

Combining ranges will modify the original range. In the code snippet below, the variable range will be modified to include the combined ranges.

:path=/static-assets/excel/content-code-examples/how-to/select-range-combine-range.cs
using IronXL;
using System.Linq;

WorkBook workBook = WorkBook.Load("sample.xls");
WorkSheet workSheet = workBook.WorkSheets.First();

// Get range from worksheet
var range = workSheet["A2:B2"];

// Combine two ranges
var combinedRange = range + workSheet["A5:B5"];
Imports IronXL
Imports System.Linq

Private workBook As WorkBook = WorkBook.Load("sample.xls")
Private workSheet As WorkSheet = workBook.WorkSheets.First()

' Get range from worksheet
Private range = workSheet("A2:B2")

' Combine two ranges
Private combinedRange = range + workSheet("A5:B5")
$vbLabelText   $csharpLabel

常见问题解答

如何使用IronXL在Excel中选择一组单元格?

您可以通过在获得WorkSheet对象后,直接引用范围workSheet["A2:B8"]来选择Excel中的一组单元格。

在IronXL中选择整行的方法是什么?

您可以使用GetRow方法并采用从零开始的索引在IronXL中选择整行。例如,要选择第4行,使用worksheet.GetRow(3)

如何使用IronXL在Excel工作表中选择列?

要在IronXL中选择列,请使用从零开始的索引的GetColumn方法,例如worksheet.GetColumn(2)表示C列,或者将范围指定为worksheet["C:C"]

在IronXL中是否可以合并多个范围?

是的,您可以使用'+'运算符合并多个IronXL.Ranges.Range对象,将两个或多个范围合并为一个新范围。

IronXL需要Office Interop来选择和操作Excel范围吗?

不,IronXL允许在无需Office Interop的情况下选择和操作Excel范围,使其成为处理Excel文件的更简单且更高效的选项。

在IronXL中我可以对选定的范围执行什么操作?

在IronXL中选定范围后,您可以执行排序、计算和聚合等操作。对这些范围内单元格值的任何修改都会动态更新电子表格。

在IronXL中合并范围如何影响原始范围?

在IronXL中使用'+'运算符合并范围会修改原始范围以包含合并的范围,从而能够无缝操作更大的数据集。

IronXL对行和列使用什么索引系统?

IronXL对行与列使用从零开始的索引,这意味着索引从0开始。

Chaknith Bin
软件工程师
Chaknith 在 IronXL 和 IronBarcode 工作。他在 C# 和 .NET 方面有着深厚的专业知识,帮助改进软件并支持客户。他从用户互动中获得的见解有助于更好的产品、文档和整体体验。
准备开始了吗?
Nuget 下载 1,686,155 | 版本: 2025.11 刚刚发布