Activating Sheets and Activating a Cell in Worksheet

Activating Sheets and Activating a Cell

Aspose.Cells provides specific API calls for activating a sheet and a cell. For Example, the WorksheetCollection.ActiveSheetIndex property is useful for setting the active sheet in a workbook. Similarly, the Worksheet.ActiveCell property can be used to set and get an active cell in the worksheet.

To make sure that the horizontal or vertical scrollbars are at the row and column index position you want to show specific data, use the Worksheet.FirstVisibleRow and Worksheet.FirstVisibleColumn properties.

The following example shows how to activate a worksheet and make an active cell in it. The following output is generated when executing the code. The scrollbars are scrolled to make the 2nd row and 2nd column as their first visible row and column.

Setting B2 cell as an active cell

todo:image_alt_text

Java code to set an active worksheet in Excel

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(ActivatingSheetsandActivatingCell.class);
// Instantiate a new Workbook
Workbook workbook = new Workbook();
// Get the first worksheet in the workbook
Worksheet worksheet = workbook.getWorksheets().get(0);
// Get the cells in the worksheet
Cells cells = worksheet.getCells();
// Input data into B2 cell
cells.get(1, 1).putValue("Hello World!");
// Set the first sheet as an active sheet
workbook.getWorksheets().setActiveSheetIndex(0);
// Set B2 cell as an active cell in the worksheet
worksheet.setActiveCell("B2");
// Set the B column as the first visible column in the worksheet
worksheet.setFirstVisibleColumn(1);
// Set the 2nd row as the first visible row in the worksheet
worksheet.setFirstVisibleRow(1);
// Save the excel file
workbook.save(dataDir + "activecell.xls");