EasyPoi的导入和导出功能

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EasyPoi的导入和导出功能相关的知识,希望对你有一定的参考价值。

参考技术A

EasyPoi是一款开源的软件,用于开发EXCEL表格的导入和导出功能,简单易上手,代码量也很少,非常适合初学者去使用。我们使用Java开发Excel导入导出功能,以前常用的是Poi,但是需要编写的代码量太多,使用EasyPoi仅需要在代码中添加注解,便可以完成大部分普通的Excel编辑工作。

工具/材料

IntelliJ IDEA

我们是使用Maven管理项目,首先我们需要添加开发EasyPoi所依赖的jar包,如下所示。


<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>

EasyPoi 可以自适应Excel的xls和xlsx两种格式,我们今天主要讲解使用注解进行导入和导出的功能,我们只要修改注解就可以修改Excel的字段和格式。我们需要编写实体和Excel表格的对应关系,在实体上添加注解。@Excel 注解作用到filed(列)上面,是对列的描述。@Excel注解的name属性即为列名,format用于设置时间的格式。我们创建一个PersonEntity,实现序列化接口,并添加如下的注解,另外该类需要getter和setter方法。

我们写一个测试类去测试导出功能,创建一个测试类,并在main方法中编写测试代码,我们使用easypoi工具类ExcelExportUtil 的exportExcel方法,其中new ExportParams()是标题和sheet的基本设置,当然这些设置有些不是必须的,可以根据需要自行修改。具体的导出代码如下图所示。

接下来我们右键执行该方法,此时会生成一个excel文件,如下图所示,我们可以看到设置的标题和数据已存在该excel表格中。

接下来我们编写导入的方法,将上面生成的excel表格导入,使用ExcelImportUtil工具类的importExcel方法,ImportParams用于设置导入参数。

接下来我们测试导入功能,在执行方法后提示创建对象异常,我们需要检查异常的原因,此时发现我们上面创建的实体类没有无参构造方法,因为我们已经创建了一个有参的构造,就不会帮我们自动创建无参构造了,因此我们需要手动创建,如下图所示。

另外,我们在编写PersonEntity实体类时,步骤2的id字段上面没有@Excel注解,所以该字段不起任何作用,若不需要的话可以删除。

特别提示

EasyPoi注解作用的实体类一定要有无参构造,若实体类中存在有参构造,一定要手动创建一个无参构造。

EasyPOI导入导出Excel

EasyPOI工具可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,通过简单的注解和模板

导入maven依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- Swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>
        <!-- slf4j -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
        <!-- easypoi导入导出excel -->
        <!-- easypoi-base 导入导出的工具包,可以完成Excel导出,导入,Word的导出,Excel的导出功能 -->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>3.1.0</version>
        </dependency>
        <!-- easypoi-web 耦合了spring-mvc 基于AbstractView,极大的简化spring-mvc下的导出功能 -->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>3.1.0</version>
        </dependency>
        <!-- easypoi-annotation 基础注解包,作用与实体对象上,拆分后方便maven多工程的依赖管理 -->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>3.1.0</version>
        </dependency>

创建实体类:课程-老师-学生

package com.qjc.excel.entity;

import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelCollection;
import cn.afterturn.easypoi.excel.annotation.ExcelEntity;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import lombok.Data;

import java.util.List;

/**
 * @Description:
 * @Author: qjc
 * @Date: 2019/11/27
 */
@Data
@ExcelTarget("courseEntity")
public class CourseEntity {
    /**
     * @Excel 作用到filed上面, 是对Excel一列的一个描述
     * @ExcelCollection 表示一个集合, 主要针对一对多的导出, 比如一个老师对应多个科目, 科目就可以用集合表示
     * @ExcelEntity 表示一个继续深入导出的实体, 但他没有太多的实际意义, 只是告诉系统这个对象里面同样有导出的字段
     * @ExcelIgnore 和名字一样表示这个字段被忽略跳过这个导导出
     * @ExcelTarget 这个是作用于最外层的对象, 描述这个对象的id, 以便支持一个对象可以针对不同导出做出不同处理
     */

    private String id;
    /**
     * 课程名称
     */
    @Excel(name = "课程名称", orderNum = "1", width = 25)
    private String name;
    /**
     * 老师主键
     */
    @ExcelEntity(id = "absent")
    private TeacherEntity mathTeacher;

