The Wayback Machine - https://web.archive.org/web/20240616084249/https://www.geeksforgeeks.org/creating-a-cell-at-specific-position-in-excel-file-using-java/
Open In App

Creating a Cell at specific position in Excel file using Java

Last Updated : 30 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 

  1. 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
  2. Add the following maven dependency in the pom.xml file
  3. Write java code in javaresource folder

Example

Java




// Java Program to Demonstrate Creation Of Cell
// At Specific Position in Excel File
 
// Importing required classes
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;
 
// Class
// CreateCellAtSpecificPosition
public 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’  



Similar Reads

Creating Sheets in Excel File in Java using Apache POI
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. For Example, java doesn’t provide built-in support for working with excel files, so we need to look for open-source APIs
3 min read
How to Create a File with a Specific File Attribute in Java?
In Java, you can create a file with specific File attributes such as read-only, hidden, or system attributes. This allows you to control the behavior and visibility of the file in the File system. In this article, we'll explore how to create a file with specific attributes in Java. In this article, we will learn to create a file with a specific fil
2 min read
How to Create Formula Cell in Excel Sheet using Java and Apache POI?
In the previous article, we have seen how to read data from the formula cell, here we are going to create the formula cell using Apache POI. In this article, we are creating an Excel file with three columns consisting of values and the last column is the formula cell which is calculated from the other cells by defining a certain formula, here we ar
2 min read
How to Read Data From Formula Cell in Excel Sheet using Selenium and Apache POI?
The Apache POI is one of the most popular java libraries for selenium data-driven testing. It is mainly used for handling all types of Microsoft documents such as MS Word, and MS Excel. MS word and others. Apache POI is an open-source library and POI stands for "Poor Obfuscation Implementation". Getting Started with Apache POI We need to download t
2 min read
How to Write Data from Excel File into a HashMap using Java and Apache POI?
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. For Example, Java doesn’t provide built-in support for working with excel files, so we need to look for open-source APIs
3 min read
Reading and Writing Data to Excel File in Java using Apache POI
In Java, reading an Excel file is not similar to reading a Word file because of cells in an Excel file. JDK does not provide a direct API to read data from Excel files for which we have to toggle to a third-party library that is Apache POI. Apache POI is an open-source java library designed for reading and writing Microsoft documents in order to cr
5 min read
How to Create a File with a Specific charset in Java?
In Java, when we write text to a file, we must provide the character encoding to create a file with a specified charset. Classes to generate Java Files with Specified CharsetUsing a FileOutputStream and the OutputStreamWriter class we can generate a Java file with a specified charset. FileOutputStream: It is an OutputSream class used for storing da
2 min read
How to Create a File with a Specific Owner and Group in Java?
Java is a basic tool that does not directly let you choose who owns a file or which group it belongs to when you create it. You have to rely on other methods or external libraries if you need to control these aspects. In this article, we will learn How to create a file with a specific owner and group in Java. Approaches to Create a File with Specif
3 min read
Creating a file using FileOutputStream
FileOutputStream class belongs to byte stream and stores the data in the form of individual bytes. It can be used to create text files. A file represents storage of data on a second storage media like a hard disk or CD. Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a
5 min read
How to Execute SQL File with Java using File and IO Streams?
In many cases, we often find the need to execute SQL commands on a database using JDBC to load raw data. While there are command-line or GUI interfaces provided by the database vendor, sometimes we may need to manage the database command execution using external software. In this article, we will learn how to execute an SQL file containing thousand
5 min read
Article Tags :
Practice Tags :