欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

java導出excel最佳實踐,大文件excel避免OOM(內存溢出)框架-02-API

項目簡介

IExcel 用于優(yōu)雅地讀取和寫入 excel。

目前創(chuàng)新互聯(lián)公司已為數(shù)千家的企業(yè)提供了網(wǎng)站建設、域名、網(wǎng)站空間、網(wǎng)站托管、服務器托管、企業(yè)網(wǎng)站設計、尼河口網(wǎng)站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

避免大 excel 出現(xiàn) oom,簡約而不簡單。。

特性

  • OO 的方式操作 excel,編程更加方便優(yōu)雅。

  • sax 模式讀取,SXSS 模式寫入。避免 excel 大文件 OOM。

  • 基于注解,編程更加靈活。

  • 寫入可以基于對象列表,也可以基于 Map,實際使用更加方便。

  • 設計簡單,注釋完整。方便大家學習改造。

變更日志

變更日志

v0.0.4 主要變化

  • 引入 ExcelBs 引導類,優(yōu)化使用體驗。

創(chuàng)作緣由

實際工作和學習中,apache poi 操作 excel 過于復雜。

近期也看了一些其他的工具框架:

  • easypoi

  • easyexcel

  • hutool-poi

都或多或少難以滿足自己的實際需要,于是就自己寫了一個操作 excel 導出的工具。

快速開始

環(huán)境要求

jdk1.7+

maven 3.x

引入 jar

使用 maven 管理。

<dependency>
     <groupId>com.github.houbb</groupId>
     <artifactId>iexcel</artifactId>
     <version>0.0.4</version>
</dependency>

Excel 寫入

示例

/**
 * 寫入到 excel 文件
 * 直接將列表內容寫入到文件
 */
public void writeTest() {
    // 待生成的 excel 文件路徑
    final String filePath = PathUtil.getAppTestResourcesPath()+"/excelWriter03.xls";

    // 對象列表
    List<User> models = User.buildUserList();

    // 直接寫入到文件
    ExcelBs.newInstance(filePath).write(models);
}

其中:

  • User.java
public class User {

    private String name;

    private int age;

    //fluent getter/setter/toString()
}
  • buildUserList()

構建對象列表方法如下:

/**
 * 構建用戶類表
 * @return 用戶列表
 * @since 0.0.4
 */
public static List<User> buildUserList() {
    List<User> users = new ArrayList<>();
    users.add(new User().name("hello").age(20));
    users.add(new User().name("excel").age(19));
    return users;
}

寫入效果

excel 內容生成為:

name    age
hello   20
excel   19

Excel 讀取

示例

/**
 * 讀取 excel 文件中所有信息
 */
public void readTest() {
    // 待生成的 excel 文件路徑
    final String filePath = PathUtil.getAppTestResourcesPath()+"/excelWriter03.xls";
    List<User> userList = ExcelBs.newInstance(filePath).read(User.class);
    System.out.println(userList);
}

信息

[User{name='hello', age=20}, User{name='excel', age=19}]

ExcelBs 簡介

相比較于 static 方法,fluent 的對象工具更便于后期拓展。

為了用戶方便使用,提供了常見的默認屬性,以及靈活的 api 接口。

使用簡介

ExcelBs.newInstance("excel文件路徑")

使用上述方式即可創(chuàng)建。會根據(jù)文件后綴,自動選取 03 excel 或者 07 excel 進行讀寫。

屬性配置

屬性說明

屬性值類型默認值說明
path 字符串 NA 默認創(chuàng)建 ExcelBs 時要指定,可以通過 path() 方法再次指定。
bigExcelMode 布爾 false 是否是大 Excel 模式,如果寫入/讀取的內容較大,建議設置為 true

設置

Fluent 模式設置

  • 設置舉例
ExcelBs.newInstance("excel文件路徑").bigExcelMode(true)

方法說明

方法概覽

方法參數(shù)返回值說明
append(Collection<?>) 對象列表 ExcelBs 將列表寫入到緩沖區(qū),但是不寫入文件
write() void 將緩沖區(qū)中對象寫入到文件
write(Collection<?>) void 將緩沖區(qū)中對象寫入到文件,并將列表中寫入到文件
read(Class<T>) 讀取對象的類型 對象列表
read(Class<T>, startIndex, endIndex) 對象類型,開始下標,結束下標 對象列表

寫入

一次性寫入

最常用的方式,直接寫入。

ExcelBs.newInstance("excel文件路徑").write(Collection<?>)

多次寫入

有時候我們要多次構建對象列表,比如從數(shù)據(jù)庫中分頁讀取。

則可以使用如下的方式:

ExcelBs.newInstance("excel文件路徑").append(Collection<?>)
    .append(Collection<?>).write()

讀取文件

讀取所有

ExcelBs.newInstance("excel文件路徑").read(Class<T>);

讀取指定下標

這里的下標從0開始,代表第一行數(shù)據(jù),不包含頭信息行。

ExcelBs.newInstance("excel文件路徑").read(Class<T>, 1, 1);

@ExcelField 簡介

有時候我們需要靈活的指定字段屬性,比如對應的 excel 表頭字段名稱。

比如是否要讀寫這一行內容。

@ExcelField 注解就是為此設計。

注解說明

public @interface ExcelField {

    /**
     * excel 表頭字段名稱
     * 如果不傳:默認使用當前字段名稱
     * @return 字段名稱
     */
    String headName() default "";

    /**
     * excel 文件是否需要寫入此字段
     *
     * @return 是否需要寫入此字段
     */
    boolean writeRequire() default true;

    /**
     * excel 文件是否讀取此字段
     * @return 是否讀取此字段
     */
    boolean readRequire() default true;

}

使用例子

public class UserField {

    @ExcelField(headName = "姓名")
    private String name;

    @ExcelField(headName = "年齡")
    private int age;

}

這樣生成的 excel 表頭就是我們指定的中文。

分享標題:java導出excel最佳實踐,大文件excel避免OOM(內存溢出)框架-02-API
瀏覽路徑:http://www.chinadenli.net/article6/gecpig.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內鏈面包屑導航網(wǎng)站策劃外貿(mào)建站虛擬主機

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設公司