Introduction
Freeze Panes is also another good feature provided by Microsoft Excel. Freezing panes allows you to select data that remains visible when scrolling in a worksheet. Using Microsoft Excel, you can use the freeze panes feature for your worksheet as shown below:
|
Figure: Using Freeze Panes feature in Microsoft Excel
|
Aspose.Cells also allows developers to apply this freeze panes feature (offered by Microsoft Excel) in their worksheets at runtime using its robust API.
Aspose.Cells & Freeze Panes
Aspose.Cells provides a class, Workbook that represents an Excel file. Workbook class contains a Worksheets collection that allows to access each worksheet in the Excel file.
A worksheet is represented by the Worksheet class. Worksheet class provides a wide range of properties and methods to manage a worksheet.
But, to configure freeze panes settings for a worksheet, developers may call FreezePanes method of the Worksheet class. FreezePanes method takes four parameters as follows:
- row, represents the row index of the cell from where to start freezing
- column, represents the column index of the cell from where to start freezing
- freezedRows, represents the number of visible rows in top pane
- freezedColumns, represents the number of visible columns in left pane
Example:
A complete example is given below that demonstrates the use of FreezePanes method of Worksheet class to freeze rows and columns (starting from C4 cell represented by 4th row and 3rd column, where the rows and columns start from 0 index) of the first worksheet of the Excel file.
[C#]
//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream("C:\\book1.xls",FileMode.Open);
//Opening the Excel file through the file stream
workbook.Open(fstream);
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
//Applying freeze panes settings
worksheet.FreezePanes(3,2,3,2);
//Saving the modified Excel file in default (that is Excel 2000) format
workbook.Save("C:\\output.xls",FileFormatType.Default);
//Closing the file stream to free all resources
fstream.Close();
[VB.NET]
'Instantiating a Workbook object
Dim workbook As Workbook = New Workbook()
'Creating a file stream containing the Excel file to be opened
Dim fstream As FileStream = New FileStream("C:\\book1.xls",FileMode.Open)
'Opening the Excel file through the file stream
workbook.Open(fstream)
'Accessing the first worksheet in the Excel file
Dim worksheet As Worksheet = workbook.Worksheets(0)
'Applying freeze panes settings
worksheet.FreezePanes(3,2,3,2)
'Saving the modified Excel file in default (that is Excel 2000) format
workbook.Save("C:\\output.xls",FileFormatType.Default)
'Closing the file stream to free all resources
fstream.Close()
[JAVA]
//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Opening the Excel file
workbook.open("C:\\book1.xls");
//Accessing the first worksheet in the Excel file
Worksheets worksheets = workbook.getWorksheets();
Worksheet worksheet = worksheets.getSheet(0);
//Applying freeze panes settings
worksheet.freezePanes(3,2,3,2);
//Saving the modified Excel file in default (that is Excel 2000) format
workbook.save("C:\\output.xls");
Worksheet - Before Modification
In the screenshot below, you can see Book1.xls file without applying freeze panes settings.
|
Figure: Worksheet view before any modification
|
Worksheet - After Executing the Example Code
Book1.xls file is opened by calling the Open method of Workbook class and then few rows and columns are freezed in the first worksheet of the Book1.xls file. The modified file is saved as output.xls file whose pictorial view is shown below:
|
Figure: Worksheet view after modification
|