获取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 @   码奴生来只知道前进~  阅读(60)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
点击右上角即可分享
微信分享提示