java 导出 excel 最佳实践,大文件 excel 避免OOM(内存溢出) 框架-02-API 优化

项目简介

IExcel 用于优雅地读取和写入 excel。java

避免大 excel 出现 oom,简约而不简单。。git

特性

  • OO 的方式操做 excel,编程更加方便优雅。github

  • sax 模式读取,SXSS 模式写入。避免 excel 大文件 OOM。数据库

  • 基于注解,编程更加灵活。apache

  • 写入能够基于对象列表,也能够基于 Map,实际使用更加方便。编程

  • 设计简单,注释完整。方便你们学习改造。api

变动日志

变动日志bash

v0.0.4 主要变化

  • 引入 ExcelBs 引导类,优化使用体验。

创做原因

实际工做和学习中,apache poi 操做 excel 过于复杂。app

近期也看了一些其余的工具框架:框架

  • 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) 读取对象的类型 对象列表
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 表头就是咱们指定的中文。

相关文章
相关标签/搜索