使用poi读取Excel中的各种数据类型处理

来源:互联网 发布:php system返回1 编辑:程序博客网 时间:2024/05/17 09:38

Excel中可能会有各种数据格式,但poi只提供了那几种,这就要我们好好处理我们想要的数据了

以下是我的总结:

if (null != cell)
{
// 以下是判断数据的类型
switch (cell.getCellType())
{
case HSSFCell.CELL_TYPE_NUMERIC: // 数字

if (0 == cell.getCellType()) {//判断单元格的类型是否则NUMERIC类型
if (HSSFDateUtil.isCellDateFormatted(cell)) {// 判断是否为日期类型
Date date = cell.getDateCellValue();
DateFormat formater = new SimpleDateFormat(
"yyyy-MM-dd HH:mm");
cellValue = formater.format(date);
}else{
cellValue = cell.getNumericCellValue() + "";
}
}
break;


case HSSFCell.CELL_TYPE_STRING: // 字符串
cellValue = cell.getStringCellValue();
break;


case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
cellValue = cell.getBooleanCellValue() + "";
break;


case HSSFCell.CELL_TYPE_FORMULA: // 公式
cellValue = cell.getCellFormula() + "";
break;


case HSSFCell.CELL_TYPE_BLANK: // 空值
cellValue = "";
break;


case HSSFCell.CELL_TYPE_ERROR: // 故障
cellValue = "非法字符";
break;


default:
cellValue = "未知类型";
break;

原创粉丝点击