文章18 | 阅读 21384 | 点赞0
相关注解
@DateTimeFormat:标注在字段上
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface DateTimeFormat {
String value() default "";
boolean use1904windowing() default false;
}
**写入excel sheet中时,**转换为指定格式(value)的字符串;
**从excel sheet中读取value格式的字符串,**转换为对应的日期类型
@NumberFormat:标注在字段上
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@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) {
System.out.println(order2);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
System.out.println("数据解析完成");
}
}
测试类
Test
public class Test2 {
private static final String file_path="e:"+ File.separator+"java"+File.separator+"easyexcel"+File.separator+"write_test5.xlsx";
public static void write(){
List<Order> list=new ArrayList<>();
Order order=new Order();
order.setId(1);
order.setCreateTime(new Date());
order.setPayTime(new Date());
order.setTotalFee(10d);
list.add(order);
EasyExcel.write(file_path,Order.class).sheet().doWrite(list);
}
public static void read(){
EasyExcel.read(file_path,Order2.class,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)
数据解析完成
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_43931625/article/details/107548985
内容来源于网络,如有侵权,请联系作者删除!