Read Excel using apache poi in java

Read Excel using apache poi in java:

package com.yamicode.excel;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class ReadExcel {

    public List<List<Object>> readExcel(File excel) {
        List<List<Object>> excelData = new LinkedList<>();
        if (excel != null && excel.exists()) {
            Workbook workbook = null;
            try {
                workbook = WorkbookFactory.create(excel);
                Sheet dataOfSheet = workbook.getSheetAt(0);
                Iterator<Row> iterator = dataOfSheet.iterator();
                while (iterator.hasNext()) {
                    Row currentRow = iterator.next();
                    if (currentRow != null && currentRow.getLastCellNum() != 0) {
                        List<Object> cellValues = new LinkedList<>();
                        for (int i = 0; i < currentRow.getLastCellNum(); i++) {
                            cellValues.add(readCell(currentRow, workbook, i));
                        }
                        excelData.add(cellValues);
                    }

                }
            } catch (IOException | EncryptedDocumentException e) {
                System.out.println(e);
            }
            if (workbook != null) {
                try {
                    workbook.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return excelData;
    }

    public Object readCell(Row currentRow, Workbook workbook, int index){
        //the type of the formule value will be converted to the value using evaluator
        FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
        Cell currentCell = currentRow.getCell(index);
        CellValue cellValue = evaluator.evaluate(currentCell);
        if (currentCell != null && cellValue != null) {
            if (cellValue.getCellType() == CellType.STRING) {
                String value = cellValue.getStringValue();
                return value;
            } else if (cellValue.getCellType() == CellType.NUMERIC) {
                if (HSSFDateUtil.isCellDateFormatted(currentCell)) {
                    Date value = currentCell.getDateCellValue();
                    return value;
                } else {
                    Double value  = ((Double) cellValue.getNumberValue());
                    return value;
                }
            } else if (cellValue.getCellType() == CellType.FORMULA) {
                //this is not going to be executed in order to get the formule use the first function
                String value = cellValue.getStringValue();
                return value;
            } else if (cellValue.getCellType() == CellType.BOOLEAN) {
                Boolean value = ((Boolean) cellValue.getBooleanValue());
                return value;
            }
        }
        return null;
    }
}

Maven dependencies:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.0.0</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.0.0</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>4.0.0</version>
</dependency>