EasyPoi

Easypoi 可以让初次接触 poi 的人,更简单的上手。

为谁而开发

  • 不太熟悉poi的

  • 不想写太多重复太多的

  • 只是简单的导入导出的

  • 喜欢使用模板的

快如入门

easy-poi 代码地址

Jar 引入

<dependencies>
    <dependency>
        <groupId>cn.afterturn</groupId>
        <artifactId>easypoi-base</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>cn.afterturn</groupId>
        <artifactId>easypoi-web</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>cn.afterturn</groupId>
        <artifactId>easypoi-annotation</artifactId>
        <version>3.1.0</version>
    </dependency>
</dependencies>

入门代码

  • 实体类
import java.io.Serializable;
import java.util.Date;

import cn.afterturn.easypoi.excel.annotation.Excel;
import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class Student implements Serializable {

    private static final long serialVersionUID = 6743357184603380077L;

    /**
     * id
     */
    private String id;
    /**
     * 学生姓名
     */
    @Excel(name = "学生姓名", height = 20, width = 30, isImportField = "true_st")
    private String name;
    /**
     * 学生性别
     */
    @Excel(name = "学生性别", replace = {"男_1", "女_2"}, suffix = "生", isImportField = "true_st")
    private int    sex;

    @Excel(name = "出生日期", databaseFormat = "yyyyMMddHHmmss", format = "yyyy-MM-dd", isImportField = "true_st", width = 20)
    private Date birthday;

    @Excel(name = "进校日期", databaseFormat = "yyyyMMddHHmmss", format = "yyyy-MM-dd")
    private Date registrationDate;

}
  • Easypoi.java
import org.apache.poi.ss.usermodel.Workbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Date;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;

public class Easypoi {

    public static void main(String[] args) throws IOException {
        Student student = Student.builder()
                .id("XXX")
                .name("ryo")
                .registrationDate(new Date())
                .birthday(new Date())
                .sex(2)
                .build();
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("计算机一班学生","学生"),
                Student.class, Arrays.asList(student));
        OutputStream outputStream = new FileOutputStream("student.xls");
        workbook.write(outputStream);
    }

}

运行方法会生成 student.xls