欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

EasyExcel案例

程序员文章站 2022-06-24 19:26:26
案例: 开撸public interface DataHandler {// 数据 List data(ReporterContext context);}public interface ReporterHandler extends DataHandler { default Logger getLogger() { return LoggerFactory.getLogger(this...

案例: 开撸

public interface DataHandler<T> {
	// 数据
    List<T> data(ReporterContext context);
    /*
    @Setter
public class ReporterContext {
    @Getter
    private List<CompleteDetail> completeDetailDataList ==Lists.newLinkedList();

    @Getter
    private List<AvailableDetail> availableDetailDataList = Lists.newLinkedList();
}
*/
}
public interface ReporterHandler<T> extends DataHandler<T> {

    default Logger getLogger() {
        return LoggerFactory.getLogger(this.getClass());
    }

    default void writeHead(ExcelWriter excelWriter, ReporterContext context) {
        getLogger().info("sheetNo: {}, sheetName: {}", sheetNo(), sheetName());

        WriteSheet writeSheet = EasyExcel.writerSheet(sheetNo(), sheetName()).head(head()).build();
        excelWriter.write(Lists.newArrayList(), writeSheet);
    }

    default void writeData(ExcelWriter excelWriter, ReporterContext context) {
        getLogger().info("sheetNo: {}, sheetName: {}", sheetNo(), sheetName());

        WriteSheet writeSheet = EasyExcel.writerSheet(sheetNo(), sheetName()).head(head()).build();
        // 数据的来源 super
        excelWriter.write(data(context), writeSheet);
    }

    Integer sheetNo(); // 起始页

    String sheetName();// sheet名称

    Class<T> head();	// 头部

    Integer dataOrder(); // 顺序
}

实现

@RequiredArgsConstructor
@Component
// 并重写以上的方法
public class CompleteDetailReporterHandler implements ReporterHandler<CompleteDetail> {

    @Getter
    private final List<CompleteDetailDataHandler> dataHandlers;

    @Override
    // 数据来源
    public List<CompleteDetail> data(ReporterContext context) {
        List<CompleteDetail> dataList = Lists.newLinkedList();

        if (CollectionUtils.isNotEmpty(getDataHandlers())) {
            for (DataHandler<CompleteDetail> dataHandler : getDataHandlers()) {
                dataList.addAll(dataHandler.data(context));
            }
        }

        context.setCompleteDetailDataList(dataList);

        return dataList;
    }

    @Override
    public Integer sheetNo() {
        return 3;
    }

    @Override
    public String sheetName() {
        return "完整性详表";
    }
/*
@Data
public class CompleteDetail {

    @ExcelProperty(value = "资源类型")
    private String type;

    @ExcelProperty(value = "UUID")
    private String uuid;
 }
 ....省略
*/
    @Override
    // 看上面的注释的实体
    public Class<CompleteDetail> head() {
        return CompleteDetail.class;
    }

    @Override
    public Integer dataOrder() {
        return 2;
    }

}

本文地址:https://blog.csdn.net/qq_43406764/article/details/110829193