java export excel best practices, large file excel avoid OOM (out of memory) -02-API framework

Project Description

IExcel for reading and writing gracefully excel.

Avoid large excel appear oom, but not simple. .

characteristic

  • OO way operation excel, programming more convenient and elegant.

  • sax mode read, SXSS write mode. Avoid large excel file OOM.

  • Annotation-based programming more flexible.

  • Written based on a list of objects, it can also be based Map, actually easier to use.

  • Simple design, complete comment. Facilitate learning transformation.

Change Log

Change Log

The main changes v0.0.4

  • Introducing ExcelBs bootstrap class, to optimize the experience.

Writing reason

Practical work and learning, apache poi excel too complicated operation.

Recently read some other tools framework:

  • easypoi

  • easyexcel

  • hutool-then

They are more or less difficult to meet their actual needs, so he wrote a tool excel export operations themselves.

Quick Start

Environmental requirements

jdk1.7 +

maven 3.x

Introducing jar

Use maven management.

<dependency>
     <groupId>com.github.houbb</groupId>
     <artifactId>iexcel</artifactId>
     <version>0.0.4</version>
</dependency>

Write Excel

Examples

/**
 * 写入到 excel 文件
 * 直接将列表内容写入到文件
 */
public void writeTest() {
    // 待生成的 excel 文件路径
    final String filePath = PathUtil.getAppTestResourcesPath()+"/excelWriter03.xls";

    // 对象列表
    List<User> models = User.buildUserList();

    // 直接写入到文件
    ExcelBs.newInstance(filePath).write(models);
}

among them:

  • User.java
public class User {

    private String name;

    private int age;

    //fluent getter/setter/toString()
}
  • buildUserList()

Construct an object list as follows:

/**
 * 构建用户类表
 * @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;
}

Write effect

excel contents into:

name    age
hello   20
excel   19

Excel reads

Examples

/**
 * 读取 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);
}

information

[User{name='hello', age=20}, User{name='excel', age=19}]

ExcelBs Profile

Compared to the static method, fluent object tool easier post-expansion.

For user convenience, providing a common default attributes, as well as flexible api interface.

Introduction to use

ExcelBs.newInstance("excel文件路径")

It can be created using the above method. Based on the file extension and automatically selects 03 excel or 07 excel read and write.

Configuration Properties

Property Description

Property Value Types of Defaults Explanation
path String NA To specify the default when you create ExcelBs, you can specify the method again by path ().
bigExcelMode Boolean false Whether it is large Excel model, if the contents of the write / read, the proposed set to true

Set up

Fluent mode settings

  • Setting an example
ExcelBs.newInstance("excel文件路径").bigExcelMode(true)

Method Description

Method Summary

method parameter return value Explanation
append(Collection<?>) Object List ExcelBs The list is written to the buffer, but not write to a file
write() no void The buffer object is written to the file
write(Collection<?>) no void The buffer object is written to a file, and writes to the file list
read(Class<T>) Read object type Object List
read(Class<T>, startIndex, endIndex) Object type, start index, end index Object List

Write

Write-once

The most common way to directly write.

ExcelBs.newInstance("excel文件路径").write(Collection<?>)

Multiple writing

Sometimes we want to build a list of multiple objects, such as page read from the database.

You may be used in the following manner:

ExcelBs.newInstance("excel文件路径").append(Collection<?>)
    .append(Collection<?>).write()

Read the file

Read all

ExcelBs.newInstance("excel文件路径").read(Class<T>);

Reads the specified index

Here the subscript starts from 0, representing the first row of data does not include header rows.

ExcelBs.newInstance("excel文件路径").read(Class<T>, 1, 1);

@ExcelField Brief introduction

Sometimes we need to be flexible in the specified field attributes, such as the corresponding excel header field names.

For example, if you want to read the content of this line.

@ExcelField Notes is designed for this purpose.

Explanatory notes

public @interface ExcelField {

    /**
     * excel 表头字段名称
     * 如果不传:默认使用当前字段名称
     * @return 字段名称
     */
    String headName() default "";

    /**
     * excel 文件是否需要写入此字段
     *
     * @return 是否需要写入此字段
     */
    boolean writeRequire() default true;

    /**
     * excel 文件是否读取此字段
     * @return 是否读取此字段
     */
    boolean readRequire() default true;

}

Examples of use

public class UserField {

    @ExcelField(headName = "姓名")
    private String name;

    @ExcelField(headName = "年龄")
    private int age;

}

Thus generated is what we excel header specified Chinese.

Guess you like

Origin blog.51cto.com/9250070/2439799