easypoi 操作 excel 表格,这也太 easy 了吧

what is easypoi?

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

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

怎使用easypoi

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

        <dependency>
            <groupId>cn.afterturngroupId>
            <artifactId>easypoi-baseartifactId>
            <version>4.1.0version>
        dependency>
        <dependency>
            <groupId>cn.afterturngroupId>
            <artifactId>easypoi-webartifactId>
            <version>4.1.0version>
        dependency>
        <dependency>
            <groupId>cn.afterturngroupId>
            <artifactId>easypoi-annotationartifactId>
            <version>4.1.0version>
        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 
     * @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 +
                '}';
    }
}

注解说明:

easypoi 操作 excel 表格,这也太 easy 了吧_第1张图片

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("[email protected]");
            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");
    }

导出效果

easypoi 操作 excel 表格,这也太 easy 了吧_第2张图片

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("用户信息"+n,"用户信息"+n);
            Object entity = UserEntity.class;
            List<UserEntity> data = new ArrayList<>();
            int i = 0;
            while (i < 10){
                UserEntity user = new UserEntity();
                user.setId(i*n+1);
                user.setAge(20+i);
                user.setEmail("[email protected]");
                user.setName("张三"+i*n);
                user.setSex(i%2==0?1:2);
                user.setTime(new Date());
                data.add(user);
                i++;
            }
            // 构建map
            Map<String,Object> map = new HashMap<>();
            map.put("title",exportParams);
            map.put("entity",entity);
            map.put("data",data);
            list.add(map);
        }
        EasyPoiUtils.exportExcel(list,"src/main/resources/excel/","user1.xls");
    }

导出效果:

easypoi 操作 excel 表格,这也太 easy 了吧_第3张图片

3、测试导入excel

    /**
     * 测试导入
     */
    public static void testImportExcel(){
        List<UserEntity> list = EasyPoiUtils.importExcel(

                new File("src/main/resources/excel/user.xls"),
                UserEntity.class, new ImportParams());
        list.forEach((user)->{
            System.out.println(user);
        });

    }

导入时候能够直接帮我们转化为实体类,这功能还是非常赞的。我们将结果输出到控制台,输出信息如下:

easypoi 操作 excel 表格,这也太 easy 了吧_第4张图片

以上就是easypoiexcel表格的简单操作,当然easypoi的功能远不止这么多,easypoi的功能还是非常强大的,有兴趣的朋友可以去官方学习。

扫码关注公众号(搜索公众号:平头哥的技术博文)一起交流学习呗

你可能感兴趣的:(Java)