Java读取Excel日期格式 Java获取单元格内容(支持日期及小数) getCellValue() 兼容xls2003及xlsx2007 jar包从3.17升级到5.2.2后 必须为枚举常量

   仅xls2003

//获取单元格内容(支持日期及小数)
private String getCellValue(HSSFCell cell) {
    if(cell == null){
        return null;
    }

    String cellValue = "";
    //like12 modified bug,20171124,不能保留小数
    //DecimalFormat df = new DecimalFormat("#");//不保留小数
    DecimalFormat df = new DecimalFormat("#.##");//最多保留x位小数
    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;
}

兼容xls2003及xlsx2007 

//获取单元格内容(支持日期及小数)(兼容xls2003及xlsx2007)
private String getCellValue(Cell cell) {
    if(cell == null){
        return null;
    }

    String cellValue = "";
    //like12 modified bug,20171124,不能保留小数
    //DecimalFormat df = new DecimalFormat("#");//不保留小数
    DecimalFormat df = new DecimalFormat("#.##");//最多保留x位小数
    switch (cell.getCellType()) {
        case Cell.CELL_TYPE_STRING:
            cellValue = cell.getRichStringCellValue().getString().trim();
            break;
        case  Cell.CELL_TYPE_NUMERIC:
            // like12 add,20180622,支持日期格式
            if (DateUtil.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  Cell.CELL_TYPE_BOOLEAN:
            cellValue = String.valueOf(cell.getBooleanCellValue()).trim();
            break;
        case  Cell.CELL_TYPE_FORMULA:
            cellValue = cell.getCellFormula();
            break;
        default:
            cellValue = "";
    }
    return cellValue;
}
//获取文件的后缀名,不能是exe/dll/com/bat
String fileName = file.getOriginalFilename(); // 获取文件名
String suffixName = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();// 获取文件的后缀名,并转化为小写
if(suffixName.equals(".exe")||suffixName.equals(".dll")||suffixName.equals(".com")||suffixName.equals(".bat")) {
    result.put("code", 999);
    result.put("msg", "文件格式错误");
    return result.toJSONString();
}

//兼容xls2003及xlsx2007
Workbook wb = null;
if(".xls".equals(suffixName)){
    wb = new HSSFWorkbook(file.getInputStream());
}else if(".xlsx".equals(suffixName)){
    wb = new XSSFWorkbook(file.getInputStream());
}else{
    result.put("code", 999);
    result.put("msg", "不支持的文件后缀");
    return result.toJSONString();
}

jar包从3.17升级到5.2.2后

解决jar升级到5.2.2后报”枚举 switch case 标签必须为枚举常量的非限定名称“的问题

//like12 modified,20220818,解决jar升级到5.2.2后报”枚举 switch case 标签必须为枚举常量的非限定名称“的问题
//获取单元格内容(支持日期及小数)(兼容xls2003及xlsx2007)
private String getCellValue(Cell cell) {
    if(cell == null){
        return null;
    }

    String cellValue = "";
    //like12 modified bug,20171124,不能保留小数
    //DecimalFormat df = new DecimalFormat("#");//不保留小数
    DecimalFormat df = new DecimalFormat("#.##");//最多保留x位小数
    switch (cell.getCellType()) {
        case STRING://Cell.CELL_TYPE_STRING
            cellValue = cell.getRichStringCellValue().getString().trim();
            break;
        case NUMERIC://CELL_TYPE_NUMERIC
            // like12 add,20180622,支持日期格式
            if (DateUtil.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 BOOLEAN://Cell.CELL_TYPE_BOOLEAN
            cellValue = String.valueOf(cell.getBooleanCellValue()).trim();
            break;
        case FORMULA://Cell.CELL_TYPE_FORMULA
            cellValue = cell.getCellFormula();
            break;
        default:
            cellValue = "";
    }
    return cellValue;
}

参考:

POI 操作 Excel 中 HSSFCell.CELL_TYPE_STRING 等无定义解决方法_竹林幽深的博客-CSDN博客_cell_type_stringcell.getCellType() is deprecated 与 HSSFCell.CELL_TYPE_NUMERIC is deprecated的替代方法_神蜗牛的博客-CSDN博客(后面这个有点误差5.2.2版本找不到cell.getCellTypeEnum()而依然是cell.getCellType()) 

  • 0
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值