Setting up the page layout in Excel is an important step to make your worksheets look polished and professional. Whether you’re printing a report or sharing it digitally, customizing options like margins, orientation, paper size, and scaling helps ensure your data is presented clearly and effectively. In this article, you will learn how to programmatically set page setup options in Excel in Java using Spire.XLS for Java.
- Set Page Margins in Excel in Java
- Set Page Orientation in Excel in Java
- Set Paper Size in Excel in Java
- Set Print Area in Excel in Java
- Set Scaling Factor in Excel in Java
- Set FitToPages Options in Excel in Java
- Set Headers and Footers in Excel in Java
Install Spire.XLS for Java
First of all, you're required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.xls</artifactId> <version>15.1.3</version> </dependency> </dependencies>
Set Page Margins in Excel in Java
The PageSetup class in Spire.XLS for Java allows you to customize page setup options for Excel worksheets. It provides methods like setTopMargin(), setBottomMargin(), setLeftMargin(), setRightMargin(), setHeaderMarginInch(), and setFooterMarginInch(), enabling you to adjust the top, bottom, left, right, header, and footer margins of a worksheet. The detailed steps are as follows.
- Create an object of the Workbook class.
- Load an Excel file using the Workbook.loadFromFile() method.
- Get a specific worksheet using the Workbook.getWorksheets().get(index) method.
- Access the PageSetup object of the worksheet using the Worksheet.getPageSetup() method.
- Set the top, bottom, left, right, header, and footer margins using the PageSetup.setTopMargin(), PageSetup.setBottomMargin(), PageSetup.setLeftMargin(), PageSetup.setRightMargin(), PageSetup.setHeaderMarginInch(), and PageSetup.setFooterMarginInch() methods.
- Save the modified workbook to a new file using the Workbook.saveToFile() method.
- Java
import com.spire.xls.ExcelVersion; import com.spire.xls.PageSetup; import com.spire.xls.Workbook; import com.spire.xls.Worksheet; public class PageMargins { public static void main(String[] args) { // Create a Workbook object Workbook workbook = new Workbook(); // Load an Excel file workbook.loadFromFile("Sample.xlsx"); // Get the first worksheet Worksheet sheet = workbook.getWorksheets().get(0); // Get the PageSetup object of the worksheet PageSetup pageSetup = sheet.getPageSetup(); // Set top, bottom, left, and right margins for the worksheet // The measure of the unit is Inch (1 inch = 2.54 cm) pageSetup.setTopMargin(1); pageSetup.setBottomMargin(1); pageSetup.setLeftMargin(1); pageSetup.setRightMargin(1); pageSetup.setHeaderMarginInch(1); pageSetup.setFooterMarginInch(1); // Save the modified workbook to a new file workbook.saveToFile("SetPageMargins.xlsx", ExcelVersion.Version2016); workbook.dispose(); } }
Set Page Orientation in Excel in Java
The PageSetup.setOrientation() method allows you to specify the page orientation for printing. You can choose between two options: portrait mode or landscape mode. The detailed steps are as follows.
- Create an object of the Workbook class.
- Load an Excel file using the Workbook.loadFromFile() method.
- Get a specific worksheet using the Workbook.getWorksheets().get(index) method.
- Access the PageSetup object of the worksheet using the Worksheet.getPageSetup() method.
- Set the page orientation using the PageSetup.setOrientation() method.
- Save the modified workbook to a new file using the Workbook.saveToFile() method.
- Java
import com.spire.xls.*; public class PageOrientation { public static void main(String[] args) { // Create a Workbook object Workbook workbook = new Workbook(); // Load an Excel file workbook.loadFromFile("Sample.xlsx"); // Get the first worksheet Worksheet sheet = workbook.getWorksheets().get(0); // Get the PageSetup object of the worksheet PageSetup pageSetup = sheet.getPageSetup(); pageSetup.setOrientation(PageOrientationType.Landscape); // Save the modified workbook to a new file workbook.saveToFile("SetPageOrientation.xlsx", ExcelVersion.Version2016); workbook.dispose(); } }
Set Paper Size in Excel in Java
The PageSetup.setPaperSize() method enables you to select from a variety of paper sizes for printing your worksheet. These options include A3, A4, A5, B4, B5, letter, legal, tabloid, and more. The detailed steps are as follows.
- Create an object of the Workbook class.
- Load an Excel file using the Workbook.loadFromFile() method.
- Get a specific worksheet using the Workbook.getWorksheets().get(index) method.
- Access the PageSetup object of the worksheet using the Worksheet.getPageSetup() method.
- Set the paper size using the PageSetup.setPaperSize() method.
- Save the modified workbook to a new file using the Workbook.saveToFile() method.
- Java
import com.spire.xls.*; public class PaperSize { public static void main(String[] args) { // Create a Workbook object Workbook workbook = new Workbook(); // Load an Excel file workbook.loadFromFile("Sample.xlsx"); // Get the first worksheet Worksheet sheet = workbook.getWorksheets().get(0); // Get the PageSetup object of the worksheet PageSetup pageSetup = sheet.getPageSetup(); // Set the paper size to A4 pageSetup.setPaperSize(PaperSizeType.PaperA4); // Save the modified workbook to a new file workbook.saveToFile("SetPaperSize.xlsx", ExcelVersion.Version2016); workbook.dispose(); } }
Set Print Area in Excel in Java
You can define the specific area to be printed by using the PageSetup.setPrintArea() method. The detailed steps are as follows.
- Create an object of the Workbook class.
- Load an Excel file using the Workbook.loadFromFile() method.
- Get a specific worksheet using the Workbook.getWorksheets().get(index) method.
- Access the PageSetup object of the worksheet using the Worksheet.getPageSetup() method.
- Set the print area using the PageSetup.setPringArea() method.
- Save the modified workbook to a new file using the Workbook.saveToFile() method.
- Java
import com.spire.xls.ExcelVersion; import com.spire.xls.PageSetup; import com.spire.xls.Workbook; import com.spire.xls.Worksheet; public class PrintArea { public static void main(String[] args) { // Create a Workbook object Workbook workbook = new Workbook(); // Load an Excel file workbook.loadFromFile("Sample.xlsx"); // Get the first worksheet Worksheet sheet = workbook.getWorksheets().get(0); // Get the PageSetup object of the worksheet PageSetup pageSetup = sheet.getPageSetup(); // Set the print area of the worksheet to "A1:E5" pageSetup.setPrintArea("A1:E5"); // Save the modified workbook to a new file workbook.saveToFile("SetPrintArea.xlsx", ExcelVersion.Version2016); workbook.dispose(); } }
Set Scaling Factor in Excel in Java
To scale the content of your worksheet to a specific percentage of its original size, you can use the PageSetup.setZoom() method. The detailed steps are as follows.
- Create an object of the Workbook class.
- Load an Excel file using the Workbook.loadFromFile() method.
- Get a specific worksheet using the Workbook.getWorksheets().get(index) method.
- Access the PageSetup object of the worksheet using the Worksheet.getPageSetup() method.
- Set the scaling factor using the PageSetup.setZoom() method.
- Save the modified workbook to a new file using the Workbook.saveToFile() method.
- Java
import com.spire.xls.ExcelVersion; import com.spire.xls.PageSetup; import com.spire.xls.Workbook; import com.spire.xls.Worksheet; public class ScalingFactor { public static void main(String[] args) { // Create a Workbook object Workbook workbook = new Workbook(); // Load an Excel file workbook.loadFromFile("Sample.xlsx"); // Get the first worksheet Worksheet sheet = workbook.getWorksheets().get(0); // Get the PageSetup object of the worksheet PageSetup pageSetup = sheet.getPageSetup(); // Set the scaling factor of the worksheet to 90% pageSetup.setZoom(90); // Save the modified workbook to a new file workbook.saveToFile("SetScalingFactor.xlsx", ExcelVersion.Version2016); workbook.dispose(); } }
Set FitToPages Options in Excel in Java
Spire.XLS also provides the ability to adjust your worksheet content to fit a specific number of pages by using the PageSetup.setFitToPagesTall() and PageSetup.setFitToPagesWide() methods. The detailed steps are as follows.
- Create an object of the Workbook class.
- Load an Excel file using the Workbook.loadFromFile() method.
- Get a specific worksheet using the Workbook.getWorksheets().get(index) method.
- Access the PageSetup object of the worksheet using the Worksheet.getPageSetup() method.
- Fit the content of the worksheet to one page using the PageSetup.setFitToPagesTall() and PageSetup.setFitToPagesWide() methods.
- Save the modified workbook to a new file using the Workbook.saveToFile() method.
- Java
import com.spire.xls.ExcelVersion; import com.spire.xls.PageSetup; import com.spire.xls.Workbook; import com.spire.xls.Worksheet; public class FitToPages { public static void main(String[] args) { // Create a Workbook object Workbook workbook = new Workbook(); // Load an Excel file workbook.loadFromFile("Sample.xlsx"); // Get the first worksheet Worksheet sheet = workbook.getWorksheets().get(0); // Get the PageSetup object of the worksheet PageSetup pageSetup = sheet.getPageSetup(); // Fit the content of the worksheet within one page vertically (i.e., all rows will fit on a single page) pageSetup.setFitToPagesTall(1); // Fit the content of the worksheet within one page horizontally (i.e., all columns will fit on a single page) pageSetup.setFitToPagesWide(1); // Save the modified workbook to a new file workbook.saveToFile("FitToPages.xlsx", ExcelVersion.Version2016); workbook.dispose(); } }
Set Headers and Footers in Excel in Java
For instructions on setting headers and footers in Excel, please refer to this article: Java: Add Headers and Footers to Excel.
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.