Available for: UI for ASP.NET MVC | UI for ASP.NET AJAX | UI for Blazor | UI for WPF | UI for WinForms | UI for Silverlight | UI for Xamarin | UI for WinUI | UI for ASP.NET Core | UI for .NET MAUI

New to Telerik Document Processing? Download free 30-day trial

Accessing Cells of a Worksheet

Each worksheet consists of cells organized in rows and columns. In order to manipulate the data and properties of the cells, you need to create and use an instance of the CellSelection class.

As a worksheet contains a considerable number of rows and columns, it is unusual to perform an operation to the whole range of cells. Typically, you would like to set the value of a single cell or, for example, apply formatting to a range of cells. In order to perform an operation on a specified range of cells you need to create a CellSelection object that holds the desired cell region and then invoke the appropriate action for this CellSelection instance. This class exposes a rich API that allows you to get, set and clear cell's value, colors, borders and style.

The document model offers multiple ways to create a CellSelection object. The following examples list all approaches you can use to retrieve a CellSelection instance. Note that each example creates a new workbook, adds one worksheet and creates a selection for its cells.

Using a CellIndex object allows you to specify a single cell (identified by a row index and column index) from the sheet and create a CellSelection for this single cell. Then, you can get the Value of the cell.

Example 1: Create CellSelection using CellIndex

Workbook workbook = new Workbook(); 
Worksheet worksheet = workbook.Worksheets.Add(); 
 
CellIndex cellIndex = new CellIndex(0, 5); 
CellSelection selection1 = worksheet.Cells[cellIndex]; 

Example 2: Create CellSelection using CellRange

Workbook workbook = new Workbook(); 
Worksheet worksheet = workbook.Worksheets.Add(); 
 
CellRange cellRange = new CellRange(0, 0, 5, 5); 
CellSelection selection2 = worksheet.Cells[cellRange]; 

Example 3: Create CellSelection using multiple CellRange objects

Workbook workbook = new Workbook(); 
Worksheet worksheet = workbook.Worksheets.Add(); 
 
List<CellRange> ranges = new List<CellRange>(); 
ranges.Add(new CellRange(0, 0, 5, 5)); 
ranges.Add(new CellRange(0, 10, 5, 15)); 
CellSelection selection3 = worksheet.Cells[ranges]; 

Example 4: Create CellSelection using two CellIndex instances that specify a CellRange

Workbook workbook = new Workbook(); 
Worksheet worksheet = workbook.Worksheets.Add(); 
 
CellIndex fromIndex = new CellIndex(0, 0); 
CellIndex toIndex = new CellIndex(5, 5); 
CellSelection selection4 = worksheet.Cells[fromIndex, toIndex]; 

Example 5: Create CellSelection using two integers that indicate the CellIndex

Workbook workbook = new Workbook(); 
Worksheet worksheet = workbook.Worksheets.Add(); 
 
CellSelection selection5 = worksheet.Cells[0, 5]; 

Example 6: Create CellSelection using four integers that specify the CellRange

Workbook workbook = new Workbook(); 
Worksheet worksheet = workbook.Worksheets.Add(); 
 
CellSelection selection6 = worksheet.Cells[0, 0, 5, 5]; 

Once you have a CellSelection object you can get, set and clear the properties of the selected cells. More information about cell properties is available in the Get, Set and Clear Cell Properties article.

In this article