Java Read and Write Excel File Using Apache POI

Apache POI provides libraries for reading and writing excel file in Java. There are other libraries available in market but Apache POI is most popular and reliable among them.

In this tutorial I will show you how you can read or write data to excel file in Java using Apache POI Library.

There are few things that you should know about this library.

  • Class prefixed by HSSF are used to perform operations on MS Excel 2003 file. For example, HSSFWorkbook and HSSFSheet. The file format for excel 2003 file is XLS.
  • Class prefixed by XSSF are used to perform operations on MS Excel 2007 or later versions file. For example, XSSFWorkbook and XSSFSheet. The file format for excel 2007 and later versions file is XLSX.
  • HSSFWorkbook and XSSFWorkbook class represents excel workbook.
  • HSSFSheet and XSSFSheet class represents excel worksheet.
  • Row and Cell class represents excel row and column.

Download Apache POI Library

Go to below link and download the binary distribution of latest version.

https://poi.apache.org/download.html

You will get a compressed file. Just extract it and import all the jars present in it to your project.

Writing to Excel File in Java

Below example shows how you can perform write operation on an excel file. Here I am saving data of some users in an excel file.

After writing the excel file will look like as show in below image.

Java Read and Write Excel File Using Apache POI

Reading from Excel File in Java

In this example I am simply reading the excel file that I have generated in previous example.

Output

Name === Age === Gender ===
Neeraj === 22 === Male ===
Pankaj === 25 === Male ===
Sneha === 20 === Female ===

Comment below if you have any queries related to above read and write excel file example in java.

Leave a Reply

Your email address will not be published. Required fields are marked *