Javatpoint Logo
Javatpoint Logo

Java Create Excel File

In Java, read excel file and write excel file is a bit tricky because excel worksheet has cells to store data. Java does not provide direct API to read or write Microsoft Excel or Word documents. We have to rely on the third-party library that is Apache POI. In this section, we will learn how to create an excel file in Java and how to write or insert data in the excel file using the Apache POI Java library.

Java Apache POI Library

Apache POI (Poor Obfuscation Implementation) is a Java API for reading and writing Microsoft Documents. It contains classes and interfaces. The Apache POI library provides two implementations for reading or writing excel files:

  • HSSF (Horrible SpreadSheet Format) Implementation:It denotes an API that is working with Excel 2003 or earlier versions.
  • XSSF (XML SpreadSheet Format) Implementation:It denotes an API that is working with Excel 2007 or later versions.

Throughout this section, we have used HSSF implementation.

Create Excel File in Java

Follow the steps given below to create an excel file in Java.

Step 1: Create a Java project in eclipse. We have created a Java project with the name CreateExcelEile.

Step 2: Create a class with the name CreateExcelFileExample1 and write the code that we have written in CreateExcelFileExample1.java file.

Step 3: Download the Apache POI library (poi-3.17.jar).

Step 4: Add the Apache POI to the project. Right-click on the project -> Build Path -> Configure Build Path. It opens the Properties window for the current project.

Step 5: In the Properties window, click on the Add External JARs button.

Java Create Excel File

Step 6: Go to the path where the poi-3.17.jar file is located. Select the JAR file and click on the Open button. It adds the JAR file to the project. After that, click on the Apply and Close button to apply the changes.

Java Create Excel File

When we have done with all the above steps, the project structure looks like the following:

Java Create Excel File

Let's understand the following code.

In the following program, we have used the Apache POI library to create an excel file. The library provides the class named HSSFWorkbook defined in the org.apache.poi.hssf.usermodel package.

CreateExcelFileExample1.java

Output:

Java Create Excel File

It creates a blank excel file at the specified location.

Java Create Excel File

Let's create another Java program that creates an excel file.

CreateExcelFileExample2.java

Output:

Java Create Excel File

It creates a blank excel file at the specified location.

Java Create Excel File

Insert Data in the Excel File

Still, we have created a blank excel file. But it is not always necessary that create a blank excel file only. Some times we need to insert some data in the worksheet. Let's see how to insert data in an excel file through a Java program.

CreateExcelFileExample3.java

Output:

Java Create Excel File

It creates an excel file at the specified location with the values that we have inserted using the setCellValue() method.

Java Create Excel File
Next TopicJava Interpreter




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA