The problem of poi/jxl operating excel to obtain the real number of rows

When using the jxl framework to operate excel, because it is necessary to verify the content of excel, but when traversing the data of excel according to the number of rows, it is found that the number of rows obtained is not the real number of data rows. Later, according to relevant information, it is found that when using getRows When the () method gets the number of rows, it will get the number of rows with formatting and the number of rows that have been entered but cleared , so it is necessary to traverse each row and column to see if the cell content is empty.

/**
     * 循环判断--获取excel的真实行数
     */
    private static int realRows(Sheet sheet) {
        int rows = sheet.getRows();
        int columns=sheet.getColumns();
        List<Integer> realRows=new ArrayList<Integer>();
        for (int i = 0; i < rows; i++) {
            boolean isBlankRow=false;
            for (int j = 0; j < columns; j++) {
                if (!StringUtils.isEmpty(sheet.getCell(j, i).getContents())) {
                    isBlankRow=false;
                    break;
                } else {
                    isBlankRow=true;
                }
            }
            if (!isBlankRow) {
				//如果行的每一列不为空,把循环到的行添加到集合
                realRows.add(i);
            }
        }
		//集合的size就是真实数据的行数,去掉表头,就是需要的真实行数
        int row = realRows.size()-1;
        return row;
    }

 

Guess you like

Origin blog.csdn.net/qq_45001053/article/details/126072622