From da146c2a708387059d4e31cbe1db0fdd8ceb41e3 Mon Sep 17 00:00:00 2001 From: RuoYi Date: Wed, 19 Aug 2020 11:20:31 +0800 Subject: [PATCH] =?UTF-8?q?Excel=E6=B3=A8=E8=A7=A3=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E8=AE=BE=E7=BD=AEBigDecimal=E7=B2=BE=E5=BA=A6&=E8=88=8D?= =?UTF-8?q?=E5=85=A5=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/ruoyi/common/annotation/Excel.java | 13 ++++++++++++- .../com/ruoyi/common/utils/poi/ExcelUtil.java | 16 ++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java b/ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java index eed41a2ab..f781b1e37 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java @@ -4,6 +4,7 @@ import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import java.math.BigDecimal; /** * 自定义导出Excel数据注解 @@ -30,7 +31,7 @@ public @interface Excel public String dateFormat() default ""; /** - * 如果是字典类型,请设置字典的type值 + * 如果是字典类型,请设置字典的type值 (如: sys_user_sex) */ public String dictType() default ""; @@ -44,6 +45,16 @@ public @interface Excel */ public String separator() default ","; + /** + * BigDecimal 精度 默认:-1(默认不开启BigDecimal格式化) + */ + public int scale() default -1; + + /** + * BigDecimal 舍入规则 默认:BigDecimal.ROUND_HALF_EVEN + */ + public int roundingMode() default BigDecimal.ROUND_HALF_EVEN; + /** * 导出类型(0数字 1字符串) */ diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java index d950b77f4..d5a23b155 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java @@ -8,6 +8,7 @@ import java.io.OutputStream; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.math.BigDecimal; +import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; @@ -546,10 +547,14 @@ public class ExcelUtil { cell.setCellValue(convertByExp(Convert.toStr(value), readConverterExp, separator)); } - else if (StringUtils.isNotEmpty(dictType)) + else if (StringUtils.isNotEmpty(dictType) && StringUtils.isNotNull(value)) { cell.setCellValue(convertDictByExp(Convert.toStr(value), dictType, separator)); } + else if (value instanceof BigDecimal && -1 != attr.scale()) + { + cell.setCellValue((((BigDecimal) value).setScale(attr.scale(), attr.roundingMode())).toString()); + } else { // 设置列类型 @@ -896,7 +901,14 @@ public class ExcelUtil } else { - val = new BigDecimal(val.toString()); // 浮点格式处理 + if ((Double) val % 1 > 0) + { + val = new BigDecimal(val.toString()); + } + else + { + val = new DecimalFormat("0").format(val); + } } } else if (cell.getCellTypeEnum() == CellType.STRING)