2016-02-03 176 views
2

我從Excel中獲取數據,我需要對某些單元格包含空值的行進行計數。代碼沒有任何返回。對於這個單元格中包含一些值的行,它正常工作。你能幫我如何處理空值的單元格嗎?當單元格包含空值時從Excel中獲取數據

的代碼如下:

HSSFSheet sheet = workbook.getSheetAt(1); 

     // Get iterator to all the rows in current sheet 
     Iterator<Row> rowIterator = sheet.iterator(); 

     // Iterate through rows 
     while (rowIterator.hasNext()) { 
      Row row = rowIterator.next(); 

      // Index of column D is 3 (A->0, B->1, etc) 
      Cell cellD = row.getCell(3); 
      Cell cellG = row.getCell(6); 

      // 
      String key = null; 
      Integer value = null; 

      // finding cell type 
      int cellTypeG = cellG.getCellType(); 

      // getting value from first cell 
      key = cellD.getStringCellValue(); 


      // fetching value to the Double, to be able to compare data 
      if (Cell.CELL_TYPE_NUMERIC == cellTypeG) { 
       value = new Double(cellG.getNumericCellValue()).intValue(); 
       if (value != null) { 
        if (map.containsKey(key)) { 
         map.put(key, map.get(key) + 1); 
        } else { 
         map.put(key, 1); 
        } 

       } 
      } 
     } 

     // cycle which is filling the map 
     for (Entry<String, Integer> e : map.entrySet()) { 
      System.out.println(e.getKey() + ": " + e.getValue()); 
     } 
+0

我知道現在閱讀這些文檔是非常流行的,但是會發生什麼[如果你給它一個嘗試?](http://poi.apache.org/spreadsheet/quick-guide.html#Iterator)只有[非常清楚和易於遵循遍歷行和單元格的文檔,包括空白/空處理](http://poi.apache.org/spreadsheet/quick-guide.html#Iterator)..... – Gagravarr

回答

0

你能幫我如何處理與空值的細胞?

只需使用StringUtils.isEmpty()檢查單元格是否爲空/黑色。

例如,StringUtils.isEmpty(cellD.getStringCellValue())。如果單元格爲空或空,這將返回true

相關問題