Creating Sheets in Excel File in Java using Apache POI
Last Updated :
18 Apr, 2023
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 for the job.
Apache POI provides Java API for manipulating various file formats based on the Office Open XML (OOXML) standard and OLE2 standard from Microsoft. Apache POI releases are available under the Apache License (V2.0).
Implementation:
Before we move ahead it is suggested geeks you must be well versed with how to read files in the Apache POI library. it does include fundamental interfaces such as Workbook, Sheet, Row, and Cell. For a given Excel file say here it be 'Geeks.xlsx', it is needed to create sheets in it then do follow these below generic steps as listed below:
Step 1: Create a Java Maven project
Step 2: Add dependency in the pom.xml file. It is as shown below in the media file.
Example
XML
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<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>
Step 3: Create a class in the 'javaResource' Folder.
Java
// Java Program to Illustrate Creating Sheets In Excel File
// Using Apache POI
// Importing required classes
import java.io.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
// Main class
// CreatingSheet
public class GFG {
// Main driver method
public static void main(String[] args)
throws FileNotFoundException, IOException
{
// Creating Workbook instances
Workbook wb = new HSSFWorkbook();
// An output stream accepts output bytes and
// sends them to sink
OutputStream fileOut
= new FileOutputStream("Geeks.xlsx");
// Now creating Sheets using sheet object
Sheet sheet1 = wb.createSheet("Array");
Sheet sheet2 = wb.createSheet("String");
Sheet sheet3 = wb.createSheet("LinkedList");
Sheet sheet4 = wb.createSheet("Tree");
Sheet sheet5 = wb.createSheet("Dynamic Programming");
Sheet sheet6 = wb.createSheet("Puzzles");
// Display message on console for successful
// execution of program
System.out.println(
"Sheets Has been Created successfully");
// Finding number of Sheets present in Workbook
int numberOfSheets = wb.getNumberOfSheets();
System.out.println("Total Number of Sheets: "
+ numberOfSheets);
wb.write(fileOut);
}
}
Output: On console
Sheets Has been Created successfully
Total Number of Sheets: 6
Output: Changes inside the Excel file are depicted in below visual aid provided.

Output explanation:
Here 6 sheets will be created in the Excel file passed in the above program that is 'geeks.xlsx' as shown in the below media provided.
Similar Reads
Creating a Cell at specific position in Excel file using Java 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 po
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 o
2 min read
Opening Existing Excel sheet in Java using Apache POI Apache POI is a powerful API by which one can read, write and modify any Microsoft document like powerpoint, world, or excel. Apache POI have different classes and method to work upon different MS Office Document. POIFS -It's Stand for "Poor Obfuscation Implementation File System". This component i
2 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 read
5 min read
How to Create a Formula in Excel using Java? Apache POI is a popular open-source Java library that provides programmers with APIs for creating, modifying, and editing MS Office files. Excel is very excellent at calculating formulas. And perhaps most Excel documents have formulas embedded. Therefore, itâs trivial that on a fine day, you have to
3 min read