2012-08-31 8 views

Odpowiedz

5

Musisz przejrzeć wszystkie komórki w rzędzie i sprawdzić, czy wszystkie są puste. Nie znam żadnego innego rozwiązania ...

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; 
    } 
} 

Kod jest od here

2

Zakładając chcesz sprawdzić, czy rząd n jest pusta, pamiętając, że wiersze w Apache POI są zerowe nie opiera się na podstawie , że chcesz coś takiego:

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; 
     } 
    } 
} 
26

używam następujące metody w moim projekcie i to POI pracuje dobrze. Jest to odmiana rozwiązania firmy zeller.

public static boolean isRowEmpty(Row row) { 
    for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) { 
     Cell cell = row.getCell(c); 
     if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK) 
      return false; 
    } 
    return true; 
} 
+0

Użyłem tego samego fragmentu kodu, ale zwraca on wartość false, ponieważ wartość komórki jest pusta. Jeśli sprawdzę, czy (cell.getStringCellValue! = Null && cell.getNumericValue! = Null) działa ona dla wartości String, ale dla wartości BigDecimal zwraca IllegalStateException: Nie można przekonwertować tekstu na wartość liczbową. Czy możesz zasugerować mi, jak radzić sobie z isRowEmpty, aby zwrócić wartość true dla pustego wiersza? – Harleen

+0

A jeśli Typ komórki ma wzór? –

3

Tak, ale jeśli w jakimś rzędu będziemy musieli w jakiś komórce = „” i pustych wartości w kolejnych komórkach. Metoda ta będzie działać lepiej:

boolean isEmptyRow(Row row){ 
    boolean isEmptyRow = 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())){ 
      isEmptyRow = false; 
      }  
     } 
    return isEmptyRow; 
    } 
12

wiersz iteracyjnej zwraca tylko wierszy zawierających dane, jednak jeśli są one całkowicie pusty, iteracji przez indeks wiersza, getRow(index) powraca null

Rozwiązanie:

Do wersji POI 3.14 (dzięki 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; 
} 

W wersji POI 3,15 do 4,2 (int getCellType() jest przestarzała)

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; 
} 

od wersji POI 4 (CellTypeEnum getCellType() powróci wyliczenia nie int)

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; 
} 
0

spróbuj if (iterator.hasNext)

Row nextRow = null; 
Cell nextCell = null; 
Iterator<Row> iterator = firstSheet.rowIterator(); 
if(iterator.hasNext) { 
    return true; 
} 
else { 
    return false; 
}