Excel Freeze Pane Feature
In this section, you will learn about freezing row of excel document using Apache POI library.
Sometimes when you scroll down too far, we lost the heading at top. Due to this, we feel difficulty to relate which cell belongs to which heading. The solution of this problem is Freeze Pane which freezes the the row you want to retain at it's place.
Given below code will freezes the top row and all other row can scroll up or scroll down :
import java.io.FileOutputStream; import java.io.IOException; 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 XLfreezePanes { public static void main(String args[]) throws IOException { Workbook wb = new XSSFWorkbook(); CreationHelper createHelper = wb.getCreationHelper(); Sheet sheet1 = wb.createSheet("new sheet"); Row row = sheet1.createRow((short) 0); // Create a cell and put a value in it. row.createCell(0).setCellValue( createHelper.createRichTextString("THIS IS FREEZED ROW")); for (int i = 1; i < 20; i++) { row = sheet1.createRow((short) i); row .createCell(0) .setCellValue( createHelper .createRichTextString("This is the Moving Row")); } // Freeze just one row sheet1.createFreezePane(0, 1, 0, 1); FileOutputStream fileOut = new FileOutputStream( "xlsx/FixedWorkbook.xlsx"); wb.write(fileOut); fileOut.close(); } }
In the function createFreezePane , the first two parameters are the columns and rows you wish to split by. The second two parameters indicate the cells that are visible in the bottom right quadrant.
OUTPUT
In the below example, row 1freezes and all other can scroll up or down :