How to Set the Print Area of a Spreadsheet Using Java?
Last Updated :
19 Jan, 2021
We can set the print area in a Spreadsheet. This can be done using the Apache POI library of Java. We use the different classes and methods of POI library to set a print area of the spreadsheet. Usually, it takes from top left to the bottom right of the Excel Spreadsheets. It can be customized as per the requirement. We can print a particular range of cells from the whole spreadsheet, customize the paper size, print the contents with the grid lines turned on, etc.
Create a new workbook
We can create a new workbook and a spreadsheet using the various POI classes. First, we have to create a workbook then we create a spreadsheet in that workbook. Here we create a workbook object named ‘workbook’ using XSSFWorkBook class. Then we created a spreadsheet named as ‘Print Area’ using the createSheet() function of XSSFSheet class.
To create the workbook:
// Create a Work Book
XSSFWorkbook workbook = new XSSFWorkbook();
To create the spreadsheet in that workbook:
// Create spreadsheet named "Print Area"
XSSFSheet spreadsheet = workbook.createSheet("Print Area");
Setup of Print Area
Now we set up the print area using the setPrintArea() function. In this function, we send the five values.
workbook.setPrintArea(
0, // sheet index
0, // start column
5, // end column
0, // start row
5 // end row
);
We can display the grid lines in the print area by using the setDisplayGridlines() function.
// set display grid lines or not
spreadsheet.setDisplayGridlines(true);
To set the paper size, we use the setPaperSize() function.
// set paper size
spreadsheet.getPrintSetup().setPaperSize(XSSFPrintSetup.A4_PAPERSIZE);
If we want to print the gridlines also with our content, we can do that by the setPrintGridlines() function.
// set print grid lines or not
spreadsheet.setPrintGridlines(true);
Below is the implementation of the problem statement:
Java
import java.io.*;
import org.apache.poi.xssf.usermodel.XSSFPrintSetup;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class GFG {
public static void main(String[] args) throws Exception
{
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet spreadsheet
= workbook.createSheet( "Print Area" );
workbook.setPrintArea( 0 ,
0 ,
5 ,
0 ,
5
);
spreadsheet.setDisplayGridlines( true );
spreadsheet.getPrintSetup().setPaperSize(
XSSFPrintSetup.A4_PAPERSIZE);
spreadsheet.setPrintGridlines( true );
FileOutputStream out = new FileOutputStream(
new File( "GFGSpreadsheet.xlsx" ));
workbook.write(out);
out.close();
System.out.println(
"GFGSpreadsheet.xlsx has been created" );
}
}
|
Output:
After we compile the above program, it will create a ‘GFGSpreadsheet.xlsx’ file
in your computer. Then in this file, a workbook is created and in that workbook
a Spreadsheet named “Print Area” is created.

Then we selected the rows and columns we have to print and some setups we applied to the print area.
Then after all that got executed we get the following confirmation line as mentioned in the code.
GFGSpreadsheet.xlsx has been created
As in our code, we have not set any values for the cells, so the file is a blank file. In the print preview tab, the cells we have selected for printing will be empty. Although as per our code, we have written the code to display the gridlines in the print area.
Final Output of our code shown below:

Similar Reads
How to Create Different Types of Cells in a Spreadsheet using Java?
Apache POI is an API which was provided by Apache Foundation and is used to set the various type of values to the cell. The overloading function setCellValue() of Apache POI is used to set the value type of the cell. Maven is a powerful project management tool that is based on POM (project object mo
4 min read
How to Apply Different Styles to a Cell in a Spreadsheet using Java?
Apache POI is a powerful API that enables the user to create, manipulate, and display various file formats based on Microsoft Office using java programs. Using POI, one should be able to perform create, modify, and display/read operations on the following file formats. For Example, Java doesnât prov
6 min read
How to Set Direction to the Text in Cell using Java?
Apache POI is an open-source library by Apache which can be used to create, modify and display files MS office file in Java. It provides classes and methods to do so. This API provides various components such as POIFS (Poor Obfuscation Implementation File System), HSSF (Horrible Spreadsheet Format),
3 min read
Java Program to Find out the Area and Perimeter of Rectangle using Class Concept
The perimeter is the length of the outline of a shape. To find the perimeter of a rectangle or square you have to add the lengths of all four sides. x is in this case the length of the rectangle while y is the width of the rectangle. The area is the measurement of the surface of a shape. The main ta
3 min read
How to Find the Area of any Polygon Using Triangulation in Java?
Polygons are basic shapes in geometry and can have many sides. Finding the Area of a Polygon is a complex task, So to perform this complex task we have a method called Triangulation. Triangulation is a method of dividing a polygon into triangles and then finding the area of each triangle to get the
4 min read
How to Print an Array in Java Without using Loop?
Given an array arr in Java, the task is to print the contents of this array without using any loop. First let's see the loop method. Loop method: The first thing that comes to mind is to write a for loop from i = 0 to n, and print each element by arr[i].Pseudo Code: for(int i = 0; i < Array.lengt
2 min read
Java Program to Draw a Shape in Excel Sheet using Apache POI
Apache POI supports customized printing by allowing users to select a range of cells in order to configure the desired print area in a worksheet using Java Program. The top-most shape is the patriarch. This is not visible on the sheet at all. To start drawing you need to call createPatriarch on the
4 min read
Setting the Position of the Image in PDF Document using Java
To set the Position of the Image in a PDF document using Java multiple external dependencies need to download first. Setting the Position of the Image in a PDF, use the iText library. These are the steps that should be followed to Set the Position of the Image in a PDF using java. 1. Creating a PdfW
3 min read
Adding Nested Tables to a PDF using Java
We can add nested tables to a PDF by installing the document class. While instantiating this class, you would like to pass a PdfDocument object as a parameter, to its constructor. Then, to feature a table to the document, you would like to instantiate the Table class and add this object to the docum
4 min read
Java Program to Find the Area of a Circle Given the Radius
A circle is a simple shape consisting of all the points in the plane that are equidistant from a point known as the center of the circle. In this article, we will learn how to find the area of the circle. Terminology: Area: A quantity that represents the extent of a 2-dimensional figure or shape in
2 min read