- Example - Home
- Example - Environment
- Example - Strings
- Example - Arrays
- Example - Date & Time
- Example - Methods
- Example - Files
- Example - Directories
- Example - Exceptions
- Example - Data Structure
- Example - Collections
- Example - Networking
- Example - Threading
- Example - Applets
- Example - Simple GUI
- Example - JDBC
- Example - Regular Exp
- Example - Apache PDF Box
- Example - Apache POI PPT
- Example - Apache POI Excel
- Example - Apache POI Word
- Example - OpenCV
- Example - Apache Tika
- Example - iText
- Java Useful Resources
- Java - Quick Guide
- Java - Useful Resources
How to set the print area of a spread sheet using Java
Problem Description
How to set the print area of a spread sheet using Java.
Solution
Following is the program to set the print area of a spread sheet using Java.
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.XSSFPrintSetup;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class SettingPrintAreaToSpreadSheet {
public static void main(String[] args)throws Exception {
//Create a Work Book
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet spreadsheet = workbook.createSheet("Print Area");
//set print area with indexes
workbook.setPrintArea(
0, //sheet index
0, //start column
5, //end column
0, //start row
5 //end row
);
//set paper size
spreadsheet.getPrintSetup().setPaperSize(XSSFPrintSetup.A4_PAPERSIZE);
//set display grid lines or not
spreadsheet.setDisplayGridlines(true);
//set print grid lines or not
spreadsheet.setPrintGridlines(true);
FileOutputStream out = new FileOutputStream(
new File("C:/poiexcel/printarea.xlsx"));
workbook.write(out);
out.close();
System.out.println("printarea.xlsx written successfully");
}
}
Output
java_apache_poi_excel
Advertisements