項目簡介 IExcel 用於優雅地讀取和寫入 excel。 避免大 excel 出現 oom,簡約而不簡單。 特性 一行代碼搞定一切 OO 的方式操作 excel,編程更加方便優雅。 sax 模式讀取,SXSS 模式寫入。避免 excel 大文件 OOM。 基於註解,編程更加靈活。 設計簡單,註釋完 ...
項目簡介
IExcel 用於優雅地讀取和寫入 excel。
避免大 excel 出現 oom,簡約而不簡單。
特性
-
一行代碼搞定一切
-
OO 的方式操作 excel,編程更加方便優雅。
-
sax 模式讀取,SXSS 模式寫入。避免 excel 大文件 OOM。
-
基於註解,編程更加靈活。
-
設計簡單,註釋完整。方便大家學習改造。
-
可根據註解指定表頭順序
-
支持 excel 文件內容 bytes[] 內容獲取,便於用戶自定義操作。
變更日誌
v0.0.9 主要變更
Fixed @ExcelField註解失效問題
創作緣由
實際工作和學習中,apache poi 操作 excel 過於複雜。
近期也看了一些其他的工具框架:
-
easypoi
-
easyexcel
-
hutool-poi
都或多或少難以滿足自己的實際需要,於是就自己寫了一個操作 excel 導出的工具。
實現:在阿裡 easyexcel 的基礎上進行封裝,提升使用的簡易度。
快速開始
環境要求
jdk1.8+
maven 3.x
引入 jar
使用 maven 管理。
<dependency>
<groupId>com.github.houbb</groupId>
<artifactId>iexcel</artifactId>
<version>1.0.0</version>
</dependency>
Excel 寫入
示例
// 基本屬性
final String filePath = PathUtil.getAppTestResourcesPath()+"/excelHelper.xls";
List<User> models = User.buildUserList();
// 直接寫入到文件
ExcelHelper.write(filePath, 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 讀取
示例
final String filePath = PathUtil.getAppTestResourcesPath()+"/excelHelper.xls";
List<User> userList = ExcelHelper.read(filePath, User.class);
信息
[User{name='hello', age=20}, User{name='excel', age=19}]
SAX 讀
// 待生成的 excel 文件路徑
final String filePath = PathUtil.getAppTestResourcesPath()+"/excelReadBySax.xls";
AbstractSaxReadHandler<User> saxReadHandler = new AbstractSaxReadHandler<User>() {
@Override
protected void doHandle(int i, List<Object> list, User user) {
System.out.println(user);
}
};
ExcelHelper.readBySax(User.class, saxReadHandler, filePath);
文檔
Bug & Issues
歡迎提出寶貴意見:Bug & Issues
本文由博客一文多發平臺 OpenWrite 發佈!