Set Zoom in Excel - Java POI Example Program

Zooming Excel Worksheets - Introduction


In this tutorial, we will explain how to magnify / zoom an Excel worksheet in Java, using Apache POI library. Depending on the amount of data you load into your worksheet, you may often find yourself to zoom in or zoom out your worksheet by a percentage value, as it would give a better readability to your audience.Apache POI library's smart method setZoom helps you to control the zoom factor in a worksheet. Let us now visit this method with suitable examples.

setZoom - Controlling Zoom Factor in Worksheets


The method setZoom helps you to control the magnification of the worksheet. This method takes two integer values through which you set the zoom percentage value. For example, to set a zoom of 50%, you pass 1 and 2 to this method. POI automatically handles the zoom then when you write the worksheet to a file. Here are some examples explaining this.

XLS - Set Zoom - Java POI Example Program


The Java example program to set the zoom factor in a XLS worksheet is provided below:

import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.*;
import org.apache.poi.ss.usermodel.ComparisonOperator;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.util.CellRangeAddress;
public class zoomWorksheet {  
        public static void main(String[] args) throws Exception{
                /* Create Workbook and Worksheet - Add Input Rows */
                HSSFWorkbook my_workbook = new HSSFWorkbook();
                HSSFSheet my_sheet = my_workbook.createSheet("Zoom_Example");           
                /* Add header rows */
                Row row0 = my_sheet.createRow(0);               
                row0.createCell(0).setCellValue("Cell 1");
                row0.createCell(1).setCellValue("Cell 2");
                row0.createCell(2).setCellValue("Cell 3");              
                /* set the zoom to 50% */
                my_sheet.setZoom(1,2); // You can send 3,6 as well :)           
                FileOutputStream out = new FileOutputStream(new File("C:\\zoom_example.xls"));
                my_workbook.write(out);
                out.close();
        }
}

The output of the program is shown below:

Excel - Control Zoom - setZoom- Java Example Program - Output
Excel - Control Zoom - setZoom- Java Example Program - Output

XLSX - Magnify Worksheet - Java Example Program


Let us try to zoom out in this example. Here is a Java program that zooms an Excel workbook to 150%.

import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
public class zoomWorksheetXLSX {  
        public static void main(String[] args) throws Exception{
                /* Auto filter for xlsx workbooks */
                XSSFWorkbook my_workbook = new XSSFWorkbook();
                XSSFSheet my_sheet = my_workbook.createSheet("zoom_xlsx");
                /* Create Test Data */          
                Row row0 = my_sheet.createRow(0);               
                row0.createCell(0).setCellValue("out");
                row0.createCell(1).setCellValue("in");
                row0.createCell(2).setCellValue("test data");
                my_sheet.setZoom(3,2); //sets zoom to 150%
                /* Write changes to the workbook */
                FileOutputStream out = new FileOutputStream(new File("C:\\zoom_worksheet.xlsx"));
                my_workbook.write(out);
                out.close();
        }
}

The output of this program is shown below:

XLSX - Magnify Worksheet - setZoom- Java Example Output
XLSX - Magnify Worksheet - setZoom- Java Example Output
That completes a quick tutorial to control zoom in an Excel worksheet using Java and Apache POI library. If you have any questions around this, you can post it in the comments section of this blog.

No comments:

Post a Comment