IExcel 用于优雅地读取和写入 excel。
避免大 excel 出现 oom,简约而不简单。。
变更日志
实际工作和学习中,apache poi 操作 excel 过于复杂。
近期也看了一些其他的工具框架:
都或多或少难以满足自己的实际需要,于是就自己写了一个操作 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<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 表头就是我们指定的中文。