I had the same issue, i've was aware that the implementation via the standard (Apache POI) why cost so much time so after searching and looking around, i have found a better why (JXLS-Reader)
first of all use/import/include the library jxls-reader
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-reader</artifactId>
<version>2.0.3</version>
</dependency>
then create an XML file used by the library for the correspondence between the columns and the your object attributes, this XML take as parameter an initialized list to fill it by extracted data (Employee objects) from the Excel file, in your example, it will look like :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<workbook>
<worksheet idx="0">
<section startRow="0" endRow="0" />
<loop startRow="1" endRow="1" items="employeeList" var="employee" varType="com.department.Employee">
<section startRow="1" endRow="1">
<mapping row="1" col="0">employee.empNo</mapping>
<mapping row="1" col="1">employee.empName</mapping>
</section>
<loopbreakcondition>
<rowcheck offset="0">
<cellcheck offset="0"></cellcheck>
</rowcheck>
</loopbreakcondition>
</loop>
</worksheet>
</workbook>
Then in Java, initialize the list of the Employees (where the result of parsing will be included), then call the JXLS reader by the input Excel file and the XML mapping, it will look like:
package com.department;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.jxls.reader.ReaderBuilder;
import org.jxls.reader.ReaderConfig;
import org.jxls.reader.XLSReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;
public class ExcelProcessor {
private static Logger logger = LoggerFactory.getLogger(ExcelProcessor.class);
public void parseExcelFile(File excelFile) throws Exception{
final List<Employee> employeeList = new ArrayList<Employee>();
InputStream xmlMapping = new BufferedInputStream(ExcelProcessor.class.getClassLoader().getResourceAsStream("proBroMapping.xml"));
ReaderConfig.getInstance().setUseDefaultValuesForPrimitiveTypes(true);
ReaderConfig.getInstance().setSkipErrors(true);
InputStream inputXLS;
try{
XLSReader mainReader = ReaderBuilder.buildFromXML(xmlMapping);
inputXLS = new BufferedInputStream(new FileInputStream(excelFile));
final Map<String, Object> beans = new HashMap<String, Object>();
beans.put("employeeList", employeeList);
mainReader.read(inputXLS, beans);
System.out.println("Employee data are extracted successfully from the Excel file, number of Employees is: "+employeeList.size());
} catch(java.lang.OutOfMemoryError ex){
// Case of a very large file that exceed the capacity of the physical memory
ex.printStackTrace();
throw new Exception(ex.getMessage());
} catch (IOException ex) {
logger.error(ex.getMessage());
throw new Exception(ex.getMessage());
} catch (SAXException ex) {
logger.error(ex.getMessage());
throw new Exception(ex.getMessage());
} catch (InvalidFormatException ex) {
logger.error(ex.getMessage());
throw new Exception(ex.getMessage());
} finally {
IOUtils.closeQuietly(inputStream);
}
}
}
Hope this helps anyone having such a problem !