Commit da146c2a708387059d4e31cbe1db0fdd8ceb41e3
1 parent
7de5358d
Excel注解支持设置BigDecimal精度&舍入规则
Showing
2 changed files
with
26 additions
and
3 deletions
ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java
| ... | ... | @@ -4,6 +4,7 @@ import java.lang.annotation.ElementType; |
| 4 | 4 | import java.lang.annotation.Retention; |
| 5 | 5 | import java.lang.annotation.RetentionPolicy; |
| 6 | 6 | import java.lang.annotation.Target; |
| 7 | +import java.math.BigDecimal; | |
| 7 | 8 | |
| 8 | 9 | /** |
| 9 | 10 | * 自定义导出Excel数据注解 |
| ... | ... | @@ -30,7 +31,7 @@ public @interface Excel |
| 30 | 31 | public String dateFormat() default ""; |
| 31 | 32 | |
| 32 | 33 | /** |
| 33 | - * 如果是字典类型,请设置字典的type值 | |
| 34 | + * 如果是字典类型,请设置字典的type值 (如: sys_user_sex) | |
| 34 | 35 | */ |
| 35 | 36 | public String dictType() default ""; |
| 36 | 37 | |
| ... | ... | @@ -45,6 +46,16 @@ public @interface Excel |
| 45 | 46 | public String separator() default ","; |
| 46 | 47 | |
| 47 | 48 | /** |
| 49 | + * BigDecimal 精度 默认:-1(默认不开启BigDecimal格式化) | |
| 50 | + */ | |
| 51 | + public int scale() default -1; | |
| 52 | + | |
| 53 | + /** | |
| 54 | + * BigDecimal 舍入规则 默认:BigDecimal.ROUND_HALF_EVEN | |
| 55 | + */ | |
| 56 | + public int roundingMode() default BigDecimal.ROUND_HALF_EVEN; | |
| 57 | + | |
| 58 | + /** | |
| 48 | 59 | * 导出类型(0数字 1字符串) |
| 49 | 60 | */ |
| 50 | 61 | public ColumnType cellType() default ColumnType.STRING; | ... | ... |
ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java
| ... | ... | @@ -8,6 +8,7 @@ import java.io.OutputStream; |
| 8 | 8 | import java.lang.reflect.Field; |
| 9 | 9 | import java.lang.reflect.Method; |
| 10 | 10 | import java.math.BigDecimal; |
| 11 | +import java.text.DecimalFormat; | |
| 11 | 12 | import java.util.ArrayList; |
| 12 | 13 | import java.util.Arrays; |
| 13 | 14 | import java.util.Comparator; |
| ... | ... | @@ -546,10 +547,14 @@ public class ExcelUtil<T> |
| 546 | 547 | { |
| 547 | 548 | cell.setCellValue(convertByExp(Convert.toStr(value), readConverterExp, separator)); |
| 548 | 549 | } |
| 549 | - else if (StringUtils.isNotEmpty(dictType)) | |
| 550 | + else if (StringUtils.isNotEmpty(dictType) && StringUtils.isNotNull(value)) | |
| 550 | 551 | { |
| 551 | 552 | cell.setCellValue(convertDictByExp(Convert.toStr(value), dictType, separator)); |
| 552 | 553 | } |
| 554 | + else if (value instanceof BigDecimal && -1 != attr.scale()) | |
| 555 | + { | |
| 556 | + cell.setCellValue((((BigDecimal) value).setScale(attr.scale(), attr.roundingMode())).toString()); | |
| 557 | + } | |
| 553 | 558 | else |
| 554 | 559 | { |
| 555 | 560 | // 设置列类型 |
| ... | ... | @@ -896,7 +901,14 @@ public class ExcelUtil<T> |
| 896 | 901 | } |
| 897 | 902 | else |
| 898 | 903 | { |
| 899 | - val = new BigDecimal(val.toString()); // 浮点格式处理 | |
| 904 | + if ((Double) val % 1 > 0) | |
| 905 | + { | |
| 906 | + val = new BigDecimal(val.toString()); | |
| 907 | + } | |
| 908 | + else | |
| 909 | + { | |
| 910 | + val = new DecimalFormat("0").format(val); | |
| 911 | + } | |
| 900 | 912 | } |
| 901 | 913 | } |
| 902 | 914 | else if (cell.getCellTypeEnum() == CellType.STRING) | ... | ... |