Read Excel File in Java using Apache POI

Apache POI is a very useful library when it comes to reading/writing in Excel. POI stands for Poor Obfuscation Implementation, referring humorously to the fact that the file formats seemed to be deliberately obfuscated, but poorly, since they were successfully reverse-engineered. Ref

Lets see some basic steps on how to Read Excel File in Java using Apache POI

1. Add dependency in your pom.xml for Apache POI

NOTE : For non maven users, you need to download the latest Apache POI jar from here and set it in your classpath along with some other jars.

2. Apache POI basics

Below are some of the useful classes that can be used while reading a excel file

XSSF - Used to reading and writting XLSX format files.
HSSF - Use to read and write Microsoft Excel (XLS) format files.
Workbook: is a high level representation of an Excel workbook. We have 2 implementations of the Workbook
HSSFWorkbook for xls files
XSSFWorkbook for xlsx files
Sheet: high level representation of an Excel worksheet. We also have 2 implementations of the Sheet – HSSFSheet and XSSFSheet.
Row: high level representation of a row in a spreadsheet. HSSFRow and XSSFRow are implementations
Cell: high level representation of a cell in a row. HSSFCell and XSSFCell are implementations classes.

Example

Since we are reading a .xlsx file we are using XSSFWorkbook to read data.
If you need to read a .xls file you can use HSSFWorkbook instead.

We will be reading the sample excel file that has below data
read excel file in java using apache poi

Output

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn