Skip to content
Related Articles

Related Articles

How to Create Different Types of Cells in a Spreadsheet using Java?
  • Last Updated : 07 Jan, 2021

Apace POI is an API which was provided by Apache Foundation and is used to set the various type of values to the cell. The overloading function setCellValue() of Apache POI is used to set the value type of the cell. Maven is a powerful project management tool that is based on POM (project object model). It is used for project build, dependency, and documentation. It simplifies the build process like ANT. But it is too much advanced than ANT.

Algorithm: Steps To Create Different Types of Cells In A Spreadsheet 

  • Step 1: Create an instance of the workbook
  • Step 2: Create a spreadsheet in the above workbook.
  • Step 3: Create rows using XSSFRow
  • Step 4: Writing the content to the workbook by defining the object of type  FileOutputStream
  • Step 5: Close the file


Step 1: Create a Maven project in eclipse and add Apache POI(used for setting value type of cells) dependencies to it or a Java project with Apache POI library installed.

Step 2: As in creating a Maven project, prerequisites are required to add the following Maven dependencies to pom.xml file, in order to use the Apache POI library. For these libraries search online for Maven Central Library and then search for Apache POI and get the dependencies or can also copy the following dependencies as shown below to the main java program.



Step 3: After adding dependencies create a new class. For creating a new class go to src/main/java/package right-click and create a new class and name it, here for illustration purpose file is names as GFG. . In this class we will first create an instance of the workbook and with that instance, we will create a spreadsheet and in that spreadsheet, we will define the value type of cells. Below is the code snippet is given for the GFG class(taken here) as discussed above.

Implementation: Code snippet

Example: To create different types of cells in a spreadsheet


// Java Program to create 
// different types of cells in a spreadsheet
// Importing Maven dependenncies
// Importing Apache POI dependency files
// Importing java input/output file and
// Date class from java.util package
import java.util.Date;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
// Class having different types of cells
public class GFG {
    // Main driver method
    public static void main(String[] args) throws Exception
        // Step 1: Create an instance of the workbook
        XSSFWorkbook workbook = new XSSFWorkbook();
        CreationHelper creationHelper
            = workbook.getCreationHelper();
        // Step 2: Create a spreadsheet in the above
        // workbook
        // and name it as CellTypesSheet
        XSSFSheet spreadsheet
            = workbook.createSheet("CellTypesSheet");
        // Step 3: Create rows using XSSFRow
        // We need to create row before creating cells,
        // row is a collection of cells
        XSSFRow noOfRows = spreadsheet.createRow((short)2);
        // Creating cells
        // Custom inputs in cells
        // Cell 1
        noOfRows.createCell(0).setCellValue("Cell Types");
        // Cell 2
        noOfRows.createCell(1).setCellValue("Cell Value");
        // Above two cells(Cell Types and Cell Values) are
        // created at row 3 in excel and similarly for rest
        // of the cells
        noOfRows = spreadsheet.createRow((short)3);
        // Cell 3
        // Setting a Blank type cell
            "Cell Type-BLANK");
        noOfRows = spreadsheet.createRow((short)4);
        // Cell 4
        // Setting a Boolean type cell
            "Cell Type-BOOLEAN");
        noOfRows = spreadsheet.createRow((short)5);
        // Cell 5
        // Setting a Error type cell
            "Cell Type-ERROR");
        noOfRows = spreadsheet.createRow((short)6);
        // Cell 6
        // Setting a Date and Time type cell
        CellStyle cellStyle = workbook.createCellStyle();
                "d/m/y h:mm"));
            "Cell Type-DATE-TIME");
        noOfRows.createCell(1).setCellValue(new Date());
        noOfRows = spreadsheet.createRow((short)7);
        // Cell 7
        // Setting a Numeric type cell
            "Cell Type-Numeric");
        noOfRows = spreadsheet.createRow((short)8);
        // Cell 8
        // Setting a String type cell
            "Cell Type-String");
        // Step 4: Writing the content to the workbook
        // by defining object of type  FileOutputStream
        FileOutputStream out = new FileOutputStream(
            new File("GfgTypesOfCells.xlsx"));
        // Step 5: Close the file
        // Display message to be printed for successful
        // execution of program
        System.out.println("GFG.xlsx Created Successfully");

Output: A message will be printed in console as specified at last in the program that “GFG.xlsx Created Successfully” as the successful execution of the program responsible for changes in the spreadsheet below:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :