Apache POI を使用して .xls ドキュメントの空の行を特定するにはどうすればよいですか?
10 に答える
行イテレータはデータを含む行のみを返しますが、行が完全に空の場合は、行インデックスで反復して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;
}
行内のすべてのセルを繰り返し処理し、それらがすべて空かどうかを確認する必要があります。他の解決策を知りません...
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;
}
}
コードはこちらから
行が空であるかどうかを確認したい場合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;
}
}
}
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 string
null
BLANK
参照用のインポート ステートメント:
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;
お役に立てれば!
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/