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

项目简介

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 表头就是我们指定的中文。

    原文作者:叶止水ryo
    原文地址: https://blog.51cto.com/9250070/2439799
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