2015-09-13 11 views
1

我能夠從Excel表格中獲取值,我的表中有20列,其中第7和第8列是日期類型單元格,如2015年3月7日,I我能夠正確獲取字符串/文本值,但無法獲取日期格式單元格,並獲得一個錯誤,我試着改變類型來獲取單元格的值,但沒有發生,任何人都可以幫助我如何獲得文本/字符串或日期格式細胞在我while循環,」從Java中的Excel中獲取值的問題

我的代碼如下,

while (iterator.hasNext()) { 
      Row nextRow = iterator.next(); 
      Map<Field, String> values = new EnumMap(Field.class); 
      for (Field f : Field.values()) { 
       Cell cell = nextRow.getCell(f.ordinal()); 
       if (cell != null) { 
        values.put(f, cell.getStringCellValue()); 
       } 
      } 

日期格式爲7-MAR-2015

回答

2

嘗試檢查單元格的格式,然後提取與正確funtion值,

參考,

if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC){ 
     if (DateUtil.isCellDateFormatted(cell)) { 
      System.out.println(cell.getDateCellValue()); 
     } else { 
      System.out.println(cell.getNumericCellValue()); 
     } 
}