AutoExcel實(shí)現(xiàn)百萬(wàn)數(shù)據(jù)秒級(jí)導(dǎo)入導(dǎo)出

GitHub | 博客 | 使用手冊(cè)

AutoExcel V2.0.0 帶來(lái)以下新特性

  1. 支持百萬(wàn)數(shù)據(jù)秒級(jí)導(dǎo)入導(dǎo)出
  2. 重寫(xiě)導(dǎo)入方法

Maven

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

支持百萬(wàn)數(shù)據(jù)秒級(jí)導(dǎo)入導(dǎo)出

以某巴巴的EasyExcel(版本2.2.6)作為對(duì)比,相同環(huán)境下測(cè)得以下結(jié)果,單位為毫秒

10W行10列數(shù)據(jù) 100W行10列數(shù)據(jù)
AutoExcel (模板導(dǎo)出) 6,258 23,540
EasyExcel (模板導(dǎo)出) 7,358 28,881
AutoExcel (直接導(dǎo)出) 5,711 24,952
EasyExcel (直接導(dǎo)出) 5,775 27,118
AutoExcel (導(dǎo)入) 4,466 21,595
AutoExcel (導(dǎo)入+轉(zhuǎn)換) 4,823 26,279
EasyExcel (導(dǎo)入) 5,966 30,844

可見(jiàn),AutoExcel和EasyExcel在導(dǎo)入導(dǎo)出效率上相差不大,因?yàn)閮烧叩膶?dǎo)出都是基于SXSSF,導(dǎo)入都是基于SAX,只是AutoExcel對(duì)POI的封裝更加輕量,所以效率會(huì)稍微高一些,數(shù)據(jù)量越大越明顯。

當(dāng)然,實(shí)際上,AutoExcel并非專(zhuān)注于導(dǎo)入導(dǎo)出的效率,而是開(kāi)發(fā)體驗(yàn),為開(kāi)發(fā)者提供自動(dòng)化功能的同時(shí),讓開(kāi)發(fā)者書(shū)寫(xiě)盡可能少的代碼。如自動(dòng)應(yīng)用單元格樣式、自動(dòng)填充行號(hào)、自動(dòng)填充公式、自動(dòng)匯總、自動(dòng)列寬等,詳細(xì)用法可前往AutoExcel——Excel導(dǎo)入導(dǎo)出利器查看。

重寫(xiě)導(dǎo)入方法

V1.0.0中基于模板的導(dǎo)入方式已被廢棄,新版中需使用FieldSetting來(lái)指定列名與字段名的映射關(guān)系

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);
    // 方式一、直接獲取數(shù)據(jù),沒(méi)有類(lèi)型轉(zhuǎn)換,可通過(guò)這種方式檢驗(yàn)數(shù)據(jù)是否符合要求
    List<Map<String, Object>> products = dataSet.get("Product");
    List<Map<String, Object>> projects = dataSet.get("Project");
    // 方式二、通過(guò)sheet索引獲取指定類(lèi)的數(shù)據(jù),類(lèi)型自動(dòng)轉(zhuǎn)換,轉(zhuǎn)換失敗將拋出異常
    // List<Product> products = dataSet.get(0, Product.class);
    // List<Project> projects= dataSet.get(1, Project.class);
    // 方式三、通過(guò)sheet名稱(chēng)獲取指定類(lèi)的數(shù)據(jù),類(lèi)型自動(dòng)轉(zhuǎn)換,轉(zhuǎn)換失敗將拋出異常
    // 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構(gòu)造方法入?yún)ⅲ?/p>

  1. sheetIndex:必填,sheet索引
  2. fieldSettings:必填,列名與字段名映射設(shè)置
  3. titleIndex:可省略,標(biāo)題行開(kāi)始索引,默認(rèn)為0
  4. dataStartIndex:可省略,數(shù)據(jù)行開(kāi)始索引,默認(rèn)為1

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

  1. 非侵入式,不影響原來(lái)的代碼
  2. 在系統(tǒng)設(shè)計(jì)的時(shí)候,為了重用相同的配置,如頁(yè)面展示、導(dǎo)出、導(dǎo)入、打印都展示相同的列名,我們會(huì)將這些配置存放在存儲(chǔ)介質(zhì)如數(shù)據(jù)庫(kù),待使用時(shí)加載出來(lái),這種方式也可以避免硬編碼,還可以方便地進(jìn)行動(dòng)態(tài)配置,采用FieldSetting就是為了配合這種方式。AutoExcel盡可能讓導(dǎo)入導(dǎo)出融入到你的自動(dòng)化系統(tǒng)。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容