    @ExcelCollection(name = "学生", orderNum = "4")
    private List<StudentEntity> students;
}
package com.qjc.excel.entity;

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

/**
 * @Description:
 * @Author: qjc
 * @Date: 2019/11/27
 */
@ExcelTarget("teacherEntity")
@Data
public class TeacherEntity {

    private String id;
    /**
     * name
     */
    @Excel(name = "主讲老师_major,代课老师_absent", orderNum = "1", isImportField = "true_major,true_absent")
    private String name;
}
package com.qjc.excel.entity;

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

import java.util.Date;

/**
 * @Description:
 * @Author: qjc
 * @Date: 2019/11/27
 */
@Data
@ToString
public class StudentEntity {

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

导入导出工具类

package com.qjc.excel.utils;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;

/**
 * @Description:
 * @Author: qjc
 * @Date: 2019/11/27
 */
public class ExcelUtil {

    /**
     * 导出数据到Excel
     *
     * @param list           数据集合
     * @param title          数据第一行标题
     * @param sheetName      Sheet名称
     * @param pojoClass      实体Class
     * @param fileName       文件名
     * @param isCreateHeader 是否创建数据表头
     * @param response       响应
     */
    public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName,
                                   boolean isCreateHeader, HttpServletResponse response) {
        ExportParams exportParams = new ExportParams(title, sheetName);
        exportParams.setCreateHeadRows(isCreateHeader);
        defaultExport(list, pojoClass, fileName, response, exportParams);

    }

    /**
     * 导出数据到Excel
     *
     * @param list      数据集合
     * @param title     数据第一行标题
     * @param sheetName Sheet名称
     * @param pojoClass 实体Class
     * @param fileName  文件名
     * @param response  响应
     */
    public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName,
                                   HttpServletResponse response) {
        defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
    }

    public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response)
            throws Exception {
        defaultExport(list, fileName, response);
    }

    private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response,
                                      ExportParams exportParams) {
        Workbook workbook = null;
        try {
            workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (workbook != null)
            try {
                downLoadExcel(fileName, response, workbook);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }

    private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook)
            throws Exception {
        try {
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            throw new Exception(e.getMessage());
        }
    }

    private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response)
            throws Exception {
        Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
        if (workbook != null)
            ;
        downLoadExcel(fileName, response, workbook);
    }

    public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass)
            throws Exception {
        if (org.apache.commons.lang3.StringUtils.isBlank(filePath)) {
            return null;
        }
        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);
        List<T> list = null;
        try {
            list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
        } catch (NoSuchElementException e) {
            throw new Exception("模板不能为空");
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
        return list;
    }

    /**
     * 导入(解析)Excel
     *
     * @param file       Excel文件
     * @param titleRows  标题行数
     * @param headerRows 表头行数
     * @param pojoClass  实体类
     * @param <T>
     * @return
     * @throws Exception
     */
    public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)
            throws Exception {
        if (file == null) {
            return null;
        }
        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);
        List<T> list = null;
        try {
            list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
        } catch (NoSuchElementException e) {
            throw new Exception("excel文件不能为空");
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
        return list;
    }

}

Controller

package com.qjc.excel.controller;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.qjc.excel.entity.CourseEntity;
import com.qjc.excel.entity.StudentEntity;
import com.qjc.excel.entity.TeacherEntity;
import com.qjc.excel.utils.DateUtils;
import com.qjc.excel.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * @Description:
 * @Author: qjc
 * @Date: 2019/11/27
 */
@Api(value = "EasyPOI接口", tags = {"EasyPOI接口"})
@RestController
@CrossOrigin(origins = "*", methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE, RequestMethod.PUT})
@Slf4j
public class FileController {

    @ApiOperation(value = "对象定义--导出数据到Excel", notes = "对象定义--导出数据到Excel")
    @RequestMapping(value = "/exportExcel/object", method = RequestMethod.GET)
    public void exportData(HttpServletResponse response) {
        //从数据库中查询数据,直接用浏览器访问http://localhost:8777/exportExcel/object即可
        List<StudentEntity> studentEntityList = createStudents();

        //导出操作
        ExcelUtil.exportExcel(studentEntityList, "四大学生", "EasyPOISheet", StudentEntity.class, "学生.xls", response);
    }

