若依-@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;
相关推荐
hello_simon1 小时前
免费在线文档工具,在线PDF添加空白页,免费在任意位置插入空白页,多样化的文件处理
pdf·html·excel·pdf转html·excel转pdf格式
chenchihwen2 小时前
常用 Excel VBA 技巧,简单好学易上手
excel
夔8882 小时前
Excel通过VBA脚本去除重复数据行并保存
java·服务器·excel
啊波次得饿佛哥4 小时前
excel中的VBA指令示例(一)
excel·vba
inxunoffice4 小时前
批量将 JSON 转换为 Excel/思维导入等其它格式
json·excel
忘忧记5 小时前
如何在 Excel 中处理并拆分你提供的这种混合单位的库存数据。
excel
CAT_cwds21 小时前
EasyExcel-一款好用的excel生成工具
excel·easy
Eiceblue1 天前
C# 设置Excel中文本的对齐方式、换行、和旋转
开发语言·c#·excel
刘大猫.1 天前
java导入excel更新设备经纬度度数或者度分秒
java·excel·导入excel·经纬度·度分秒·经纬度 度数·经纬度 度分秒