Java開発–Excelタイプによってセル(Cell)の値を取得する
Javaコード:
private String getCellValueFunc(Cell cell) {
String strCellVal = “";
if (cell != null) {
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC: {
short format = cell.getCellStyle().getDataFormat();
if(format == 14 || format == 31 || format == 57 || format == 58){ SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd");
double value = cell.getNumericCellValue();
Date date = DateUtil.getJavaDate(value);
strCellVal = sdf.format(date);
}
else if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
DateFormat formater = new SimpleDateFormat(“yyyy-MM-dd");
strCellVal= formater.format(date);
} else {
strCellVal = NumberToTextConverter.toText(cell.getNumericCellValue());
}
break;
}
case HSSFCell.CELL_TYPE_STRING:
strCellVal = cell.getStringCellValue().replaceAll(“'", “"");
break;
case HSSFCell.CELL_TYPE_BLANK:
strCellVal = null;
break;
default:{
strCellVal = " “;
}
}
} else {
strCellVal = “";
}
return strCellVal;
}