若依-@Excel新增注解numberFormat

@Excel注解中原本的scale会四舍五入小数,导致进度丢失

想要的效果

  • 显示的时候保留两个小数
  • 真正的数值是保留之前的数值

还原过程

若以中有一個專門的工具类,用来处理excel的

  • 找到EXCEL导出方法exportExcel()
  • 找到writeSheet,写表格的方法
  • 找到填充数据的方法fillExcelData
  • 找到添加单元格的方法addCell
  • 找到设置 单元格VO的方法 setCellVo

对于NUMBERIC类型的,但是设置format的属性值

java 复制代码
    /**
     * 设置单元格信息
     *
     * @param value 单元格值
     * @param attr  注解相关
     * @param cell  单元格信息
     */
    public void setCellVo(Object value, Excel attr, Cell cell) {
        if (ColumnType.STRING == attr.cellType()) {
            String cellValue = Convert.toStr(value);
            // 对于任何以表达式触发字符 =-+@开头的单元格,直接使用tab字符作为前缀,防止CSV注入。
            if (StringUtils.startsWithAny(cellValue, FORMULA_STR)) {
                cellValue = RegExUtils.replaceFirst(cellValue, FORMULA_REGEX_STR, "\t$0");
            }
            if (value instanceof Collection && StringUtils.equals("[]", cellValue)) {
                cellValue = StringUtils.EMPTY;
            }
            cell.setCellValue(StringUtils.isNull(cellValue) ? attr.defaultValue() : cellValue + attr.suffix());
        } else if (ColumnType.NUMERIC == attr.cellType()) {
            if (StringUtils.isNotNull(value)) {
                if (StringUtils.isNotEmpty(attr.numberFormat())) {
                    CellStyle numberCellStyle = cell.getCellStyle();
                    DataFormat dataFormat = this.wb.createDataFormat();
                    numberCellStyle.setDataFormat(dataFormat.getFormat(attr.numberFormat()));
                    cell.setCellStyle(numberCellStyle);
                }
                cell.setCellValue(StringUtils.contains(Convert.toStr(value), ".") ? Convert.toDouble(value) : Convert.toInt(value));
            }
        } else if (ColumnType.IMAGE == attr.cellType()) {
            ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1), cell.getRow().getRowNum() + 1);
            String imagePath = Convert.toStr(value);
            if (StringUtils.isNotEmpty(imagePath)) {
                byte[] data = ImageUtils.getImage(imagePath);
                getDrawingPatriarch(cell.getSheet()).createPicture(anchor,
                        cell.getSheet().getWorkbook().addPicture(data, getImageType(data)));
            }
        }
    }

别忘了在注解@Excel中加上我们的自定义注解

java 复制代码
    /**
     * 数字类型格式
     */
    public String numberFormat() default "";

使用

在对应的@Excel注解中,新增一个属性numberFormat

java 复制代码
    /**
     * 总重
     */
    @Excel(name = "总重", cellType = Excel.ColumnType.NUMERIC, align = HorizontalAlignment.RIGHT,numberFormat = "#,##0.00")
    private BigDecimal totalWeight;
相关推荐
Access开发易登软件3 小时前
Access 和 SQLite,根本不在一个赛道上
java·jvm·数据库·sqlite·excel·vba·access开发
biuyyyxxx11 小时前
Excel常见异常
excel
猫猫不是喵喵.12 小时前
关于解析Excel中的日期出现是数字序列的问题
excel
asdzx6712 小时前
Python TXT 转 Excel (自动识别分隔符)
python·excel
也要大步向前呀12 小时前
excel(1)将多行内容合并到一行里
excel
Non-existent98714 小时前
海拔批量查询 + 批量 KML 生成工具-WPS 插件 TableGIS 新功能
javascript·c++·excel·wps
Metaphor6921 天前
使用 Python 在 Excel 文件中查找并替换
python·excel
xiaomin-Michael1 天前
EXCEL pivot table
excel
包子源1 天前
PDF 转 Word/Excel 全链路实战:Next.js + 阿里云文档智能
pdf·word·excel
Java知识技术分享1 天前
excel动态表头实现+异步使用
java·spring·excel