easyexcel 日期、数字格式化
*********************
相关注解
@DateTimeFormat:标注在字段上
@Target({})
@Retention()
@Inherited
public @interface DateTimeFormat {
String value() default "";
boolean use1904windowing() default false;
}
写入excel sheet中时,转换为指定格式(value)的字符串;
从excel sheet中读取value格式的字符串,转换为对应的日期类型
@NumberFormat:标注在字段上
@Target({})
@Retention()
@Inherited
public @interface NumberFormat {
String value() default "";
RoundingMode roundingMode() default RoundingMode.HALF_UP;
}
写入excel sheet中时,转换为指定格式(value)的字符串;
从excel sheet中读取value格式的字符串,转换为对应的数字类型
*********************
示例
*************
head 类
Order
@Data
public class Order {
private Integer id;
private Date createTime;
@DateTimeFormat("yyyy-MM-dd")
private Date payTime;
@NumberFormat("##.00")
private Double totalFee;
}
*************
监听器
CustomListener
class CustomListener extends AnalysisEventListener<Order2>{
@Override
public void invoke(Order2 order2, AnalysisContext analysisContext) {
(order2);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
("数据解析完成");
}
}
*************
测试类
Test
public class Test2 {
private static final String file_path="e:"+ +"java"++"easyexcel"++"write_test5.xlsx";
public static void write(){
List<Order> list=new ArrayList<>();
Order order=new Order();
(1);
(new Date());
(new Date());
(10d);
(order);
(file_path,).sheet().doWrite(list);
}
public static void read(){
(file_path,,new CustomListener()).sheet().doRead();
}
public static void main(String[] args){
write();
read();
}
}
*********************
使用测试
write
read
Order(id=1, createTime=Fri Jul 24 10:55:40 GMT+08:00 2020, payTime=Fri Jul 24 00:00:00 GMT+08:00 2020, totalFee=10.0)
数据解析完成