Easypoi导入导出快速上手

原创阿虎
发布于:2020-08-17 
标签: 

1. 介绍

Easypoi的目标不是替代poi,而是让一个不懂导入导出的快速使用poi完成Excel和word的各种操作,而不是看很多api才可以完成这样工作

独特的功能

http://doc.wupaas.com/docs/easypoi/

2. 引入依赖

<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-spring-boot-starter</artifactId>
    <version>4.2.0</version>
</dependency>

3. 导入

以下针对Controller的excel导入示例进行说明

3.1 导入和校验数据

/**
 * 导入学生实体
 * 
 * @author hushow
 * @date 2020年8月13日 下午1:04:01
 */
@Getter
@Setter
public class StudentDTO implements IExcelModel {

    @Excel(name = "学生名称")
    @NotNull(message = "不能为空")
    private String name;

    @Excel(name = "班级编号")
    @Pattern(regexp = "^-?[1-9]\\d*$", message = "格式不正确")
    private String no;

    @Excel(name = "年级")
    @NotNull(message = "不能为空")
    private String grade;

    @Excel(name = "邮箱", width = 25)
    @Pattern(regexp = "^[A-Za-z0-9]+([_\\.][A-Za-z0-9]+)"
        + "*@([A-Za-z0-9\\-]+\\.)+[A-Za-z]{2,6}$", message = "格式不正确")
    private String email;

    @Excel(name = "班主任姓名")
    private String teacherName;

    @Excel(name = "班主任手机号")
    @Pattern(regexp = "^[1]([3-9])[0-9]{9}$", message = "格式不正确")
    private String teacherPhone;

    private String errorMsg;
}

关键说明:
验证失败,如果需要拿到每一行的验证errorMsg信息,则导入实体需要实现IExcelModel接口

@PostMapping("/importVerify")
@ResponseBody
public List<StudentDTO> importVerify(ServletContext servletContext, 
    @RequestParam("myfile") MultipartFile myfile) {

    try {

        ImportParams params = new ImportParams();
        // 设置需要验证数据
        params.setNeedVerify(true);
        ExcelImportResult<StudentDTO> result =
            ExcelImportUtil.importExcelMore(myfile.getInputStream(), StudentDTO.class, params);

        // 处理校验错误数据
        if (!CollectionUtils.isEmpty(result.getFailList())) {
            log.debug("开始错误数据================================");

            String failName = "fail_" + myfile.getName();
            File tmpDir = WebUtils.getTempDir(servletContext);
            File failFile = new File(tmpDir, failName);
            FileOutputStream fos = new FileOutputStream(failFile);
            result.getFailWorkbook().write(fos);
            fos.close();

            log.debug("错误文件已写入:" + failFile.getAbsolutePath());

            // 失败的数据
            for (int i = 0; i < result.getFailList().size(); i++) {
                log.debug(ReflectionToStringBuilder.toString(result.getFailList().get(i)));
            }
            log.debug("结束错误数据================================");
        }

        log.debug("开始处理成功数据================================");
        for (int i = 0; i < result.getList().size(); i++) {
            log.debug(ReflectionToStringBuilder.toString(result.getList().get(i)));
        }
        log.debug("结束处理成功数据================================");

        return result.getList();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        // 此处建议抛出具体的业务异常封装
        throw new RuntimeException("导入数据异常" + e.getMessage(), e);
    }
}

4. 导出

以下针对Controller的excel导出示例进行说明

4.1 普通导出