1. 程式人生 > >Java讀取Excel日期格式

Java讀取Excel日期格式

//獲取單元格內容

private String getCellValue(HSSFCell cell) {

if(cell == null){

return null;

}

String cellValue = "";

DecimalFormat df = new DecimalFormat("#");

switch (cell.getCellType()) {

case HSSFCell.CELL_TYPE_STRING:

cellValue = cell.getRichStringCellValue().getString().trim();

break;

case HSSFCell.CELL_TYPE_NUMERIC:

//like12 add,20180622,支援日期格式

if(HSSFDateUtil.isCellDateFormatted(cell)){ 

Date d = cell.getDateCellValue();

DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");//HH:mm:ss

cellValue = df2.format(d);

}

//數字

else{

cellValue = df.format(cell.getNumericCellValue()).toString();

}

break;

case HSSFCell.CELL_TYPE_BOOLEAN:

cellValue = String.valueOf(cell.getBooleanCellValue()).trim();

break;

case HSSFCell.CELL_TYPE_FORMULA:

cellValue = cell.getCellFormula();

break;

default:

cellValue = "";

}

return cellValue;

    }