Introduction
Sometimes, you may have the requirement that a specific worksheet should be active and viewed first when the user will open the excel file in MS Excel. Similarly, you do need to activate a specific cell in the worksheet and also the scrollbars should be scroll down to the active cell to have a better view of data or portion of data in the worksheet. Aspose.Cells is capable of doing all the above mentioned tasks for you. An Active Sheet is a sheet that you're working on in a workbook. The name on the tab of the active sheet is bold by default. An Active Cell is a selected cell in which normally data is entered when you begin typing. Only one cell is active at a time. The active cell is bounded by a heavy border.
Activating Sheets and Making an Active Cell in the Worksheet
Aspose.Cells provides some specific API for the tasks. For Example, the Aspose.Cells.Worksheets.ActiveSheetIndex property is useful for setting and active sheet in the workbook. Similarly, the Aspose.Cells.Worksheet.ActiveCell property is used to set / get an active cell in the worksheet. Moreover, if you want that the horizontal / vertical scrollbars should be scroll down to adjust the row / column index position and give you a better view of data or portion of data opening the file in MS Excel, you may use Aspose.Cells.Worksheet.FirstVisibleRow and Aspose.Cells.FirstVisibleColumn properties.
Note: All the above APIs are handy and worthwhile, if you are using the licensed version of Aspose.Cells.
Example
The following example shows how to activate a worksheet and make an active cell in it.
[C#]
//Instantiate an instance of license and set the license file through its path
Aspose.Cells.License license = new Aspose.Cells.License();
license.SetLicense("Aspose.Cells.lic");
//Instantiate a new Workbook.
Workbook workbook = new Workbook();
//Get the first worksheet in the workbook.
Worksheet worksheet1 = workbook.Worksheets[0];
//Get the cells in the worksheet.
Cells cells = worksheet1.Cells;
//Input data into B2 cell.
cells[1,1].PutValue("Hello World!");
//Set the first sheet as an active sheet.
workbook.Worksheets.ActiveSheetIndex = 0;
//Set B2 cell as an active cell in the worksheet.
worksheet1.ActiveCell= "B2";
//Set the B column as the first visible column in the worksheet.
worksheet1.FirstVisibleColumn =1;
//Set the 2nd row as the first visible row in the worksheet.
worksheet1.FirstVisibleRow = 1;
//Save the excel file.
workbook.Save("d:\\test\\activecell.xls");
[VB]
'Instantiate an instance of license and set the license file through its path
Dim license As Aspose.Cells.License = New Aspose.Cells.License()
license.SetLicense("Aspose.Cells.lic")
'Instantiate a new Workbook.
Dim workbook As Workbook = New Workbook()
'Get the first worksheet in the workbook.
Dim worksheet1 As Worksheet = workbook.Worksheets(0)
'Get the cells in the worksheet.
Dim cells As Cells = worksheet1.Cells
'Input data into B2 cell.
cells(1,1).PutValue("Hello World!")
'Set the first sheet as an active sheet.
workbook.Worksheets.ActiveSheetIndex = 0
'Set B2 cell as an active cell in the worksheet.
worksheet1.ActiveCell= "B2"
'Set the B column as the first visible column in the worksheet.
worksheet1.FirstVisibleColumn =1
'Set the 2nd row as the first visible row in the worksheet.
worksheet1.FirstVisibleRow = 1
'Save the excel file.
workbook.Save("d:\test\activecell.xls")
[JAVA]
//Instantiate an instance of license and set the license file through streams.
License license = new License();
license.setLicense(myStream);
//Instantiate a new Workbook.
Workbook workbook = new Workbook();
//Get the first worksheet in the workbook.
Worksheet worksheet1 = workbook.getWorksheets().getSheet(0);
//Get the cells in the worksheet.
Cells cells = worksheet1.getCells();
//Input data into B2 cell.
cells.getCell(1,1).setValue("Hello World!");
//Set the first sheet as an active sheet.
workbook.getWorksheets().setActiveSheet(0);
//Set B2 cell as an active cell in the worksheet.
worksheet1.setActiveCell("B2");
//Set the B column as the first visible column in the worksheet.
worksheet1.setFirstVisibleColumn(1);
//Set the 2nd row as the first visible row in the worksheet.
worksheet1.setFirstVisibleRow(1);
//Save the excel file.
workbook.save("d:\\test\\activecell.xls");
The following output would be generated after executing the above code:
|
Figure: Setting B2 Cell as an Active Cell. The scrollbars are scrolled to make the 2nd row and 2nd column as their first visible row and column.
|