java 读取excl优化_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 管理。

com.github.houbb

iexcel

0.0.4

Excel 写入

示例

/**

* 写入到 excel 文件

* 直接将列表内容写入到文件

*/

public void writeTest() {

// 待生成的 excel 文件路径

final String filePath = PathUtil.getAppTestResourcesPath()+"/excelWriter03.xls";

// 对象列表

List 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 buildUserList() {

List 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 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);

读取指定下标

这里的下标从0开始,代表第一行数据,不包含头信息行。

ExcelBs.newInstance("excel文件路径").read(Class, 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 表头就是我们指定的中文。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值