Apache POI - Read from Excel

Read from Excel with APACHE POI Library:

Add below library reference in pom.xml:
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17</version>
</dependency>

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


//Read from excel by row and each cell as column by passing file input path (.xls or .xlsx)
private void readFromExcel(String xslFilePath) throws Exception {
Workbook wb= null;
try{
    wb = WorkbookFactory.create(new File(xslFilePath));

//Get data sheet at index 0 (First Tab)
    Sheet sh = wb.getSheetAt(0);
    Iterator<Row> rowItr = sh.iterator();

        while(rowItr .hasNext()){
            Row row = rowItr.next();
            
            if(row.getCell(0) ! =null) {
                // Set data into custom object or collection arraylist
        DataFormatter format = new DataFormatter();
        String val = format.formatCellValue(row.getCell(0));
            }

        if(row.getCell(1) ! =null) {
                // Set data into custom object or collection arraylist
            }
            
        }

    wb.close();
} catch(Exception e) {
        throw e;
    }
finally{
    if(wb !=null) {
        wb.close();
    }
}

}

Comments

Popular posts from this blog

PUTTY - The server's host key is not cached in the registry cache

OIM-12c Installation - FMW - SOA - IDM

SAML & OAuth 2.0