首先EasyExcel是非常奈斯的工具
但是文档中一些功能需要我们去摸索,例如我需要保留6位小数,在官方文档找了半天。。。
下面是官网对于数字的自定义导出的说明,emmm。。。
直接说使用吧,使用@NumberFormat("0.000000")可以设置保留6位小数,并且看格式,应该可以明白要几位填几个0,而且@NumberFormat这个注解还有一些格式设置可以探索的,毕竟它很强大!
@ExcelProperty(value = "结算汇率")
@NumberFormat("0.000000")
protected BigDecimal chargeExchangeRate;
同时借助一篇文档我也发现了另外一个注解@ContentStyle可以设置格式,当然@ContentStyle就是设置格式的。
在官方文档的这个位置被提起,但显然,一样需要我们自己去探索使用。。。
例如:我使用的@ContentStyle(dataFormat = 2)其中dataFormat实际对应的格式和显示的方式并没有详细的说明。
@ExcelProperty(value = "应收金额")
@ContentStyle(dataFormat = 2)
protected BigDecimal payment;
ContentStyle注解的部分源码,可以看到dataFormat是一个short类型
@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface ContentStyle {
short dataFormat() default -1;
}
而对应的dataFormat参数在BuiltinFormats类中的BUILTIN_FORMATS_CN则有着对应的解释格式,例如我上面选择的是2对应的是"0.00",即保留两位小数,你可以看到里面有居多的格式类型,好想官网给出每个类型的解释。当然有兴趣的可以一个个探究。
public class BuiltinFormats {
public static short GENERAL = 0;
public static final String[] BUILTIN_FORMATS_CN = new String[]{"General", "0", "0.00", "#,##0", "#,##0.00", ""¥"#,##0_);("¥"#,##0)", ""¥"#,##0_);[Red]("¥"#,##0)", ""¥"#,##0.00_);("¥"#,##0.00)", ""¥"#,##0.00_);[Red]("¥"#,##0.00)", "0%", "0.00%", "0.00E+00", "# ?/?", "# ??/??", "yyyy/m/d", "d-mmm-yy", "d-mmm", "mmm-yy", "h:mm AM/PM", "h:mm:ss AM/PM", "h:mm", "h:mm:ss", "yyyy-m-d h:mm", null, null, null, null, "yyyy"年"m"月"", "m"月"d"日"", "m"月"d"日"", "m-d-yy", "yyyy"年"m"月"d"日"", "h"时"mm"分"", "h"时"mm"分"ss"秒"", "上午/下午h"时"mm"分"", "上午/下午h"时"mm"分"ss"秒"", "yyyy"年"m"月"", "#,##0_);(#,##0)", "#,##0_);[Red](#,##0)", "#,##0.00_);(#,##0.00)", "#,##0.00_);[Red](#,##0.00)", "_(* #,##0_);_(* (#,##0);_(* "-"_);_(@_)", "_("¥"* #,##0_);_("¥"* (#,##0);_("¥"* "-"_);_(@_)", "_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)", "_("¥"* #,##0.00_);_("¥"* (#,##0.00);_("¥"* "-"??_);_(@_)", "mm:ss", "[h]:mm:ss", "mm:ss.0", "##0.0E+0", "@", "yyyy"年"m"月"", "m"月"d"日"", "yyyy"年"m"月"", "m"月"d"日"", "m"月"d"日"", "上午/下午h"时"mm"分"", "上午/下午h"时"mm"分"ss"秒"", "yyyy"年"m"月"", "m"月"d"日"", "t0", "t0.00", "t#,##0", "t#,##0.00", null, null, null, null, "t0%", "t0.00%", "t# ?/?", "t# ??/??", "ว/ด/ปปปป", "ว-ดดด-ปป", "ว-ดดด", "ดดด-ปป", "ช:นน", "ช:นน:ทท", "ว/ด/ปปปป ช:นน", "นน:ทท", "[ช]:นน:ทท", "นน:ทท.0", "d/m/bb"};
public static final String[] BUILTIN_FORMATS_US = new String[]{"General", "0", "0.00", "#,##0", "#,##0.00", ""$"#,##0_);("$"#,##0)", ""$"#,##0_);[Red]("$"#,##0)", ""$"#,##0.00_);("$"#,##0.00)", ""$"#,##0.00_);[Red]("$"#,##0.00)", "0%", "0.00%", "0.00E+00", "# ?/?", "# ??/??", "yyyy/m/d", "d-mmm-yy", "d-mmm", "mmm-yy", "h:mm AM/PM", "h:mm:ss AM/PM", "h:mm", "h:mm:ss", "yyyy-m-d h:mm", null, null, null, null, "yyyy"年"m"月"", "m"月"d"日"", "m"月"d"日"", "m-d-yy", "yyyy"年"m"月"d"日"", "h"时"mm"分"", "h"时"mm"分"ss"秒"", "上午/下午h"时"mm"分"", "上午/下午h"时"mm"分"ss"秒"", "yyyy"年"m"月"", "#,##0_);(#,##0)", "#,##0_);[Red](#,##0)", "#,##0.00_);(#,##0.00)", "#,##0.00_);[Red](#,##0.00)", "_(* #,##0_);_(* (#,##0);_(* "-"_);_(@_)", "_("$"* #,##0_);_("$"* (#,##0);_("$"* "-"_);_(@_)", "_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)", "_("$"* #,##0.00_);_("$"* (#,##0.00);_("$"* "-"??_);_(@_)", "mm:ss", "[h]:mm:ss", "mm:ss.0", "##0.0E+0", "@", "yyyy"年"m"月"", "m"月"d"日"", "yyyy"年"m"月"", "m"月"d"日"", "m"月"d"日"", "上午/下午h"时"mm"分"", "上午/下午h"时"mm"分"ss"秒"", "yyyy"年"m"月"", "m"月"d"日"", "t0", "t0.00", "t#,##0", "t#,##0.00", null, null, null, null, "t0%", "t0.00%", "t# ?/?", "t# ??/??", "ว/ด/ปปปป", "ว-ดดด-ปป", "ว-ดดด", "ดดด-ปป", "ช:นน", "ช:นน:ทท", "ว/ด/ปปปป ช:นน", "นน:ทท", "[ช]:นน:ทท", "นน:ทท.0", "d/m/bb"};
public static final Map<String, Short> BUILTIN_FORMATS_MAP_CN;
public static final Map<String, Short> BUILTIN_FORMATS_MAP_US;
public static final short MIN_CUSTOM_DATA_FORMAT_INDEX = 82;
public BuiltinFormats() {
}
}
感谢: