How to Read Excel Files in Java using Apache POI
Apache POI library
Apache POI is the pure Java API for reading and writing Excel files in both formats XLS (Excel 2003 and earlier) and XLSX (Excel 2007 and later).
To use Apache POI in Java project:
For non-Maven projects:
Download the latest library from here: Apache POI – Download Release Artifacts
Extract the zip file and add the appropriate JAR files to the project’s classpath:
- If reading and writing only Excel 2003 format, only the file poi-VERSION.jar is enough.
- If reading and writing Excel 2007 format, we have to include the following files:
For Maven projects:
Add the following dependency to the project’s pom.xml file:
- For Excel 2003 format only(XLS)
- For Excel 2007 format(XLSX)
Note: VERSION need to replace with the POI version that you are using(for example: 3.9)
The Apache API Basics
There are two main prefixes which we will encounter while working with Apache POI APIs:
HSSF: denotes the API is for working with Excel 2003 and earlier.
XSSF: denotes the API is for working with Excel 2007 and later.
To get started the Apache POI API, we just need to understand and use the following 4 interfaces:
- Workbook: high-level representation of an Excel workbook. Implementation classes are:
- Sheet: high-level representation of an Excel worksheet. Implementation classes are:
- Row: high-level representation of a row in a spreadsheet. Implementation classes are:
- Cell: high-level representation of a cell in a row. Implementation classes are:
Let’s see the example how to read excel(xlsx) file using the Apache POI library. Below is the sample of excel file that wants to read
Below is the code to read the file:
Run the program and see the output as below: