@Excel注解中原本的scale会四舍五入小数,导致进度丢失
想要的效果
- 显示的时候保留两个小数
- 真正的数值是保留之前的数值
还原过程
若以中有一個專門的工具类,用来处理excel的
- 找到EXCEL导出方法exportExcel()
- 找到writeSheet,写表格的方法
- 找到填充数据的方法fillExcelData
- 找到添加单元格的方法addCell
- 找到设置 单元格VO的方法
setCellVo
对于NUMBERIC类型的,但是设置format的属性值
/*** 设置单元格信息** @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中加上我们的自定义注解
/*** 数字类型格式*/public String numberFormat() default "";
使用
在对应的@Excel注解中,新增一个属性numberFormat
/*** 总重*/@Excel(name = "总重", cellType = Excel.ColumnType.NUMERIC, align = HorizontalAlignment.RIGHT,numberFormat = "#,##0.00")private BigDecimal totalWeight;