項目簡介
IExcel 用於優雅地讀取和寫入 excel。
避免大 excel 出現 oom,簡約而不簡單。。
特性
-
OO 的方式操作 excel,編程更加方便優雅。
-
sax 模式讀取,SXSS 模式寫入。避免 excel 大文件 OOM。
-
基於註解,編程更加靈活。
-
寫入可以基於對象列表,也可以基於 Map,實際使用更加方便。
- 設計簡單,註釋完整。方便大家學習改造。
變更日誌
v0.0.4 主要變化
- 引入 ExcelBs 引導類,優化使用體驗。
創作緣由
實際工作和學習中,apache poi 操作 excel 過於複雜。
近期也看了一些其他的工具框架:
-
easypoi
-
easyexcel
- hutool-poi
都或多或少難以滿足自己的實際需要,於是就自己寫了一個操作 excel 導出的工具。
快速開始
環境要求
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文件路徑")
使用上述方式即可創建。會根據文件後綴,自動選取 03 excel 或者 07 excel 進行讀寫。
屬性配置
屬性說明
屬性值 | 類型 | 默認值 | 說明 |
---|---|---|---|
path | 字符串 | NA | 默認創建 ExcelBs 時要指定,可以通過 path() 方法再次指定。 |
bigExcelMode | 布爾 | false | 是否是大 Excel 模式,如果寫入/讀取的內容較大,建議設置爲 true |
設置
Fluent 模式設置
- 設置舉例
ExcelBs.newInstance("excel文件路徑").bigExcelMode(true)
方法說明
方法概覽
方法 | 參數 | 返回值 | 說明 |
---|---|---|---|
append(Collection<?>) | 對象列表 | ExcelBs | 將列表寫入到緩衝區,但是不寫入文件 |
write() | 無 | void | 將緩衝區中對象寫入到文件 |
write(Collection<?>) | 無 | void | 將緩衝區中對象寫入到文件,並將列表中寫入到文件 |
read(Class<T>) | 讀取對象的類型 | 對象列表 | |
read(Class<T>, startIndex, endIndex) | 對象類型,開始下標,結束下標 | 對象列表 |
寫入
一次性寫入
最常用的方式,直接寫入。
ExcelBs.newInstance("excel文件路徑").write(Collection<?>)
多次寫入
有時候我們要多次構建對象列表,比如從數據庫中分頁讀取。
則可以使用如下的方式:
ExcelBs.newInstance("excel文件路徑").append(Collection<?>)
.append(Collection<?>).write()
讀取文件
讀取所有
ExcelBs.newInstance("excel文件路徑").read(Class<T>);
讀取指定下標
這裏的下標從0開始,代表第一行數據,不包含頭信息行。
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 表頭就是我們指定的中文。