若依-@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;
相关推荐
醇氧26 分钟前
【wps】 excel 删除重复项
excel·wps
盛夏绽放9 小时前
Vue3 中 Excel 导出的性能优化与实战指南
vue.js·excel
Tomorrow'sThinker11 小时前
[特殊字符] Python 自动查找替换 Excel 单元格内容 —— 高效批量处理
excel
Shipley Leo11 小时前
如何在Excel中每隔几行取一行
excel
bing_15811 小时前
Excel 数据透视表不够用时,如何处理来自多个数据源的数据?
excel
想要入门的程序猿15 小时前
Qt写入excel
数据库·qt·excel
爱代码的小黄人1 天前
Excel VLOOKUP函数使用详解:原理、格式、常见错误与解决方案
excel
YouYOUyouFairy1 天前
EXCEL动态表格
excel
Maruko3101 天前
Java 实现excel大批量导出
excel·poi
忧郁的蛋~1 天前
HTML表格导出为Excel文件的实现方案
前端·html·excel