Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaJava Program to Create blank Excel Sheet

Java Program to Create blank Excel Sheet

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 ‘xlsx’ extension files and as well as ‘xlsx’ 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. 

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 on to 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 ‘XSSF 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
// Importing generic java libraries
import java.io.File;
// Importing File libraries
import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.*;
 
public class GFG {
 
    // Main driver method
    public static void main(String[] args) throws Exception
    {
        // Creating WorkBook
        XSSFWorkbook workbook = new XSSFWorkbook();
 
        // Creating Spreadsheet by creating an object of
        // XSSFSheet and also give name
        XSSFSheet spreadsheet
            = workbook.createSheet("Sheet1");
 
        String Location = "C:\\blankExcel.xlsx";
        // Place the output file in location
        FileOutputStream outputfile
            = new FileOutputStream(Location);
 
        // Write to workbook
        workbook.write(outputfile);
 
        // Close the output file
        outputfile.close();
 
        // Display message for console window when
        // program is successfully executed
        System.out.println(
            "blankExcel.xlsx is written successfully");
    }
}


Output: On the console window

blankExcel.xlsx is written successfully

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

 

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