Comment sélectionner une plage dans Excel en utilisant C# | 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.

Veuillez noterWhen applying methods that modify or move cell values, the affected range, row, or column will update its values accordingly.

ConseilsIronXL 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

ConseilsAll 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.

Veuillez noterCombining 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

Questions Fréquemment Posées

Comment sélectionner une plage de cellules dans Excel avec IronXL ?

Vous pouvez sélectionner une plage de cellules dans Excel avec IronXL en référencé directement la plage avec workSheet["A2:B8"] après avoir obtenu un objet WorkSheet.

Quelle est la méthode pour sélectionner une ligne entière dans IronXL ?

Vous pouvez sélectionner une ligne entière dans IronXL en utilisant la méthode GetRow avec un index basé sur zéro. Par exemple, pour sélectionner la 4ème ligne, utilisez worksheet.GetRow(3).

Comment puis-je sélectionner une colonne dans une feuille Excel avec IronXL ?

Pour sélectionner une colonne dans IronXL, utilisez la méthode GetColumn avec un index basé sur zéro, comme worksheet.GetColumn(2) pour la colonne C, ou spécifiez la plage comme worksheet["C:C"].

Est-il possible de combiner plusieurs plages dans IronXL ?

Oui, vous pouvez combiner plusieurs objets IronXL.Ranges.Range en utilisant l'opérateur '+' pour fusionner deux ou plusieurs plages en une nouvelle plage.

IronXL nécessite-t-il Office Interop pour sélectionner et manipuler des plages Excel ?

Non, IronXL permet de sélectionner et manipuler des plages Excel sans besoin d'Interop Office, ce qui en fait une option plus simple et efficace pour travailler avec des fichiers Excel.

Quelles opérations puis-je effectuer sur une plage sélectionnée dans IronXL ?

Une fois une plage sélectionnée dans IronXL, vous pouvez effectuer des opérations comme le tri, les calculs et les agrégations. Toute modification des valeurs des cellules dans ces plages mettra à jour dynamiquement la feuille de calcul.

Comment la combinaison de plages affecte-t-elle la plage originale dans IronXL ?

La combinaison de plages avec l'opérateur '+' dans IronXL modifie la plage originale pour inclure les plages combinées, permettant une manipulation fluide de jeux de données plus larges.

Quel système d'indexation IronXL utilise-t-il pour les lignes et colonnes ?

IronXL utilise un index basé sur zéro pour les lignes et colonnes, ce qui signifie que l'index commence à 0.

Chaknith Bin
Ingénieur logiciel
Chaknith travaille sur IronXL et IronBarcode. Il a une expertise approfondie en C# et .NET, aidant à améliorer le logiciel et à soutenir les clients. Ses idées issues des interactions avec les utilisateurs contribuent à de meilleurs produits, documentation et expérience globale.
Prêt à commencer?
Nuget Téléchargements 1,686,155 | Version : 2025.11 vient de sortir