Creating a Cell at specific position in Excel file using Java

Apache POI can be used to create a cell in a Given Excel file at specific position. Apache POI is an API provided by Apache foundation.

Steps to Create a Cell at specific position in a given Excel File:

  1. Create a maven project(Maven is a build automation tool used primarily for Java projects) in eclipse or a Java project with POI library installed
  2. Add following maven dependency in pom.xml file
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <dependency>
          <groupId>org.apache.poi</groupId>
          <artifactId>poi</artifactId>
          <version>3.12</version>
        </dependency>
        <dependency>
          <groupId>org.apache.poi</groupId>
          <artifactId>poi-ooxml</artifactId>
          <version>3.12</version>
        </dependency>

    chevron_right

    
    

  3. Write java code in javaresource folder
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    import 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;
      
    public class CreateCellAtSpecificPosition {
        public static void main(String[] args) throws FileNotFoundException, IOException
        {
            // Create a workbook instances
            Workbook wb = new HSSFWorkbook();
      
            OutputStream os = new FileOutputStream("Geeks.xlsx");
      
            // Creating a sheet using predefined class provided by Apache POI
            Sheet sheet = wb.createSheet("Company Prepration");
      
            // 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");
      
            // writing the content to Workbook
            wb.write(os);
      
            System.out.println("given cell is created at position (1, 1)");
        }
    }

    chevron_right

    
    

Output

given cell is created at position (1, 1)

Output in Geeks.xlsx File



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.