Java: Set Excel Print Options Through Page Setup

The Excel print options (also known as sheet options) allow you to control the print options when printing Excel documents. Spire.XLS for Java offers the PageSetup class to set the print options, such as print area, print titles and print order. This article will demonstrate how to set different printing settings using Spire.XLS for Java from the following aspects:

  • Set the print area in Excel
  • Print titles in Excel
  • Print gridlines in Excel
  • Print comments in Excel
  • Print Excel in black and white mode
  • Set print quality
  • Set the print order of worksheet pages

Install Spire.XLS for Java

First, 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>14.11.0</version>
    </dependency>
</dependencies>
    

Setting Excel Print Options via Page Setup

The detailed steps of controlling the Excel printing settings are as follows.

  • Create a Workbook object.
  • Load a sample Excel document using Workbook.loadFromFile() method.
  • Get a specified worksheet using Workbook.getWorksheets().get() method.
  • Get the PageSetup object of the first worksheet.
  • Select a specific print area of a worksheet using PageSetup.setPrintArea() method.
  • Set the rows to repeat at top when printing using PageSetup.setPrintTitleRows() method.
  • Set printing with gridlines using PageSetup.isPrintGridlines(true) method.
  • Set printing with comments using PageSetup.setPrintComments() method.
  • Print worksheet in black & white mode using PageSetup.setBlackAndWhite(true) method.
  • Set the printing quality using PageSetup.setPrintQuality() method.
  • Set the printing order using PageSetup.setOrder() method.
  • Save the document to another file using Workbook.saveToFile() method.
  • Java
import com.spire.xls.*;

public class pageSetupForPrinting {

    public static void main(String[] args) throws Exception {

        //Create a workbook
        Workbook workbook = new Workbook();

        //Load the Excel document from disk
        workbook.loadFromFile("Sample.xlsx");

        //Get the first worksheet
        Worksheet worksheet = workbook.getWorksheets().get(0);

        //Get the PageSetup object of the first worksheet
        PageSetup pageSetup = worksheet.getPageSetup();

        //Specifying the print area
        pageSetup.setPrintArea("A1:D10");

        //Define row numbers 1 as title rows
        pageSetup.setPrintTitleRows("$1:$2");

        //Allow to print with row/column headings
        pageSetup.isPrintHeadings(true);

        //Allow to print with gridlines
        pageSetup.isPrintGridlines(true);

        //Allow to print comments as displayed on worksheet
        pageSetup.setPrintComments(PrintCommentType.InPlace);

        //Set printing quality
        pageSetup.setPrintQuality(150);
        
        //Allow to print worksheet in black & white mode
        pageSetup.setBlackAndWhite(true);
        //Set the printing order
        pageSetup.setOrder(OrderType.OverThenDown);

        //Save the document to file
        workbook.saveToFile("PagePrintOptions.xlsx", ExcelVersion.Version2016);
    }
}

Java: Set Excel Print Options Through Page Setup

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.