EasyPoi导入导出(一)

EasyPoi导入导出(一),第1张

EasyPoi导入导出(一)

EasyPoi是一个文件导入导出的工具插件,官网:http://doc.wupaas.com/docs/easypoi/easypoi-1c0u4mo8p4ro8

一、EasyPoi简单应用:导出excel

1.1 创建一个普通的maven项目(springboot项目也可以),然后在pom中导入相关依赖

    
      cn.afterturn
      easypoi-base
      4.1.0
    
    
      cn.afterturn
      easypoi-web
      4.1.0
    
    
      cn.afterturn
      easypoi-annotation
      4.1.0
    
    
      org.projectlombok
      lombok
      1.18.20
    

1.2.创建实体类

@Data
@ExcelTarget("user")
public class User implements Serializable {
    @Excel(name="用户编号")
    private String id;
    @Excel(name="用户名称")
    private String name;
    @Excel(name="用户年龄")
    private Integer age;
   
}

要使用easypoi导出excel,实体类要实现Serializable(序列化),此处简单解释一下这几个注解:

@ExcelTarget 这个是作用于最外层的对象,描述这个对象的id,以便支持一个对象可以针对不同导出做出不同处理@Data注解是lombok注解,快速创建get,set等方法的注解@Excel 作用到filed上面,是对Excel一列的一个描述

 1.3创建一个导出测试类TestEasyPoi(名字可以随便起)

在这个测试类中测试导出excel

public class TestEasyPoi {
    //创建一个list,用于模仿从数据库中查询出来的数据
    public static List userList(){
        //存放数据的list集合
        List users = new ArrayList<>();
        //构建虚拟数据
        for (int i = 0; i < 5; i++) {
            User user = new User();
            user.setId(String.valueOf(i));
            user.setName("赵云"+i);
            user.setAge(i);
            users.add(user);
        }
        return users;
    }
    @Test
    public void testExport() throws IOException {
        //获取数据
        List list = userList();
        //将数据导出到Excel
        //参数1:ExportParams对象 参数2:导出的类型  参数3:导出的数据集合
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("用户信息表", "用户excel表"), User.class, list);
        //使用流将excel写入到指定的位置
        FileOutputStream outputStream = new FileOutputStream("C:/Users/1/Desktop/easyPoi/easyExcel.xls");//指定写出的位置
        workbook.write(outputStream);//将数据输出
        //关闭流
        workbook.close();
        outputStream.close();
    }
}
new ExportParams("用户信息表", "用户excel表")中的两个参数对应的excel表中的位置如图所示:

最后运行测试,效果如图所示:

 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zaji/5719057.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存