Browse Source

Excel注解支持设置BigDecimal精度&舍入规则

tags/v2.2
RuoYi 4 years ago
parent
commit
7401297236
  1. 11
      ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/annotation/Excel.java
  2. 14
      ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java
  3. 4
      ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/web/page/PageDomain.java

11
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/annotation/Excel.java

@ -4,6 +4,7 @@ import java.lang.annotation.ElementType; @@ -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数据注解
@ -40,6 +41,16 @@ public @interface Excel @@ -40,6 +41,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字符串
*/
public ColumnType cellType() default ColumnType.STRING;

14
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java

@ -6,6 +6,7 @@ import java.io.OutputStream; @@ -6,6 +6,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;
@ -536,6 +537,10 @@ public class ExcelUtil<T> @@ -536,6 +537,10 @@ public class ExcelUtil<T>
{
cell.setCellValue(convertByExp(Convert.toStr(value), readConverterExp, separator));
}
else if (value instanceof BigDecimal && -1 != attr.scale())
{
cell.setCellValue((((BigDecimal) value).setScale(attr.scale(), attr.roundingMode())).toString());
}
else
{
// 设置列类型
@ -831,7 +836,14 @@ public class ExcelUtil<T> @@ -831,7 +836,14 @@ public class ExcelUtil<T>
}
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)

4
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/web/page/PageDomain.java

@ -17,9 +17,9 @@ public class PageDomain @@ -17,9 +17,9 @@ public class PageDomain
/** 排序列 */
private String orderByColumn;
/** 排序的方向 "desc" 或者 "asc". */
private String isAsc;
/** 排序的方向desc或者asc */
private String isAsc = "asc";
public String getOrderBy()
{

Loading…
Cancel
Save