Cannot get a STRING value from a NUMERIC cell

使用POI读取单元格内容的时候,在读取到电话号码的时候报错Cannot get a STRING value from a NUMERIC cell

Cannot get a STRING value from a NUMERIC cell_第1张图片

读取的表格如下,手机号码
Cannot get a STRING value from a NUMERIC cell_第2张图片

解决办法:在遍历获取单元格后,遍历前加入

cell.setCellType(CellType.STRING);

另外单元格格式CellType还有如下类型:

CellType _NONE 未知类型,用于表示初始化之前的状态或缺少具体类型。仅限内部使用。
CellType NUMERIC 数字单元格类型(整数,小数,日期)
CellType STRING 字符串(文本)单元格类型
CellType BOOLEAN 布尔单元格类型
CellType FORMULA 公式单元格类型
CellType BLANK 空白单元格类型
CellType ERROR 错误单元格类型

参考文档:http://poi.apache.org/apidocs/dev/org/apache/poi/ss/usermodel/CellType.html

读取表单数据的代码如下

//1.创建一个workbook对象
HSSFWorkbook workbook = null;
workbook = new HSSFWorkbook(file.getInputStream());
//2.获取workbook中表单的数量
int numberOfSheets = workbook.getNumberOfSheets();
for (int i = 0; i < numberOfSheets; i++) {
    //3.获取表单
    HSSFSheet sheet = workbook.getSheetAt(0);
    //4.获取表单有多少行
    int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
    for (int j = 0; j < physicalNumberOfRows; j++) {
        if (j == 0) {
            continue;//5.标题行不解析
        }
        //6.获取行
        HSSFRow row = sheet.getRow(j);
        if (row == null) {
            continue;//防止中间有空行
        }
        //7.获取列数
        int physicalNumberOfCells = row.getPhysicalNumberOfCells();
        teacher = new Teacher();
        if (physicalNumberOfCells == 7) {//这里防止遍历空的列,我单元格每行有7列
            for (int k = 0; k < physicalNumberOfCells; k++) {
                HSSFCell cell = row.getCell(k);
      
                        cell.setCellType(CellType.STRING);
                        String cellValue = cell.getStringCellValue();
                       
                        switch (k) {
                            //根据列数决定如何处理
                            case 0:
                                teacher.setName(cellValue);//姓名
                                break;
                            case 1:
                                teacher.setWorkID(cellValue);
                                break;
                            case 2:
                                teacher.setGender(cellValue);
                                break;
                            case 3:
                                teacher.setEmail(cellValue);
                                break;
                            case 4:
                                teacher.setPhone(cellValue);
                                break;
                            case 5:
                                int departmentIndex = allDepartments.indexOf(new Department(cellValue));
                                teacher.setDepartmentId(allDepartments.get(departmentIndex).getId());
                                break;
                            case 6:
                                int joblevelIndex = allJobLevels.indexOf(new JobLevel(cellValue));
                                teacher.setJobLevelId(allJobLevels.get(joblevelIndex).getId());
                                break;
            }
        }else{
            break;
        }
        list.add(teacher);
    }
}

你可能感兴趣的:(错误记录)