阅读 68

AutoExcel实现百万数据秒级导入导出

GitHub | 博客 | 使用手册

AutoExcel V2.0.0 带来以下新特性

  1. 支持百万数据秒级导入导出
  2. 重写导入方法

Maven

<dependency>
  <groupId>net.fenghaitao</groupId>
  <artifactId>auto-excel</artifactId>
  <version>2.0.0</version>
</dependency>

支持百万数据秒级导入导出

以某巴巴的EasyExcel(版本2.2.6)作为对比,相同环境下测得以下结果,单位为毫秒

10W行10列数据 100W行10列数据
AutoExcel (模板导出) 6,258 23,540
EasyExcel (模板导出) 7,358 28,881
AutoExcel (直接导出) 5,711 24,952
EasyExcel (直接导出) 5,775 27,118
AutoExcel (导入) 4,466 21,595
AutoExcel (导入+转换) 4,823 26,279
EasyExcel (导入) 5,966 30,844

可见,AutoExcel和EasyExcel在导入导出效率上相差不大,因为两者的导出都是基于SXSSF,导入都是基于SAX,只是AutoExcel对POI的封装更加轻量,所以效率会稍微高一些,数据量越大越明显。

当然,实际上,AutoExcel并非专注于导入导出的效率,而是开发体验,为开发者提供自动化功能的同时,让开发者书写尽可能少的代码。如自动应用单元格样式、自动填充行号、自动填充公式、自动汇总、自动列宽等,详细用法可前往AutoExcel——Excel导入导出利器查看。

重写导入方法

V1.0.0中基于模板的导入方式已被废弃,新版中需使用FieldSetting来指定列名与字段名的映射关系

public void importExcel() {
    List<ImportPara> importParas = new ArrayList<ImportPara>() {{
        add(new ImportPara(0, DataGenerator.genProductFieldSettings()));
        add(new ImportPara(1, DataGenerator.genProjectFieldSettings(), 1, 5));
    }};
    String fileName = this.getClass().getResource("/template/Import.xlsx").getPath();
    DataSet dataSet = AutoExcel.read(fileName, importParas);
    // 方式一、直接获取数据,没有类型转换,可通过这种方式检验数据是否符合要求
    List<Map<String, Object>> products = dataSet.get("Product");
    List<Map<String, Object>> projects = dataSet.get("Project");
    // 方式二、通过sheet索引获取指定类的数据,类型自动转换,转换失败将抛出异常
    // List<Product> products = dataSet.get(0, Product.class);
    // List<Project> projects= dataSet.get(1, Project.class);
    // 方式三、通过sheet名称获取指定类的数据,类型自动转换,转换失败将抛出异常
    // List<Product> products = dataSet.get("Product", Product.class);
    // List<Project> projects = dataSet.get("Project", Project.class);
}
public static List<FieldSetting> genProjectFieldSettings() {
    List<FieldSetting> fieldSettings = new ArrayList<>();
    fieldSettings.add(new FieldSetting("projName", "Project Name"));
    fieldSettings.add(new FieldSetting("projInfo", "Project Info."));
    fieldSettings.add(new FieldSetting("basalArea", "Basal Area"));
    fieldSettings.add(new FieldSetting("availableArea", "Available Area"));
    fieldSettings.add(new FieldSetting("buildingArea", "Building Area"));
    fieldSettings.add(new FieldSetting("buildingsNumber", "Buildings Number"));
    fieldSettings.add(new FieldSetting("saleStartDate", "Sales Start Date"));
    fieldSettings.add(new FieldSetting("landAcquisitionTime", "Land Acquisition Time"));
    fieldSettings.add(new FieldSetting("availablePrice", "Available Price"));
    fieldSettings.add(new FieldSetting("availableAmount", "Available Amount"));
    fieldSettings.add(new FieldSetting("insideArea", "Inside Area"));
    return fieldSettings;
}

public static List<FieldSetting> genProductFieldSettings() {
    List<FieldSetting> fieldSettings = new ArrayList<FieldSetting>() {{
        add(new FieldSetting("projName", "Project Name"));
        add(new FieldSetting("basalArea", "Basal Area"));
        add(new FieldSetting("availableArea", "Available Area"));
        add(new FieldSetting("buildingArea", "Building Area"));
        add(new FieldSetting("buildingsNumber", "Buildings Number"));
    }};
    return fieldSettings;
}

ImportPara构造方法入参:

  1. sheetIndex:必填,sheet索引
  2. fieldSettings:必填,列名与字段名映射设置
  3. titleIndex:可省略,标题行开始索引,默认为0
  4. dataStartIndex:可省略,数据行开始索引,默认为1

为什么使用FieldSetting不采用注解的方式声明列名?

  1. 非侵入式,不影响原来的代码
  2. 在系统设计的时候,为了重用相同的配置,如页面展示、导出、导入、打印都展示相同的列名,我们会将这些配置存放在存储介质如数据库,待使用时加载出来,这种方式也可以避免硬编码,还可以方便地进行动态配置,采用FieldSetting就是为了配合这种方式。AutoExcel尽可能让导入导出融入到你的自动化系统。

作者:冯海涛_Ivan

原文链接:https://www.jianshu.com/p/5bf10bb3dcd2

文章分类
后端
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