POI读取单元格内容、空行

    /**
     * 获取EXCEL单元格的值,一律转为String返回
     * @param cell
     * @return
     */
    private String getCellStrValue(Cell cell) {
           String value = "";
           if (cell != null) {
               switch (cell.getCellType()) {
               case Cell.CELL_TYPE_NUMERIC:
                    if(DateUtil.isCellDateFormatted(cell)){  // 处理日期格式、时间格式
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
                        Date date = cell.getDateCellValue();  
                        value = sdf.format(date);
                    }else{
                        DataFormatter data = new DataFormatter();
                        value = data.formatCellValue(cell);
                    }
                   break;
               case Cell.CELL_TYPE_STRING:
                   value = cell.getStringCellValue().trim();
                   break;
               case Cell.CELL_TYPE_FORMULA:
                   break;
               case Cell.CELL_TYPE_BOOLEAN:
                   value += cell.getBooleanCellValue() + "";
                   break;
               default:
                   break;
               }
           }
           return value;
       }
    /**
     * 功能:判断是否是空行
     * */
    private boolean isBlankRow(org.apache.poi.ss.usermodel.Row columnRow,
                               int excelLastcell) {
        String value = "";
        for (int i = 0; i < excelLastcell; i++) {
            Cell cell = columnRow.getCell(i);
            if (cell != null) {
                switch (cell.getCellType()) {
                case Cell.CELL_TYPE_NUMERIC:
                    value += cell.getNumericCellValue() + "";
                    break;
                case Cell.CELL_TYPE_STRING:
                    value += cell.getStringCellValue();
                    break;
                case Cell.CELL_TYPE_FORMULA:
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    value += cell.getBooleanCellValue() + "";
                    break;
                default:
                    break;
                }
            }
        }
        if (value == null || "".equals(value)) {
            return true;
        } else {
            return false;
        }
    }

            Row dataRow = null;

           org.apache.poi.ss.usermodel.Row excelheadRow = sheet.getRow(0);

            int excelLastcell = excelheadRow.getLastCellNum(); //纵列数

                //执行操作

                for(int n=1;n<=lastNum;n++){

                    dataRow = sheet.getRow(n);

                    if (isBlankRow(dataRow, excelLastcell)) { //空行则跳过

                        continue;

                    }

                     .....

              }

--摘自收费系统的导入迎新数据,在系统管理-学生管理中

猜你喜欢

转载自563432906.iteye.com/blog/2395577