easyexcel 日期、数字格式化

x33g5p2x  于2021-12-28 转载在 其他  
字(1.9k)|赞(0)|评价(0)|浏览(2393)

easyexcel 日期、数字格式化

*********************

相关注解

@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)
数据解析完成

相关文章