Introduction
All worksheets can be viewed in two modes:
- Normal View
- Page Break Preview
Normal View is the default view of a worksheet that we normally experience while working on worksheets where as Page Break Preview is an editing view that displays a worksheet as it will print. Page Break Preview shows that what data will go on each page so, you can adjust the print area and page breaks. Using, Aspose.Cells , developers can enable normal view or page break preview modes for their desired worksheets.
Controlling the View Modes 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 enable the normal or page break preview mode for a worksheet, developers may use IsPageBreakPreview property of the Worksheet class. IsPageBreakPreview is a boolean property, which means that it can only store a true or false value.
Enabling Normal View
Developers can set any worksheet to normal view by setting the IsPageBreakPreview property of the Worksheet class to false.
Example:
[C#]
//Displaying the worksheet in normal preview
worksheet.IsPageBreakPreview = false;
[VB.NET]
'Displaying the worksheet in normal preview
worksheet.IsPageBreakPreview = False
[JAVA]
//Displaying the worksheet in page break preview
worksheet.setPageBreakPreview(false);
Enabling Page Break Preview
Developers can set any worksheet to page break preview by setting the IsPageBreakPreview property of the Worksheet class to true. Doing so, switches the worksheet from normal view to page break preview.
Example:
[C#]
//Displaying the worksheet in page break preview
worksheet.IsPageBreakPreview = true;
[VB.NET]
'Displaying the worksheet in page break preview
worksheet.IsPageBreakPreview = True
[JAVA]
//Displaying the worksheet in page break preview
worksheet.setPageBreakPreview(true);
Example:
A complete example is given below that demonstrates the use of IsPageBreakPreview property of Worksheet class to enable the page break preview mode for 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];
//Displaying the worksheet in page break preview
worksheet.IsPageBreakPreview=true;
//Saving the modified Excel file in default 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)
'Displaying the worksheet in page break preview
worksheet.IsPageBreakPreview=True
'Saving the modified Excel file in default 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);
//Displaying the worksheet in page break preview
worksheet.setPageBreakPreview(true);
//Saving the modified Excel file in default format
workbook.save("C:\\output.xls");
Worksheet - Before Modification
In the screenshot below, you can see that Book1.xls file is in Normal 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 Normal View is switched to Page Break Preview for 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
|