How to Create Excel File in Java

Sheeraz Gul Feb 02, 2024
  1. Create Excel File in Java
  2. Use Apache POI Library to Create Excel File in Java
How to Create Excel File in Java

This tutorial demonstrates how to create an excel file in Java.

Create Excel File in Java

The excel sheets have cells to store data, but it is tricky to create, read, and write excel files in Java. No built-in library in Java provides the operations related to excel files.

Creating an excel file can be achieved using the Java IO package, but we cannot read and write excel files with the Java IO package. Let’s try an example to create an excel file using the Java IO package:

package delftstack;

import java.io.*;

public class Example {
  public static void main(String[] args) throws IOException, FileNotFoundException {
    String File_Name = "Demodelftstack.xlsx";
    FileOutputStream Excel_File = new FileOutputStream(File_Name);
    Excel_File.close();
    System.out.println("An Excel File has been created.");
  }
}

The code above will create an excel file using the Java IO package. See output:

An Excel File has been created.

Create Excel File Using Java IO

The Apache POI is a third-party API that can perform excel operations, including creating, reading, and writing.

Use Apache POI Library to Create Excel File in Java

The Apache POI (Poor Obfuscation Implementation) is a third-party API to handle Microsoft documents. This API provides two methods that work with different versions of MS Excel:

  1. HSSF (Horrible SpreadSheet Format): Used to work with Excel 2003 and previous versions.
  2. XSSF (XML SpreadSheet Format): Used to work with Excel 2007 and later versions.

Before working with this API, add this API to the build path of your project. Follow the steps below:

  • Download the Apache POI library from here.
  • Right Click on your project and go to Properties.
  • In the Properties, go to Java Build Path.
  • Click Add External Jars.
  • Select the download file.
  • Click Apply and Close.

The above steps will add the Apache POI library to your build path. We can also add the library through maven dependency; add the following dependency to pom.xml of your project:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.2.0</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.2.0</version>
</dependency>

Let’s use the HSSFWorkBook to create an excel file in Java:

package delftstack;

import java.io.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;

public class Example {
  public static void main(String[] args) throws FileNotFoundException, IOException {
    // instance of Workbook class
    Workbook Demo_WorkBook = new HSSFWorkbook();
    // excel file at the specified location
    OutputStream Excel_File = new FileOutputStream("delftstack.xlsx");
    System.out.println("An Excel File has been created.");
    Demo_WorkBook.write(Excel_File);
  }
}

The code above will create an excel file. See output:

Excel File Used Apache POI

An Excel File has been created.
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java Excel