How to read and write excel file using Apache POI

 

Here, I am going to explain how to read and write excel file using Apache POI, the example which you are going to see is implemented by considering Microsoft Excel 2007 version or later if you have requirement to support prior version then you need to use HSSF version instead of XSSF

Step 1: load maven dependency

You must add below apache poi dependency in your pom.xml file

Step 2: create excel file

The below code will help you to understand how to create a sheet, row and cell in excel  

I am going to create a new sheet in the excel file and going to store some dummy data, in the below code using setExcelFile method I am loading my excel file, then creating new sheet in the excel file called “Screen4” and finally storing data in the newly created row and cell in the sheet

Finally, if you open your excel file then you should something like below

excel-create-sheet

Step 3: read excel file

I am going to read same excel file whatever I have created earlier and the below helper class will help us to read excel file

The below code loads excel file then reads every row  and get the cell data

Download Code

 apache-poi-excel-demo