Introduction
Microsoft Excel provides a feature to set the Zoom or Scaling Factor of the worksheets. This feature helps users to see the contents of their worksheets in smaller or larger views. Users can set the zoom factor to any value using Microsoft Excel as shown below:
|
Figure: Setting the Zoom Factor using Microsoft Excel
|
Aspose.Cells also allows developers to set the zoom factor for their worksheets.
Controlling the Zoom Factor of the Worksheets
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 set the zoom factor of a worksheet, developers may use Zoom property of the Worksheet class. Developers can assign a numeric (integer) value to the Zoom property as a zoom factor.
Example:
[C#]
//Setting the zoom factor of the worksheet to 75
worksheet.Zoom = 75;
[VB.NET]
'Setting the zoom factor of the worksheet to 75
worksheet.Zoom = 75
[JAVA]
//Setting the zoom factor of the worksheet to 75
worksheet.setZoom(75);
Example:
A complete example is given below that demonstrates the use of Zoom property of Worksheet class to set the zoom factor 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];
//Setting the zoom factor of the worksheet to 75
worksheet.Zoom=75;
//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)
'Setting the zoom factor of the worksheet to 75
worksheet.Zoom=75
'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);
//Setting the zoom factor of the worksheet to 75
worksheet.setZoom(75);
//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 in the default view.
|
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 the zoom factor of the first worksheet of the Book1.xls file is set to 75. The modified file is saved as output.xls file whose pictorial view is shown below:
|
Figure: Worksheet view after modification
|