获取sheet表单里的有效行数

/**
     * 获取sheet表单里的有效行数
     * @param sheet
     * @return
     */
    public int getRealRowNum(Sheet sheet) {
        int rowNum = sheet.getLastRowNum()-1;
        while(rowNum > 0 ){
            Row row = sheet.getRow(rowNum+1);
            if (row != null) {
                for (Cell cell : row) {
                    if (!StringUtils.isEmpty(cell.getStringCellValue()));
                    return rowNum;
                }
            }
            rowNum--;
        }
        return rowNum;
    }
 /**
     * 用来得到真实行数
     * @param sheet 需要读取的Excel表格(excel文件的工作簿的名称)
     * @return
     *
     */
    public static int readExcelValueRows(Sheet sheet) {
        int realRow = 0;// 返回的真实行数
        // 标题行有几行就从几开始
        for (int i = 2; i <= sheet.getLastRowNum(); i++) {
            //i从1开始,不判断第一行标题行
            Row row = sheet.getRow(i);
            if (row == null){
                continue;
            }
            for (Cell cell : row) {
                if (cell == null){
                    continue;
                }
                String value = cell.getStringCellValue().trim();
                if (value == null || "".equals(value)){
                    continue;
                } else{
                    realRow++;
                    break;
                }
            }
        }
        return realRow;
    }

posted @ 2022-08-04 19:12  码奴生来只知道前进~  阅读(76)  评论(0)    收藏  举报