NutzCN Logo
问答 导入excle的功能,小数点后的数值被去掉了
发布于 2065天前 作者 qq_4945a577 2419 次浏览 复制 上一个帖子 下一个帖子
标签:

excle的某一个单元格,值是0.5 ,实体对应的字段为string 数据库为varchar2 (50), 上传时被读成为 0,(exle 单元格值为1.5时,被读成1). 单元格的格式为“常规”格式换成”字符串“还是会出现这个问题

6 回复

报个issue吧,nutzmore,或者下源码debug看看

 public static <T> T rowValue(Row row, J4EConf j4eConf, Mirror<T> mc) {
        // FIXME 必须有标准构造函数
        T rVal = mc.born();
        for (J4EColumn jcol : j4eConf.getColumns()) {
            Field jfield = jcol.getField();
            if (null != jfield) {
                Cell cell = row.getCell(jcol.getColumnIndex()
                                        + j4eConf.getPassColumn());
                if (null == cell) {
                    log.warn(String.format("cell [%d, %d] has null, so value is ''",
                                           row.getRowNum(),
                                           jcol.getColumnIndex()));
                }
                String cVal = (null == cell ? "" : cellValue(cell, jcol));//cell读取出的数值是0.5,但是这里执行完就是0了
                if (jcol.getFromExcelFun() != null) {
                    J4ECellFromExcel cellFun = jcol.getFromExcelFun();
                    Object setVal = cellFun.fromExcel(cVal);
                    mc.setValue(rVal, jfield, setVal);
                } else {
                    mc.setValue(rVal, jfield, cVal);
                }
            }
        }
        return rVal;
    }

是不是j4eConf的参数需要配置?
怎么配置? 没有说明文档啊?

 public static String cellValue(Cell c, J4EColumn jcol) {
        J4EColumnType colType = null;
        if (jcol != null) {
            colType = jcol.getColumnType();
        }
        if (null == colType) {
            colType = J4EColumnType.STRING;
        }
        try {
            // 3.1.5之后将可直接调用c.getCellType返回枚举
            CellType cType = c.getCellTypeEnum();
            switch (cType) {
            case NUMERIC: // 数字
                if (DateUtil.isCellDateFormatted(c)) {
                    Date dval = c.getDateCellValue();
                    if (jcol.getDtFormat() != null) {
                        try {
                            return Times.format(jcol.getDtFormat()[1], dval);
                        }
                        catch (Exception e) {
                            log.error(String.format("cell [%d, %d] datetime formate err, value %s [%s-%s]",
                                                    c.getRowIndex(),
                                                    c.getColumnIndex(),
                                                    dval.toString(),
                                                    jcol.getDtFormat()[0],
                                                    jcol.getDtFormat()[1],
                                                    e));
                        }
                    }
                    return Times.sDT(dval);
                }
                if (J4EColumnType.STRING == colType) {
                    // 按照整形来拿, 防止2B的科学计数法
                    DecimalFormat df = new DecimalFormat("0");
                    return df.format(c.getNumericCellValue());
                } else if (J4EColumnType.NUMERIC == colType) {
                    if (jcol.getPrecision() == 0) {
                        // 整数
                        return "" + (int) c.getNumericCellValue();
                    } else {
                        // 按照double数字拿
                        String fString = "0."
                                         + Strings.alignLeft("",
                                                             jcol.getPrecision(),
                                                             '0');
                        DecimalFormat df = new DecimalFormat(fString);
                        return df.format(c.getNumericCellValue());
                    }
                } else {
                    throw new RuntimeException("WTF, CELL_TYPE_NUMERIC is what!");
                }
                // 按照字符拿
            case STRING: // 字符串
                String strResult = Strings.trim(c.getStringCellValue());
                if (!Strings.isBlank(strResult) && jcol != null) {
                    // 日期转换
                    if (jcol.getDtFormat() != null) {
                        try {
                            strResult = Times.format(jcol.getDtFormat()[1],
                                                     Times.parse(jcol.getDtFormat()[0],
                                                                 strResult));
                        }
                        catch (Exception e) {
                            log.error(String.format("cell [%d, %d] datetime formate err, value %s [%s-%s]",
                                                    c.getRowIndex(),
                                                    c.getColumnIndex(),
                                                    strResult,
                                                    jcol.getDtFormat()[0],
                                                    jcol.getDtFormat()[1],
                                                    e));
                        }
                    }
                    // 文字转换
                    J4EFormat strFormat = jcol.getField()
                                              .getAnnotation(J4EFormat.class);
                    if (strFormat != null) {
                        if (strFormat.LowerCase()) {
                            strResult = strResult.toLowerCase();
                        }
                        if (strFormat.UpperCase()) {
                            strResult = strResult.toUpperCase();
                        }
                    }
                }
                return strResult;
            case BOOLEAN: // boolean
                return String.valueOf(c.getBooleanCellValue());
            case FORMULA:
                return Strings.trim(String.valueOf(c.getStringCellValue()));
            default:
                return Strings.trim(c.getStringCellValue());
            }
        }
        catch (Exception e) {
            log.error(String.format("cell [%d, %d] has error, value can't convert to string",
                                    c.getRowIndex(),
                                    c.getColumnIndex()),
                      e);
            return "";
        }
    }
 DecimalFormat df = new DecimalFormat("0");好像被这段代码坑了

// 按照整形来拿, 防止2B的科学计数法
DecimalFormat df = new DecimalFormat("0");

// 按照整形来拿, 防止2B的科学计数法
 DecimalFormat df = new DecimalFormat("0");

j4e我改成下边的方式了,issues提到码云上了

// 按照整形来拿, 防止2B的科学计数法
                    DecimalFormat df = new DecimalFormat("###.###");
添加回复
请先登陆
回到顶部