无法从STRING单元格错误中获取NUMERIC值

时间:2018-02-02 05:56:23

标签: java xml-parsing apache-poi xssf

我在这方面尝试了很多SO答案,但我仍无法解决。 我的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;
    }
}

我的模型如下:

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
}

此问题是因为extCode的标头是字符串,该列中的值是整数。即使我跳过标题并读取其余部分,也会出现错误。当我将extCode的数据类型更改为String时,错误被反转并且得到&#34;无法从NUMERIC单元获取STRING单元格#34;。请有人帮我摆脱这个错误

1 个答案:

答案 0 :(得分:1)

从细胞中获取价值时进行检查。它应该像下面这样。

case 2:
    if(cell. getCellType() == CellType.NUMERIC){
        employee.setExtCode((int) cell.getNumericCellValue());
    }else{
    employee.setExtCode(Integer.valueOf(cell.getStringCellValue()));
    }
    break;

有几种支持的单元格类型POI API。有关详细信息,请访问link

编辑:

如果上述解决方案不起作用,您只需执行

即可
cell.setCellType(Cell.CELL_TYPE_STRING); 
employee.setExtCode(Integer.valueOf(cell.getStringCellValue()));

在从单元格中读取值之前。但是,POI documentation明确表示要使用 setCellType 来读取单元格中的值,因为您可能会丢失单元格的原始格式。您可以使用DataFormatter而不是使用它。代码应如下所示(我没有遵守并运行)。

String strValue = new DataFormatter().formatCellValue(cell);