Excel导入数字有几种
1、时间
判断用工具类org.apache.poi.hssf.usermodel.HSSFDateUtil
HSSFDateUtil.isCellDateFormatted
2、数字
数字区分正数与小数
也就是数字格式是否保留小数
对于这种问题,我写了个公用的读取Cell的方法
/**
* 将单元格内容转换为字符串
* @param cell
* @param format 日期或者数字格式;
* 日期:yyyy-MM-dd(年月日)、yyyy-MM-dd hh:mm:ss(年月日时分秒);
* 数字:#(不保留小数)、#.0(保留一位小数)、#.00(保留两位小数)
* @return
*/
public static String convertCellValueToString(Cell cell, String format) {
if(cell==null){
return null;
}
String returnValue = null;
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC: //数字
if(HSSFDateUtil.isCellDateFormatted(cell)){
Date date = cell.getDateCellValue();
returnValue = DateUtils.parseDateToStr(format, date);
}else{
Double doubleValue = cell.getNumericCellValue();
DecimalFormat df = new DecimalFormat(format);
returnValue = df.format(doubleValue);
}
break;
case Cell.CELL_TYPE_STRING: //字符串
returnValue = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_BOOLEAN: //布尔
Boolean booleanValue = cell.getBooleanCellValue();
returnValue = booleanValue.toString();
break;
case Cell.CELL_TYPE_BLANK: // 空值
break;
case Cell.CELL_TYPE_FORMULA: // 公式
returnValue = cell.getCellFormula();
break;
case Cell.CELL_TYPE_ERROR: // 故障
break;
default:
break;
}
return returnValue;
}
Excel导入数字的坑
最新推荐文章于 2022-03-25 17:04:07 发布