38

Apache POI を使用して .xls ドキュメントの空の行を特定するにはどうすればよいですか?

4

10 に答える 10

32

行イテレータはデータを含む行のみを返しますが、行が完全に空の場合は、行インデックスで反復してgetRow(index) 戻りますnull

解決:

POI バージョン 3.14 まで (Sergii Lisnychyi に感謝):

private boolean checkIfRowIsEmpty(Row row) {
    if (row == null) {
        return true;
    }
    if (row.getLastCellNum() <= 0) {
        return true;
    }
    for (int cellNum = row.getFirstCellNum(); cellNum < row.getLastCellNum(); cellNum++) {
        Cell cell = row.getCell(cellNum);
        if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK && StringUtils.isNotBlank(cell.toString())) {
            return false;
        }
    }
    return true;
}

POI バージョン 3.15 から 4.2 まで (int getCellType()は非推奨):

    private boolean checkIfRowIsEmpty(Row row) {
    if (row == null) {
        return true;
    }
    if (row.getLastCellNum() <= 0) {
        return true;
    }
    for (int cellNum = row.getFirstCellNum(); cellNum < row.getLastCellNum(); cellNum++) {
        Cell cell = row.getCell(cellNum);
        if (cell != null && cell.getCellTypeEnum() != CellType.BLANK && StringUtils.isNotBlank(cell.toString())) {
            return false;
        }
    }
    return true;
}

POI バージョン 4 から ( CellTypeEnum getCellTypeEnum()int ではなく Enum を返します):

private boolean checkIfRowIsEmpty(Row row) {
    if (row == null) {
        return true;
    }
    if (row.getLastCellNum() <= 0) {
        return true;
    }
    for (int cellNum = row.getFirstCellNum(); cellNum < row.getLastCellNum(); cellNum++) {
        Cell cell = row.getCell(cellNum);
        if (cell != null && cell.getCellTypeEnum() != CellType.BLANK && StringUtils.isNotBlank(cell.toString())) {
            return false;
        }
    }
    return true;
}

POI バージョン 5.1 から (CellTypeEnum getCellTypeEnum() は getCellType() に名前が変更されました):

private boolean checkIfRowIsEmpty(Row row) {
    if (row == null) {
        return true;
    }
    if (row.getLastCellNum() <= 0) {
        return true;
    }
    for (int cellNum = row.getFirstCellNum(); cellNum < row.getLastCellNum(); cellNum++) {
        Cell cell = row.getCell(cellNum);
        if (cell != null && cell.getCellType() != CellType.BLANK && StringUtils.isNotBlank(cell.toString())) {
            return false;
        }
    }
    return true;
}
于 2015-02-11T10:19:52.367 に答える
5

行内のすべてのセルを繰り返し処理し、それらがすべて空かどうかを確認する必要があります。他の解決策を知りません...

 short c;
 for (c = lastRow.getFirstCellNum(); c <= lastRow.getLastCellNum(); c++) {
     cell = lastRow.getCell(c);
     if (cell != null && lastRow.getCell(c).getCellType() != HSSFCell.CELL_TYPE_BLANK) {
          nonBlankRowFound = true;
     }
 }

コードはこちらから

于 2012-08-31T14:12:16.623 に答える
3

行が空であるかどうかを確認したい場合n、Apache POIの行は1ベースではなくゼロベースであることを思い出して、次のようにします。

 Row r = sheet.getRow(n-1); // 2nd row = row 1
 boolean hasData = true;

 if (r == null) {
    // Row has never been used
    hasData = false;
 } else {
    // Check to see if all cells in the row are blank (empty)
    hasData = false;
    for (Cell c : r) {
       if (c.getCellType() != Cell.CELL_TYPE_BLANK) {
         hasData = true;
         break;
       }
    }
 }
于 2012-08-31T15:01:51.957 に答える
3

apache-poi [4+] を使用している場合:

次に、以下の方法が機能します。提案された他の方法が私にはうまくいかなかったので、私はこの方法でそれをしなければなりませんでした.

public static boolean isRowEmpty(Row row) {
    boolean isEmpty = true;
    DataFormatter dataFormatter = new DataFormatter();
    if(row != null) {
        for(Cell cell: row) {
            if(dataFormatter.formatCellValue(cell).trim().length() > 0) {
                isEmpty = false;
                break;
            }
        }
    }
    return isEmpty;
}

このメソッドは、セルがまたはの場合にdataFormatter.formatCellValue(cell)を返します。""empty / ZERO length stringnullBLANK

参照用のインポート ステートメント:

import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;

お役に立てれば!

于 2019-01-19T06:12:06.217 に答える
0

CellReference のインスタンスを取得し、そのインスタンスで formatAsString() を使用します。空の文字列と比較します

`

if("".equals(cellRef.formatAsString())){
     System.out.println("this is an empty cell");
   }else{
      System.out.println("Cell value : "+cellRef.formatAsString());
   }

` 参照 : http://www.javabeat.net/2007/10/apache-poi-reading-excel-sheet-using-java/

于 2012-08-31T14:24:37.193 に答える