Apache POI is an open-source java library to create and manipulate various file formats based on Microsoft Office. Using POI, one should be able to perform create, modify and display/read operations on the following file formats/ it can be used to create a cell in a Given Excel file at a specific position. Apache POI is an API provided by the Apache foundation.
Steps to Create a Cell at a Specific position in a given Excel FileÂ
- Create a maven project(Maven is a build automation tool used primarily for Java projects) in eclipse or a Java project with the POI library installed
- Add the following maven dependency in the pom.xml file
- Write java code in javaresource folder
Example
Java
// Java Program to Demonstrate Creation Of Cell// At Specific Position in Excel FileÂ
// Importing required classesimport java.io.*;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;Â
// Class// CreateCellAtSpecificPositionpublic class GFG {Â
    // Main driver method    public static void main(String[] args)        throws FileNotFoundException, IOException    {Â
        // Creating a workbook instances        Workbook wb = new HSSFWorkbook();Â
        // Creating output file        OutputStream os            = new FileOutputStream("Geeks.xlsx");Â
        // Creating a sheet using predefined class        // provided by Apache POI        Sheet sheet = wb.createSheet("Company Preparation");Â
        // Creating a row at specific position        // using predefined class provided by Apache POIÂ
        // Specific row number        Row row = sheet.createRow(1);Â
        // Specific cell number        Cell cell = row.createCell(1);Â
        // putting value at specific position        cell.setCellValue("Geeks");Â
        // Finding index value of row and column of given        // cell        int rowIndex = cell.getRowIndex();        int columnIndex = cell.getColumnIndex();Â
        // Writing the content to Workbook        wb.write(os);Â
        // Printing the row and column index of cell created        System.out.println("Given cell is created at "                           + "(" + rowIndex + ","                           + columnIndex + ")");    }} |
Output: On consoleÂ
Given cell is created at (1,1)
Output: Inside file named ‘Geeks.xlsx’ Â

