文章目录

  • 环境搭建
  • 编写测试代码
  • 替换文本导入导出(最常用)
  • 导入导出图片路径替换成图片显示(easypoi的特色功能)
  • 一对多


环境搭建

创建springboot工程,不累述,然后导入依赖

<!--easypoi的相关依赖-->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>4.1.0</version>
        </dependency>

也可以直接使用启动器,只是本案例不需要这么多其他的依赖,因为其实不仅可以处理excel还可以处理其他的文件,具体的看文档吧。

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

实体类:

public class User {
    /**
     * 用户的主键
     */
    private Integer userId;
    /**
     * 名字
     */
    @Excel(name = "用户名")
    private String userName;
    /**
     * 头像图片地址
     */
    @Excel(name = "头像地址")
    private String userImage;
    /**
     * 用户的状态 0代表正常 1代表删除
     */
    @Excel(name = "用户状态")
    private Integer userStatus;
    
    //省略getter,setter,toString,空参全参构造

编写测试代码

替换文本导入导出(最常用)

导入导出测试代码

/**
     * easypoi导出
     */
    @Test
    public void exportTest() throws IOException {
        //模拟数据库查询数据
        ArrayList<User> list = new ArrayList<>();
        list.add(new User(1,"zhangsan","a.jpg",1));
        list.add(new User(2,"lisi","a.jpg",1));
        list.add(new User(3,"wangwu","a.jpg",1));
        list.add(new User(4,"xiaoliu","a.jpg",1));
        /**
         * 导出参数对象
         * 参数1 标题
         * 参数2 表的名字
         */
        ExportParams exportParams = new ExportParams("所有用户的数据", "userdatas");

        /**
         * exportExcel 导出Excel文件
         * 参数1 导出参数对象
         * 参数2 要导出的实体类的类对象
         * 参数3 要导出的数据 需要一个集合  数据库查询出来的用户对象的集合
         *
         * 返回值就是封装好的文件对象
         */
        Workbook workbook= ExcelExportUtil.exportExcel(exportParams, User.class, list);
        //"C:\\Users\\HaSee\\Desktop\\user.xls",C:\Users\HaSee\
        workbook.write(new FileOutputStream("C:\\Users\\HaSee\\Desktop\\user.xls"));

    }

    @Test
    public void importTest() throws Exception {
        FileInputStream inputStream = new FileInputStream("C:\\Users\\HaSee\\Desktop\\user.xls");

        /**
         * ImportParams 导入参数对象
         * 定义标题栏和表头数据
         */
        ImportParams importParams = new ImportParams();
        importParams.setTitleRows(1);
        importParams.setHeadRows(1);

        List<User> userlist = ExcelImportUtil.importExcel(inputStream, User.class, importParams);

        for (User user:userlist){
            System.out.println(user);
        }
    }

导出测试:

springboot easyexcel 模板 springboot easypoi_User


springboot easyexcel 模板 springboot easypoi_java_02


导入测试:

springboot easyexcel 模板 springboot easypoi_easy excel_03


导出值得替换:

实体类加入替换导入是正常变0,导出测试0变正常

springboot easyexcel 模板 springboot easypoi_spring boot_04

导出,值的替换:

springboot easyexcel 模板 springboot easypoi_java_05


直接测试导入,会自动将字符串替换回int:

springboot easyexcel 模板 springboot easypoi_User_06

导入导出图片路径替换成图片显示(easypoi的特色功能)

实现原理根据值得路径找到图片,再通过io流保存到指定可读取位置,实现图片回显

springboot easyexcel 模板 springboot easypoi_easy excel_07


springboot easyexcel 模板 springboot easypoi_spring boot_08


运行测试:

springboot easyexcel 模板 springboot easypoi_easy excel_09


springboot easyexcel 模板 springboot easypoi_excel_10


导入测试,可见以变成保存路径的生成的文件名了,和桌面的一致

springboot easyexcel 模板 springboot easypoi_easy excel_11

一对多

public class User {
    /**
     * 用户的主键
     */
    private Integer userId;
    /**
     * 名字
     */
    @Excel(name = "用户名")
    private String userName;
    /**
     * 头像图片地址
     */
    @Excel(name = "头像地址",type = 2,imageType = 1,width = 40,height = 20,savePath = "C:\\Users\\HaSee\\Desktop")
    private String userImage;
    /**
     * 用户的状态 0代表正常 1代表删除
     */
    @Excel(name = "用户状态",replace = {"正常_0","冻结_1"})
    private Integer userStatus;

    /**
     * 用户的联络方式,一个用户可以有多个
     */
    @ExcelCollection(name = "用户的所有联络方式")
    List<Contact> contacts;
    
     //省略getter,setter,toString,空参全参构造
    }
@Test
    public void oneManyExportTest() throws IOException {
        List<Contact> contacts = new ArrayList<>();
        contacts.add(new Contact("020","广州"));
        contacts.add(new Contact("0755","深圳"));

        //模拟数据库拿数据
        ArrayList<User> userList = new ArrayList<>();
        userList.add(new User(1,"zhangsan","C:\\Users\\HaSee\\Desktop\\a.jpg",1,contacts));
        userList.add(new User(5,"lisi","C:\\Users\\HaSee\\Desktop\\a.jpg",1,contacts));

        /**
         * 导出参数对象
         * 参数1 标题
         * 参数2 表的名字
         */
        ExportParams exportParams = new ExportParams("所有用户的数据","allUserData");

        /**
         * exportExcel 导出Excel文件
         * 参数1 导出参数对象
         * 参数2 要导出的实体类的类对象
         * 参数3 要导出的数据 需要一个集合  数据库查询出来的老师对象的集合
         *
         * 返回值就是封装好的文件对象
         */
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, User.class, userList);

        workbook.write(new FileOutputStream("C:\\Users\\HaSee\\Desktop\\userData.xls"));
    }

测试:

springboot easyexcel 模板 springboot easypoi_excel_12


似乎不够优雅,有单元格空着,使用合并单元格属性。

springboot easyexcel 模板 springboot easypoi_excel_13


开启needMerge!!

跑多一次看看效果:

springboot easyexcel 模板 springboot easypoi_java_14


确实合并了,图片是由于设计的尺寸,看着像一般,实际不是。


网上有一些使用从数据库获取整合的案例简单案例:
https://gitee.com/dominiczjt/easypoi-springboot