[英]Cannot get a NUMERIC value from a STRING cell error
I have tried many SO answers on this regard but still I could not resolve. 在这方面,我已经尝试了许多SO答案,但仍然无法解决。 My java file is as follows: 我的java文件如下:
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. 此问题是因为extCode的标头是字符串,并且该列中的值是整数。 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". 即使我跳过标题并阅读其余内容,也会出现错误。当我将extCode的数据类型更改为String时,错误将被反转,并显示“无法从NUMERIC单元格获取STRING单元格”。 Please somebody help me to get rid of this error 请有人帮我摆脱这个错误
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. 有几种受支持的单元类型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. 但是, POI文档明确指出要使用setCellType从单元格中读取值,因为您可能会丢失该单元格的原始格式。 Instead of using that you can use DataFormatter . 可以使用DataFormatter代替使用它。 The code should be like below (I did not complied and run). 代码应如下所示(我没有编译并运行)。
String strValue = new DataFormatter().formatCellValue(cell);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.