Setting Print Area for Excel Sheet using Apache POI in Java

Introduction

In this post we will see how to set print area for excel sheet using apache poi in Java. There may be many number of columns and rows in your excel sheet but you need only few columns and rows to be printed in paper instead of all columns and rows from the sheet then you need to set the print area for the sheet.

Prerequisites

Eclipse 4.12, Java 12 or 8, Gradle 5.6, Apache POI 4.1.0

Creating Project

Create a gradle based project in Eclipse. The project name is excel-poi-sheet-print-area.

Updating Build Script

We will update the default generated build.gradle script to include the required dependency.

If you are using Java 8 then you need to change the Java version for source and target compatibility in the below build script.

plugins {
    id 'java-library'
}

sourceCompatibility = 12
targetCompatibility = 12

repositories {
    jcenter()
}

dependencies {
    implementation 'org.apache.poi:poi-ooxml:4.1.0'
}

Java Class to Set Print Area

Now we will create a class in Java programming language to set print area in excel sheet.

You need to change the file path according to your file location because we are working on existing excel file instead of creating our own.

If you want then you may create the excel sheet with data and set the print area for your sheet.

package com.roytuts.excel.poi.sheet.print.area;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class ExcelSheetPrintArea {

	public static void main(String[] args) throws FileNotFoundException, IOException {
		InputStream fis = new FileInputStream(new File("C:/jee_workspace/sample.xlsx"));

		Workbook wb = WorkbookFactory.create(fis);

		wb.setPrintArea(0, 0, 4, 0, 5);

		OutputStream fileOut = new FileOutputStream("C:/jee_workspace/sample-print-area.xlsx");

		wb.write(fileOut);

		wb.close();

	}

}

Notice we create Workbook object from WorkbookFactory to avoid checking whether the excel file is .xls or .xlsx format. The WorkbookFactory automatically handles it.

The important method in the above code is setprintArea() on Workbook object.

The first parameter’s value indicates the sheet index. So we put 0 here and it means it’s a first sheet in the excel file.

The second parameter indicates the start index of the column, here it is first column (index value 0).

The third parameter is end index of the column, here it is fifth column (index value 4).

Related Posts:

The fourth parameter is start index of the row, here it is first row (index value 0).

The fifth parameter is end index of the row, here it is sixth row (index value 5).

Testing the Application

When you open the output excel file and try to print then you will see similar output to the below image:

print area for excel sheet using apache poi in java

So it is clear from the above output that we have set print area for excel sheet with 5 columns and 6 rows.

Source Code

download source code

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *