Read Excel File in Java Using Apache POI

In this article we’ll see how to read Excel file in Java using Apache POI library.  If you have to write to an Excel file in Java you can check this post- Write to Excel File in Java Using Apache POI

Apache POI

Apache POI is a Java API for Microsoft documents. Using Apache POI you can read and write MS Excel files using Java. In addition, you can read and write MS Word and MS PowerPoint files using Java.

Apache POI provides support for reading both OLE2 files and Office Open XML standards (OOXML) files.

  • OLE2 files include most Microsoft Office files such as XLS, DOC, and PPT as well as MFC serialization API based file formats.
  • Office OpenXML Format is the new standards based XML file format found in Microsoft Office 2007 and 2008. This includes XLSX, DOCX and PPTX.

Which means Apache POI supports reading excel files having .xls format as well as excel files with .xlsx format.

Maven dependencies for Apache POI

To read excel files using Apache POI you need to add the following dependency.


This dependency adds the following jars-


Here note that poi-4.0.1.jar has classes for working with old excel format (.xls)
and poi-ooxml-4.0.1.jar has classes for working with newer excel format (.xlsx). Apache POI 4.0.1 requires Java 8 or newer.

Apache POI classes for reading excel files

Here is a primer about the classes that are used for reading excel file in Java using Apache POI.

Apache POI library provides two implementations for two excel spread sheet formats-

  • HSSF– It is the pure Java implementation for earlier excel format (.xls). Classes in this implementation will usually have HSSF prefix like HSSFWorkBook, HSSFSheet.
  • XSSF– It is the pure Java implementation for xslx file format (OOXML). Classes in this implementation will usually have XSSF prefix like XSSFWorkBook, XSSFSheet.

SS– It is a package built on top of HSSF and XSSF that provides common support for both formats with a common API. You should try to use classes from this package for better compatibility.

While working with excel file the usual progression is-

read excel file in Java

There are interfaces corresponding to this progression-

  •– High level representation of a Excel workbook. This is the first object most users will construct whether they are reading or writing a workbook. It is also the top level object for creating new sheets.
  •– High level representation of a Excel worksheet. Sheets are the central structures within a workbook.
  •– High level representation of a row of a spreadsheet.
  •– High level representation of a cell in a row of a spreadsheet. Cells can be numeric, formula-based or string-based (text).

For creating a Workbook, WorkbookFactory class is used.

  •– Factory for creating the appropriate kind of Workbook (be it HSSFWorkbook or XSSFWorkbook), by auto-detecting from the supplied input.

Read excel file in Java using Apache POI example

In this Java example to read excel file we’ll read the following sheet (Employee.xslx) having employee information and create Employee objects using that data.

excel file POI
Employee class

public class Employee {
  private String firstName;
  private String lastName;
  private String department;
  private Date dob;
  public String getFirstName() {
    return firstName;
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  public String getLastName() {
    return lastName;
  public void setLastName(String lastName) {
    this.lastName = lastName;
  public String getDepartment() {
    return department;
  public void setDepartment(String department) {
    this.department = department;
  public Date getDob() {
    return dob;
  public void setDob(Date dob) {
    this.dob = dob;

Following class read data from the excel file and create objects.

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class ReadExcel {
  private static final String EXCEL_FILE_PATH="F:\\knpcode\\Java\\Java Programs\\Java IO\\Employee.xlsx";
  public static void main(String[] args) {
    ReadExcel readExcel = new ReadExcel();
  private void readExcelFile(String excelFilePath) {
    try {
      // get excel workbook
      Workbook workbook = WorkbookFactory.create(new FileInputStream(excelFilePath));
      // get excel sheet
      Sheet sheet = workbook.getSheetAt(0);
      //If you have more than one sheet in a workbook then you 
      // can iterate through sheets 
      /*Iterator<Sheet> sheetIterator = workbook.sheetIterator();
      while(sheetIterator.hasNext()) {
        Sheet sheet =;*/
      } catch (IOException | ParseException e) {
  private void readSheet(Sheet sheet) throws ParseException{
    Iterator<Row> rowItr = sheet.iterator();
    List<Employee> empList = new ArrayList<>();
    // Iterate through rows
    while(rowItr.hasNext()) {
      Employee emp = new Employee();
      Row row =;
      // skip header (First row)
      if(row.getRowNum() == 0) {
      Iterator<Cell> cellItr = row.cellIterator();
      // Iterate each cell in a row
      while(cellItr.hasNext()) {                
        Cell cell =;
        int index = cell.getColumnIndex();
        switch(index) {
          case 0:
          case 1:
          case 2:
          case 3:
    for(Employee emp : empList) {
      System.out.println("Employee information- " + emp.toString());
  // Utility method to get cell value based on cell type
  private Object getValueFromCell(Cell cell) {
    switch(cell.getCellType()) {
      case STRING:
          return cell.getStringCellValue();
      case BOOLEAN:
          return cell.getBooleanCellValue();
      case NUMERIC:
        if(DateUtil.isCellDateFormatted(cell)) {
          return cell.getDateCellValue();
        return cell.getNumericCellValue();
      case FORMULA:
        return cell.getCellFormula();
      case BLANK:
        return "";
        return "";                                


Employee information- John Emerson Technology Mon Nov 12 00:00:00 IST 1990
Employee information- Shelly Mcarthy HR Sat May 07 00:00:00 IST 1960
Employee information- Tom Cevor Finance Mon Feb 03 00:00:00 IST 1992

Excel file used in the code has only a single sheet which can be retrieved using- Sheet sheet = workbook.getSheetAt(0);
If there are more than one sheet in the workbook then you can iterate through the sheets too-

Iterator<Sheet> sheetIterator = workbook.sheetIterator();
while(sheetIterator.hasNext()) {
  Sheet sheet =;
  // get data from sheet

Related Posts

That’s all for the topic Read Excel File in Java Using Apache POI. If something is missing or you have something to share about the topic please write a comment.

You may also like

Share this post:

One Comment

  1. how to fetch cell value with font size and font style from excel in java

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.