Introduction
A Worksheet is an integral part of an Excel file. Infact an Excel file is composed of one or more Worksheets. Each worksheet can be composed of 65536 rows and 256 columns only. It is a worksheet that actually holds data in an Excel file. As we know that Aspose.Grid.Dekstop has the ability to create & manipulate existing and new Excel files so, there must be a way for the developers to access worksheets in an Excel file using Aspose.Grid.Desktop. This is what we are going to discuss in this topic.
Accessing a Specific Worksheet
Using Worksheet Index
Developers can access an instance of any Worksheet by using the worksheet index of any desired worksheet as shown below in the example.
Example:
[C#]
//Accesing a worksheet using its index
Worksheet sheet = gridDesktop1.Worksheets[0];
[VB.NET]
'Accesing a worksheet using its index
Dim sheet As Worksheet = gridDesktop1.Worksheets(0)
NOTE: This approach is better for iterating through a number of worksheets in an Excel file.
Using Worksheet Name
If the name of the Worksheet is already known then developers can also access a specific worksheet using its name as shown below:
Example:
[C#]
//Accesing a worksheet using its name
Worksheet sheet = gridDesktop1.Worksheets["Sheet1"];
[VB.NET]
'Accesing a worksheet using its index
Dim sheet As Worksheet = gridDesktop1.Worksheets("Sheet1")
Accessing an Active Worksheet
It is possbile for an Excel file to have more than one worksheet in itself. So, what if a developer needs to access that worksheet which is currently active and being used by a user?
To access an active worksheet, Aspose.Grid.Desktop offers two approaches:
1st Approach
One way to access an active worksheet using Aspose.Grid.Desktop control is to make use of ActiveSheetIndex property of GridDesktop control. Using this property, developer may get the index of that worksheet that is currently active in Aspose.Grid.Desktop control. Then that index can be used to access the worksheet in a traditional manner as shown below in the code snippet.
Example:
[C#]
//Accesing an active worksheet using its index
Worksheet sheet = gridDesktop1.Worksheets[gridDesktop1.ActiveSheetIndex];
[VB.NET]
'Accesing an active worksheet using its index
Dim sheet As Worksheet = gridDesktop1.Worksheets(gridDesktop1.ActiveSheetIndex)
2nd Approach
The other approach is to call GetActiveWorksheet method of GridDesktop control. This method will provide a reference of the worksheet that is currently active in Aspose.Grid.Desktop control as shown below in the example.
Example:
[C#]
//Accesing an active worksheet directly
Worksheet sheet = gridDesktop1.GetActiveWorksheet();
[VB.NET]
'Accesing an active worksheet directly
Dim sheet As Worksheet = gridDesktop1.GetActiveWorksheet()