IExcel 用於優雅地讀取和寫入 excel。java
避免大 excel 出現 oom,簡約而不簡單。。git
OO 的方式操做 excel,編程更加方便優雅。github
sax 模式讀取,SXSS 模式寫入。避免 excel 大文件 OOM。數據庫
基於註解,編程更加靈活。apache
寫入能夠基於對象列表,也能夠基於 Map,實際使用更加方便。編程
設計簡單,註釋完整。方便你們學習改造。api
變動日誌bash
實際工做和學習中,apache poi 操做 excel 過於複雜。app
近期也看了一些其餘的工具框架:框架
easypoi
easyexcel
hutool-poi
都或多或少難以知足本身的實際須要,因而就本身寫了一個操做 excel 導出的工具。
jdk1.7+
maven 3.x
使用 maven 管理。
<dependency>
<groupId>com.github.houbb</groupId>
<artifactId>iexcel</artifactId>
<version>0.0.4</version>
</dependency>
複製代碼
/** * 寫入到 excel 文件 * 直接將列表內容寫入到文件 */
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()
}
複製代碼
構建對象列表方法以下:
/** * 構建用戶類表 * @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 文件中全部信息 */
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文件路徑")
複製代碼
使用上述方式便可建立。會根據文件後綴,自動選取 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) | 讀取對象的類型 | 對象列表 | |
read(Class, 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 表頭就是咱們指定的中文。