Java POI read excel example 2017-09-18 08:26

It's easy to use Apache POI read excel file. Let's say you have an excel file. The path of it is D:\test.xlsx. The content is like following.

name age
henry 28
justin 27
charles 30

Before using POI we need import the jars. The content of pom file is here.


The code of reading excel file.

public class POIClient {
    public static void main(String[] args) throws Exception {
        FileInputStream excelFile = new FileInputStream(new File("D:\\test.xlsx"));
        Workbook workbook = new XSSFWorkbook(excelFile);
        //if there are multiple sheet use workbook.getNumberOfSheets()
        Sheet currentSheet = workbook.getSheetAt(0);
        String sheetName = currentSheet.getSheetName();
        System.out.println("the sheet name:" + sheetName);
        for (Row currentRow : currentSheet) {
            Cell nameCell = currentRow.getCell(0);
            Cell ageCell = currentRow.getCell(1);
            System.out.println("name:" + nameCell.getStringCellValue() + ",age:" + ageCell.getNumericCellValue());

The output.

the sheet name:Sheet1