IExcel 用于優(yōu)雅地讀取和寫入 excel。
目前創(chuàng)新互聯(lián)公司已為數(shù)千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、網(wǎng)站托管、服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計、尼河口網(wǎng)站維護等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
避免大 excel 出現(xiàn) oom,簡約而不簡單。。
OO 的方式操作 excel,編程更加方便優(yōu)雅。
sax 模式讀取,SXSS 模式寫入。避免 excel 大文件 OOM。
基于注解,編程更加靈活。
寫入可以基于對象列表,也可以基于 Map,實際使用更加方便。
變更日志
實際工作和學(xué)習(xí)中,apache poi 操作 excel 過于復(fù)雜。
近期也看了一些其他的工具框架:
easypoi
easyexcel
都或多或少難以滿足自己的實際需要,于是就自己寫了一個操作 excel 導(dǎo)出的工具。
jdk1.7+
maven 3.x
使用 maven 管理。
<dependency>
<groupId>com.github.houbb</groupId>
<artifactId>iexcel</artifactId>
<version>0.0.4</version>
</dependency>
/**
* 寫入到 excel 文件
* 直接將列表內(nèi)容寫入到文件
*/
public void writeTest() {
// 待生成的 excel 文件路徑
final String filePath = PathUtil.getAppTestResourcesPath()+"/excelWriter03.xls";
// 對象列表
List<User> models = User.buildUserList();
// 直接寫入到文件
ExcelBs.newInstance(filePath).write(models);
}
其中:
public class User {
private String name;
private int age;
//fluent getter/setter/toString()
}
構(gòu)建對象列表方法如下:
/**
* 構(gòu)建用戶類表
* @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 內(nèi)容生成為:
name age
hello 20
excel 19
/**
* 讀取 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}]
相比較于 static 方法,fluent 的對象工具更便于后期拓展。
為了用戶方便使用,提供了常見的默認屬性,以及靈活的 api 接口。
ExcelBs.newInstance("excel文件路徑")
使用上述方式即可創(chuàng)建。會根據(jù)文件后綴,自動選取 03 excel 或者 07 excel 進行讀寫。
屬性值 | 類型 | 默認值 | 說明 |
---|---|---|---|
path | 字符串 | NA | 默認創(chuàng)建 ExcelBs 時要指定,可以通過 path() 方法再次指定。 |
bigExcelMode | 布爾 | false | 是否是大 Excel 模式,如果寫入/讀取的內(nèi)容較大,建議設(shè)置為 true |
Fluent 模式設(shè)置
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) | 對象類型,開始下標,結(jié)束下標 | 對象列表 |
最常用的方式,直接寫入。
ExcelBs.newInstance("excel文件路徑").write(Collection<?>)
有時候我們要多次構(gòu)建對象列表,比如從數(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
簡介有時候我們需要靈活的指定字段屬性,比如對應(yīng)的 excel 表頭字段名稱。
比如是否要讀寫這一行內(nèi)容。
@ExcelField
注解就是為此設(shè)計。
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導(dǎo)出excel最佳實踐,大文件excel避免OOM(內(nèi)存溢出)框架-02-API
瀏覽路徑:http://muchs.cn/article6/gecpig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、面包屑導(dǎo)航、網(wǎng)站策劃、外貿(mào)建站、、虛擬主機
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)