DsExcel Java allows users to configure the vertical and horizontal page breaks by using the getHPageBreaks method and getVPageBreaks method of the IWorksheet interface.
You can also determine whether to adjust the horizontal and vertical page breaks or keep them fixed (while performing the insert and delete operations on the rows and columns) by using the getFixedPageBreaks and the setFixedPageBreaks methods of the IWorksheet interface.
This feature is useful especially when users need to print different reports from Excel to a PDF file. With the option to choose whether to adjust page breaks or keep them fixed, users can specify whether each section appears on a separate page or starts from a new page whenever any rows and columns are inserted or deleted in a spreadsheet.
If the setFixedPageBreaks method is set to false (this is the default behavior), then:
If the setFixedPageBreaks method is set to true, the row or column index of page breaks are not changed even after inserting or deleting rows or columns. Further, the horizontal and vertical page breaks are considered "fixed" and the page breaks can't be adjusted in this scenario.
In order to configure page breaks for customized printing, refer to the following example code.
Java |
Copy Code |
---|---|
// Initialize workbook Workbook workbook = new Workbook(); // Fetch default worksheet IWorksheet worksheet = workbook.getWorksheets().get(0); Object data = new Object[][] { { "Name", "City", "Birthday", "Sex", "Weight", "Height", "Age" }, { "Bob", "NewYork", new GregorianCalendar(1968, 6, 8), "male", 80, 180, 56 }, { "Betty", "NewYork", new GregorianCalendar(1972, 7, 3), "female", 72, 168, 45 }, { "Gary", "NewYork", new GregorianCalendar(1964, 3, 2), "male", 71, 179, 50 }, { "Hunk", "Washington", new GregorianCalendar(1972, 8, 8), "male", 80, 171, 59 }, { "Cherry", "Washington", new GregorianCalendar(1986, 2, 2), "female", 58, 161, 34 }, { "Coco", "Virginia", new GregorianCalendar(1982, 12, 12), "female", 58, 181, 45 }, { "Lance", "Chicago", new GregorianCalendar(1962, 3, 12), "female", 49, 160, 57 }, { "Eva", "Washington", new GregorianCalendar(1993, 2, 5), "female", 71, 180, 81 } }; // Set data worksheet.getRange("A1:G9").setValue(data); // Add a horizontal page break before the fourth row IHPageBreak hPageBreak = worksheet.getHPageBreaks().add(worksheet.getRange("F4")); // Add a vertical page break before the third column IVPageBreak vPageBreak = worksheet.getVPageBreaks().add(worksheet.getRange("F3")); // Saving workbook to xlsx workbook.save("7-PageBreaks.xlsx", SaveFileFormat.Xlsx); /* Delete rows and columns before the page breaks, the page breaks will be adjusted */ worksheet.getRange("1:2").delete(); // hPageBreak is before the 4th row worksheet.getRange("B:C").delete(); // vPageBreak is before the 4th column /* Set the page breaks are fixed, it will not be adjusted when inserting/ deleting rows/ columns */ worksheet.setFixedPageBreaks(true); // Saving workbook to xlsx workbook.save("PageBreaks1.xlsx", SaveFileFormat.Xlsx); /* Delete rows and columns after the page breaks, the page breaks will not be adjusted */ worksheet.getRange("1:2").delete(); // hPageBreak is still before the 4th row worksheet.getRange("B:C").delete(); // vPageBreak is still before the 4th column // Inserting rows after deleting row and column ranges worksheet.getRange("A3:A5").getEntireRow().insert(); // Saving workbook to xlsx workbook.save("PageBreaks2.xlsx", SaveFileFormat.Xlsx); |