Reputation:
I have tried many SO answers on this regard but still I could not resolve. My java file is as follows:
public class ReadExcelFileAndStore {
public List getTheFileAsObject(String filePath){
List <Employee> employeeList = new ArrayList<>();
try {
FileInputStream file = new FileInputStream(new File(filePath));
// Get the workbook instance for XLS file
XSSFWorkbook workbook = new XSSFWorkbook(file);
int numberOfSheets = workbook.getNumberOfSheets();
//System.out.println(numberOfSheets);
//loop through each of the sheets
for(int i = 0; i < numberOfSheets; i++) {
String sheetName = workbook.getSheetName(i);
System.out.println(sheetName);
// Get first sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(i);
// Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
// Get Each Row
Row row = rowIterator.next();
//Leaving the first row alone as it is header
if (row.getRowNum() == 0) {
continue;
}
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
Employee employee = new Employee();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
int columnIndex = cell.getColumnIndex();
switch (columnIndex + 1) {
case 1:
employee.setEmpName(cell.getStringCellValue());
break;
case 2:
employee.setExtCode((int) cell.getNumericCellValue());
break;
case 3:
employee.setDepartment(cell.getStringCellValue());
break;
}
}
employeeList.add(employee);
}
}
file.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return employeeList;
}
}
I have the model as follows:
package com.restfapi.demo.Model;
public class Employee {
private String empName;
private int extCode;
private String department;
public Employee(){
}
public Employee(String empName,int extCode, String department){
this.empName = empName;
this.extCode = extCode;
this.department = department;
}
//all getters and setters are followed
}
This problem is because the header of extCode is string and the values in that column are integers. Even if I skip the header and read the rest, the error appears.When I changed the datatype of extCode to String the error is reversed and getting "Cannot get a STRING cell from a NUMERIC cell". Please somebody help me to get rid of this error
Upvotes: 0
Views: 829
Reputation: 7315
Put a check when you are getting value from the cell. It should be something like below.
case 2:
if(cell. getCellType() == CellType.NUMERIC){
employee.setExtCode((int) cell.getNumericCellValue());
}else{
employee.setExtCode(Integer.valueOf(cell.getStringCellValue()));
}
break;
There are several supported cell type POI API. For more info visit the link
Edited:
If the above solution not work you can simply do
cell.setCellType(Cell.CELL_TYPE_STRING);
employee.setExtCode(Integer.valueOf(cell.getStringCellValue()));
Before reading the value from the cell. However, POI documentation clearly says to use setCellType to read the value from a cell as you might be lost original formatting of the cell. Instead of using that you can use DataFormatter. The code should be like below (I did not complied and run).
String strValue = new DataFormatter().formatCellValue(cell);
Upvotes: 1