poi导入excel,循环读取数据时,读到第10行显示单元格的值是空字符串,实际是有值的

poi导入excel,循环读取数据时,读到第10行,就显示单元格的值是空字符串,实际是有值的。只能读到前几行,包括10行以后的数据都读不到。

代码:

img

问题相关代码
for (int i = 3; i < lastRowNum; i++) {
            Row row = sheet.getRow(i);       // 当i=9 ,也就是excel里第10行,yearMonth=“”
            //申报年月
            String yearMonth = row.getCell(1).getStringCellValue();      // yearMonth取不到值
            String retyearMonth = null;
            if (null == yearMonth || "".equals(yearMonth)) {    
                error.append("\n第" + (i + 1) + "行,申报年月为空");
                return error.toString();
            } else {
                retyearMonth = yearMonth.substring(0, 4) + "-" + yearMonth.substring(4, 6);
            }
}

前面的行都可以读到

img

表格数据

img

怀疑是模板的问题,后来换了模板还是第10行出问题。

1:看能不能获取到第11行的数据
2:看下第10行,其他单元格的数据能不能获取到
可能是第10行的第2列数据格式不是字符串

你把第10行的数据放到第一行,看看会不会出问题

直觉应该是第10行出错那个单元格的格式跟其他行不一样


public String ImportData(MultipartFile file) throws Exception {

        log.info("《《《DispexpenditureService_ImportData_start》》》");

        List<CompanyOrgCorresponding> companyOrgCorrespondings = dispexpenditureMapper.companyAndorg();
        List<DisposableExpenditure> dispExpList = CollUtil.newArrayList();
        StringBuffer error = new StringBuffer();
        ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
        Sheet sheet = reader.getSheet();
        //int lastRowNum = getRealRowNum(sheet);
        int lastRowNum = sheet.getLastRowNum();

        for (int i = 3; i < lastRowNum; i++) {
            Row row = sheet.getRow(i);
            if (null == row) {
                break;
            } else {
                short lastCellNum = sheet.getRow(i).getLastCellNum();
                for (int j = 0; j < lastCellNum; j++) {
                    if (null == row.getCell(i) || "".equals(row.getCell(i))) {
                        sheet.getRow(i).createCell(j).setCellType(CellType.STRING);
                    } else {
                        sheet.getRow(i).getCell(j).setCellType(CellType.STRING);
                    }
                }
            }
        }

        log.info("《《《=== DispexpenditureService_sheet.getRow(i).getCell(j).setCellType(CellType.STRING)_end ===》》》");

        for (int i = 3; i < lastRowNum; i++) {
            Row row = sheet.getRow(i);

            //申报年月
            String yearMonth = row.getCell(1).getStringCellValue();
            String retyearMonth = null;
            if (null == yearMonth || "".equals(yearMonth)) {
                error.append("\n第" + (i + 1) + "行,申报年月为空");
                return error.toString();
            } else {
                retyearMonth = yearMonth.substring(0, 4) + "-" + yearMonth.substring(4, 6);
            }

            //二级单位
            String orgName = row.getCell(2).getStringCellValue();
            StringBuffer orgId = new StringBuffer();
            if (null == orgName || "".equals(orgName)) {
                error.append("\n第" + (i + 1) + "行,二级单位为空");
                return error.toString();
            }
            companyOrgCorrespondings.forEach(c -> {
                if (c.getCompanyName().equals(orgName)) {
                    orgId.append(c.getOrgId());
                }
            });

            //离退休类别
            String type = row.getCell(3).getStringCellValue();
            if (null == type || "".equals(type)) {
                error.append("\n第" + (i + 1) + "行,离退休类别为空");
                return error.toString();
            }
            switch (type) {
                case "离休":
                    type = "离休";
                    break;
                case "退休":
                    type = "退休";
                    break;
                case "退职":
                    type = "退职";
                    break;
                case "死亡":
                    type = "死亡";
                    break;
                case "在职":
                    type = "在职";
                    break;
                default:
                    type = "";
                    error.append("\n第" + (i + 1) + "行,离退休类别不正确");
                    break;
            }

            //身份证号码
            String nationalIdentifier = row.getCell(4).getStringCellValue();
            if (null == nationalIdentifier || "".equals(nationalIdentifier)) {
                error.append("\n第" + (i + 1) + "行,身份证号码为空");
                return error.toString();
            }
//            log.info("DispexpenditureService_ImportData_start !!===[{}]", JSON.toJSON(nationalIdentifier));

            //个人编号
            String empNumber = row.getCell(5).getStringCellValue();
            if (null == empNumber || "".equals(empNumber)) {
                error.append("\n第" + (i + 1) + "行,个人编号为空");
                return error.toString();
            }

            //姓名
            String empName = row.getCell(6).getStringCellValue();
            if (null == empName || "".equals(empName)) {
                error.append("\n第" + (i + 1) + "行,姓名为空");
                return error.toString();
            }

            //死亡日期
            String deathDate = row.getCell(7).getStringCellValue();
            String retdeathDate = null;
            if (null == deathDate || "".equals(deathDate)) {
                error.append("\n第" + (i + 1) + "行,死亡日期为空");
                return error.toString();
            } else {
                retdeathDate = deathDate.substring(0, 4) + "-" + deathDate.substring(4, 6) + "-" + deathDate.substring(6, 8);
            }

            //一次性支付金额
            String costAmount = row.getCell(8).getStringCellValue();
            if (null == costAmount || "".equals(costAmount)) {
                error.append("\n第" + (i + 1) + "行,一次性支付金额为空");
                return error.toString();
            }
            //string转成double 保留两位
            double costAmountDou = Double.valueOf(costAmount);
            DecimalFormat df = new DecimalFormat("#.00"); //保留2位小数,填写#.00 ,以此类推
            String temp = df.format(costAmountDou);
            costAmountDou = Double.valueOf(temp);

            Long currentUser = WebUtil.getCurrentUser();
            DisposableExpenditure disposableExpenditure = DisposableExpenditure.builder().
                    nationalIdentifier(nationalIdentifier).
                    orgId(Integer.valueOf(nvl(orgId))).
                    type(type).
                    empNumber(empNumber).
                    empName(empName).
                    yearMonth(retyearMonth).
                    costAmount(costAmountDou).
                    deathDate(retdeathDate).
                    enable(1).
                    createBy(currentUser).
                    createTime(new DateTime()).
                    updateBy(currentUser).
                    updateTime(new DateTime()).build();
            dispExpList.add(disposableExpenditure);

            if (null == error || "".equals(error.toString())) {
                //保存  判断是否重复
                Integer count = dispexpenditureMapper.selectCount(disposableExpenditure);
                if (count != 0) {
                    String empName_s = disposableExpenditure.getEmpName();
                    error.append("\n姓名为" + empName_s + "的数据已经存在!\n");
                    return error.toString();
                } else {
                    dispexpenditureMapper.addDisposableExpenditure(disposableExpenditure);
                }
            }
        }
        return error.toString();
    }