Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaCreating Sheets in Excel File in Java using Apache POI

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 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




<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.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments