Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Java Program to Create blank Excel Sheet

  • Last Updated : 24 Nov, 2020

ApachePOI stands for poor obfuscation Implementation which is a Java API for reading and writing Microsoft documents. It contains classes and interfaces namely Wordbook, Sheet, Row, and Cell. Apache POI can be used to access ‘xls’ extension files and as well as ‘xlls’ extension files

Concept: ApachePOI, jar files and File methods are explained to a brim to show how they are connected and the procedural execution intercepting directories. 

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.

Step 1: To get the project ready to code. Once the package is created, still certain jar files are needed to access the Apache so do download the jar files before reading further. There are 4 jar files needed to access Excel using java or simply import all jar files of ‘usermodel’ during the java project creation. 

Step 2: Create a new class and pay attention if any warning is showing that if anything is missing out or replacement issue. Once the new class is successfully created move onto the next step.



Step 3: import all the required files to interact with the java program with system libraries as per the demand. So, by now the first job would be to deal with file methods in the java program which later on is to build and run. For this FileInputStream concepts will be used to deal with the java program and secondary to create an object of type ‘XLLS wordbook’. 

Java provides an in-built package ‘org.apache.poi.xssf.usermodel’ to create and work with blank Excel documents and files. This package contains a class XSSFWorkbook which can be used to create and process blank spreadsheet workbooks. The class also provides functionality to read, write, and work with new or existing sheets. Above here other packages required are the File and FIleOutputStream to create a file and then open a connection. It also facilitates the modification of contents, that is, appending, deleting, etc. from the file in Java.

The random directory chosen where a blank Excel file is created is C:/blankExcel.xlsx”.

Implementation:

Java




// Importing Excel interface
import org.apache.poi.xssf.usermodel.*;
// Importing generic java libraries
import java.io.File;
// Importing File libraries
import java.io.FileOutputStream;
  
public class GFG {
  
    // Main driver method
    public static void main(String[] args) throws Exception
    {
  
        // Create a blank excel workbook using
        // XSSFWorkBook interface in java
        XSSFWorkbook excel_sheet = new XSSFWorkbook();
  
        // Create a new file at the specified path location
        File file = new File("C:/blankExcel.xlsx");
  
        // Create a connection using the specified file
        FileOutputStream ostream
            = new FileOutputStream(file);
  
        // write operation on the file
        excel_sheet.write(ostream);
        // close connection
        ostream.close();
    }
}

Output: The above code creates a blank Excel file in the Windows directory named‚ÄĚC:/ blankExcel.xlsx”




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!