2013-09-23 94 views
1

我有一個單元格1:20時間值(1:20:00)。我怎樣才能讀到apache poi 3.9? 我的代碼正在檢查兩種單元格類型,當它讀取時間值時,它將轉到CELL_TYPE_NUMERIC,並且1:20將轉換爲0。我怎麼能讀到1:20?謝謝!如何通過Apache POI 3.9從xls/xlsx讀取時間格式?

switch (cell.getCellType()) { 
    case HSSFCell.CELL_TYPE_NUMERIC: 
     cellValue = String.valueOf((int) (cell.getNumericCellValue())); 
     break; 
    case HSSFCell.CELL_TYPE_STRING: 
     cellValue = cell.getStringCellValue(); 
     break; 
} 

回答

1

我做到了!

switch (cell.getCellType()) { 
    case HSSFCell.CELL_TYPE_NUMERIC: 
     if(DateUtil.isCellDateFormatted(cell)) { 
      cellValue = new DataFormatter().formatCellValue(cell); 
     } else { 
      cellValue = String.valueOf((int) (cell.getNumericCellValue())); 
     } 
     break; 
    case HSSFCell.CELL_TYPE_STRING: 
     cellValue = cell.getStringCellValue(); 
     break; 
} 
+1

style.setDataFormat(HSSFDataFormat.getBuiltinFormat(「@」)).//如果數據是在不同的格式也。它將當作String.While顯示這將是任何問題。 – swamy