jeecg导入Excel时去掉小数点,附单元格数据类型

1、POI读取Excel时判断单元格数据类型:

CELL_TYPE_NUMERIC 数值型 0
CELL_TYPE_STRING 字符串型 1
CELL_TYPE_FORMULA 公式型 2
CELL_TYPE_BLANK 空值 3
CELL_TYPE_BOOLEAN 布尔型 4
CELL_TYPE_ERROR 错误 5

2、在jeecg中获取Excel中的值:

源码是这样的:jeecgframework/poi/excel/imports/CellValueServer.java

private Object getCellValue(String xclass, Cell cell, ExcelImportEntity entity)
  {
    if (cell == null) {
      return "";
    }
    Object result = null;

    if (("class java.util.Date".equals(xclass)) || ("class java.sql.Time".equals(xclass))) {
      if (0 == cell.getCellType())
      {
        result = cell.getDateCellValue();
      } else {
        cell.setCellType(1);
        result = getDateData(entity, cell.getStringCellValue());
      }
      if ("class java.sql.Time".equals(xclass))
        result = new Time(((Date)result).getTime());
    }
    else if (0 == cell.getCellType()) {
      result = Double.valueof(cell.getNumericCellValue());
    } else if (4 == cell.getCellType()) {
      result = Boolean.valueOf(cell.getBooleanCellValue());
    } else {
      result = cell.getStringCellValue();
    }
    return result;
  }

3、在使用中发现,Excel中的整数在导入后,悄悄的加了小数点


为了去掉不该出现的小数点,修改源码中的格式转换部分,发现源码中直接使用的0和4数值去判断,不太了解什么意思,所以查了单元格数据类型附在文章的开始部分,0为数值型,在进行格式转化是,转为Double型了,所以悄悄的加上了小数点,这里为满足自己的业务需要,直接强转为int型,因为我的数值本身就是int型,修改后为

private Object getCellValue(String xclass, Cell cell, ExcelImportEntity entity)
  {
    if (cell == null) {
      return "";
    }
    Object result = null;

    if (("class java.util.Date".equals(xclass)) || ("class java.sql.Time".equals(xclass))) {
      if (0 == cell.getCellType())
      {
        result = cell.getDateCellValue();
      } else {
        cell.setCellType(1);
        result = getDateData(entity, cell.getStringCellValue());
      }
      if ("class java.sql.Time".equals(xclass))
        result = new Time(((Date)result).getTime());
    }
    else if (XSSFCell.CELL_TYPE_NUMERIC == cell.getCellType()) {
      result = (int)cell.getNumericCellValue();
    } else if (XSSFCell.CELL_TYPE_BOOLEAN == cell.getCellType()) {
      result = Boolean.valueOf(cell.getBooleanCellValue());
    } else {
      result = cell.getStringCellValue();
    }
    return result;
  }



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值