    @ApiOperation(value = "导入Excel", notes = "导入Excel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public void importExcel(@RequestParam("textFile") MultipartFile file) throws Exception {
        //解析excel,直接通过swagger上传Excel就行
        List<StudentEntity> studentEntityList = ExcelUtil.importExcel(file, 1, 1, StudentEntity.class);
        for (StudentEntity studentEntity : studentEntityList) {
            System.err.println(studentEntity.toString());
        }
    }

    @ApiOperation(value = "集合定义--导出数据到Excel", notes = "集合定义--导出数据到Excel")
    @RequestMapping(value = "/importExcel/collection", method = RequestMethod.GET)
    public void importCollectionExcel(HttpServletResponse response) {
        //浏览器直接访问http://localhost:8777/importExcel/collection
        List<CourseEntity> courseEntityList = new ArrayList<>();

        CourseEntity courseEntity1 = new CourseEntity();
        courseEntity1.setName("生物");
        TeacherEntity teacherEntity1 = new TeacherEntity();
        teacherEntity1.setName("刘备");
        courseEntity1.setMathTeacher(teacherEntity1);
        courseEntity1.setStudents(createStudents());

        courseEntityList.add(courseEntity1);


        CourseEntity courseEntity2 = new CourseEntity();
        courseEntity2.setName("化学");
        TeacherEntity teacherEntity2 = new TeacherEntity();
        teacherEntity2.setName("曹操");
        courseEntity2.setMathTeacher(teacherEntity2);
        courseEntity2.setStudents(createStudents());

        courseEntityList.add(courseEntity2);

        //导出操作
        ExcelUtil.exportExcel(courseEntityList, "课程-老师-学生", "course", CourseEntity.class, "课程.xls", response);
    }


    /**
     * 创建学生集合
     *
     * @return
     */
    private List<StudentEntity> createStudents() {
        List<StudentEntity> studentEntityList = new ArrayList<>();
        StudentEntity studentEntity1 = new StudentEntity();
        studentEntity1.setName("刘德华");
        studentEntity1.setSex(1);
        studentEntity1.setBirthday(DateUtils.formatStringToDate("2010-01-01", "yyyy-MM-dd"));
        studentEntity1.setRegistrationDate(DateUtils.formatStringToDate("2015-01-01", "yyyy-MM-dd"));
        StudentEntity studentEntity2 = new StudentEntity();
        studentEntity2.setName("黎明");
        studentEntity2.setSex(1);
        studentEntity2.setBirthday(DateUtils.formatStringToDate("2009-01-01", "yyyy-MM-dd"));
        studentEntity2.setRegistrationDate(DateUtils.formatStringToDate("2014-01-01", "yyyy-MM-dd"));
        StudentEntity studentEntity3 = new StudentEntity();
        studentEntity3.setName("郭富城");
        studentEntity3.setSex(2);
        studentEntity3.setBirthday(DateUtils.formatStringToDate("2008-01-01", "yyyy-MM-dd"));
        studentEntity3.setRegistrationDate(DateUtils.formatStringToDate("2013-01-01", "yyyy-MM-dd"));
        StudentEntity studentEntity4 = new StudentEntity();
        studentEntity4.setName("张学友");
        studentEntity4.setSex(2);
        studentEntity4.setBirthday(DateUtils.formatStringToDate("2011-01-01", "yyyy-MM-dd"));
        studentEntity4.setRegistrationDate(DateUtils.formatStringToDate("2016-01-01", "yyyy-MM-dd"));

        studentEntityList.add(studentEntity1);
        studentEntityList.add(studentEntity2);
        studentEntityList.add(studentEntity3);
        studentEntityList.add(studentEntity4);
        return studentEntityList;
    }

}

源码链接:

https://gitee.com/xiaorenwu_dashije/easypoi_example.git

以上是关于EasyPoi的导入和导出功能的主要内容,如果未能解决你的问题,请参考以下文章

java EasyPoi导入/导出

easypoi $fe和fe的区别

SpringBoot加Poi仿照EasyPoi实现Excel导出

easypoi导出的excel打不开

EasyPOI导入导出Excel

easypoiword导出图片发现无法读取的内容