Apache POI reading a xlsx file tutorial

Apache POI is a popular API that allows programmers to create, modify, and display MS Office files using Java programs. This tutorial shows how to read a xlsx file using Apache POI.

employee.xlsx

employee.xlsx

Difference between HSSF and XSSF?

HSSF is the POI Project’s pure Java implementation of the Excel ’97(-2007) file format. XSSF is the POI Project’s pure Java implementation of the Excel 2007 OOXML (.xlsx) file format.

Step 1: Create a simple maven project using the maven “archetype” plugin.

Step 2: import it into eclipse by choosing File –> Import –> Maven –> Existing Maven projects and select the folder “simple-poi” you had just created with Maven.

Step 3: Modify the pom.xml file to add poi dependencies as shown below.

NOTE: “poi-ooxml” dependency is addedd for the “XSSF” to handle xlsx files.

Step 4: A simple stand alone class to read the xlsx file “employee.xlsx” and map the data to “Employee” POJOs.

Note: “XSSFWorkbook” and “XSSFSheet” APIs are used because of xlsx. For “xls”, use “HSSFWorkbook” and “HSSFSheet”.

Output:

Employees = [Employee [id=P-123, name=John, age=24], Employee [id=C-456, name=Peter, age=36]]


300+ Java & Big Data Interview FAQs

16+ Java Key Areas Interview Q&As

800+ Java Interview Q&As

300+ Java & Big Data Tutorials

Top