Set Zoom Magnification of Excel Sheet
In this section, you will learn how to set the zoom magnification of a Excel workbook's sheet.
Zoom magnification is expressed as fraction. For example if you want to set the magnification to 75%, you need to implement 3 for the numerator and 4 for the denominator.
You can set the zoom magnification of a sheet as follows :
sheet1.setZoom(3,4); // 75% magnification
The complete example is given below :
EXAMPLE
In the below code, an Excel(.xlsx) workbook's sheet's magnification is set to 75%.
import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CreationHelper; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelMagnification { public static void main(String[] args) throws IOException { Workbook wb = new XSSFWorkbook(); CreationHelper createHelper = wb.getCreationHelper(); Sheet sheet = wb.createSheet("new sheet"); // SETTING ZOOM MAGNIFIACTION sheet.setZoom(3, 4); // Create a row and put some cells in it. Rows are 0 based. Row row = sheet.createRow((short) 0); // Create a cell and put a value in it. Cell cell = row.createCell(0); cell.setCellValue(1); // Or do it on one line. row.createCell(1).setCellValue(1.2); row .createCell(2) .setCellValue( createHelper .createRichTextString("This Sheet's Zoom magnification is set to 75%")); row.createCell(3).setCellValue(true); // Write the output to a file FileOutputStream fileOut = new FileOutputStream("xlsx/workbook2.xlsx"); wb.write(fileOut); fileOut.close(); } }
OUTPUT
The content of the workbook sheet is given below whose magnification is set to 75% :
B | C | D | |
1 | 1.2 | This is a string | TRUE |