当前位置 博文首页 > 平头哥的技术博文:easypoi 操作 excel 表格,这也太 easy 了吧

    平头哥的技术博文:easypoi 操作 excel 表格,这也太 easy 了吧

    作者:[db:作者] 时间:2021-07-13 19:07

    what is easypoi?

    easypoi是一款简化版的poi工具,让你不需要太多的poi知识就能够使用poi完成Excel和word的各种操作。

    easypoi官方文档,点击官方文档

    怎使用easypoi

    1、创建一个springboot项目,在pom.xml中引入依赖

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

    2、编写easypoiutil

    public class EasyPoiUtils {
    
        /**
         *  导出excel
         * @param pojoClass
         * @param dataSet
         * @param path
         * @param filename
         * @throws IOException
         */
        public static void exportExcel(Class<?> pojoClass, Collection<?> dataSet,String path,String filename) throws IOException {
    
            File savefile = new File(path);
            if (!savefile.exists()) {
                savefile.mkdirs();
            }
            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), pojoClass, dataSet);
            FileOutputStream fos = new FileOutputStream(path+filename);
            workbook.write(fos);
            fos.close();
        }
    
        /**
         * 根据Map创建对应的Excel(一个excel 创建多个sheet)
         * @param list list 多个Map key title 对应表格Title key entity 对应表格对应实体 key data
         *      *             Collection 数据
         * @param path 路径
         * @param filename 文件名
         * @throws IOException
         */
        public static void  exportExcel(List<Map<String, Object>> list,String path,String filename) throws IOException{
            File savefile = new File(path);
            if (!savefile.exists()) {
                savefile.mkdirs();
            }
            Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
    
            FileOutputStream fos = new FileOutputStream(path+filename);
            workbook.write(fos);
            fos.close();
        }
    
    
        /**
         * 导入excel
         * @param file
         * @param pojoClass
         * @param params
         * @param <T>
         * @return
         */
        public static <T>List<T> importExcel(File file, Class<?> pojoClass, ImportParams params){
            long start = new Date().getTime();
            List<T> list = ExcelImportUtil.importExcel(file,UserEntity.class, params);
            return list;
        }
    }
    

    3、创建实体类

    创建实体类,在需要导出的字段上添加@Excel注解。

    /**
     * 用户实体类
     */
    public class UserEntity {
    
        @Excel(name = "ID")
        private int id;
    
        @Excel(name = "姓名")
        private String name;
    
        @Excel(name = "电子邮件",width = 20)
        private String email;
    
        @Excel(name = "年龄")
        private int age;
    
        @Excel(name = "性别",replace={"男_1", "女_2"})
        private int sex;
    
        @Excel(name = "操作时间",format="yyyy-MM-dd HH:mm:ss",width = 20)
        private Date time;
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public int getSex() {
            return sex;
        }
    
        public void setSex(int sex) {
            this.sex = sex;
        }
    
        public Date getTime() {
            return time;
        }
    
        public void setTime(Date time) {
            this.time = time;
        }
    
        @Override
        public String toString() {
            return "UserEntity{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", email='" + email + '\'' +
                    ", age=" + age +
                    ", sex=" + sex +
                    ", time=" + time +
                    '}';
        }
    }
    

    注解说明:

    4、测试

    1、测试导出单个sheet

        /**
         * 测试单sheet导出
         * @throws IOException
         */
        public static void testExportExcel() throws IOException {
            List<UserEntity> list = new ArrayList<>();
            int i = 0;
            while (i < 10){
                UserEntity user = new UserEntity();
                user.setId(i+1);
                user.setAge(20+i);
                user.setEmail("abc@163.com");
                user.setName("张三"+i);
                user.setSex(i%2==0?1:2);
                user.setTime(new Date());
                list.add(user);
                i++;
            }
            EasyPoiUtils.exportExcel(UserEntity.class,list,"src/main/resources/excel/","user.xls");
        }
    

    导出效果

    2、测试导出多个sheet

     /**
         * 测试多sheet导出
         * @throws IOException
         */
        public static void testExportExcels() throws IOException {
            List<Map<String, Object>> list = new ArrayList<>();
            for(int n=1;n<4;n++){
                ExportParams exportParams = new ExportParams("用户信息"