028-86261949

当前位置:首页 > 技术交流 > Java技术:easypoi

Java技术:easypoi

2019/01/15 09:50 分类: 技术交流 浏览:100

1.导出excel-helloword
1.1导入相应jar包

  1. <dependency>
  2.   <groupId>cn.afterturn</groupId>
  3.   <artifactId>easypoi-base</artifactId>
  4.   <version>3.2.0</version>
  5. </dependency>
  6. <dependency>
  7.   <groupId>cn.afterturn</groupId>
  8.   <artifactId>easypoi-web</artifactId>
  9.   <version>3.2.0</version>
  10. </dependency>
  11. <dependency>
  12.   <groupId>cn.afterturn</groupId>
  13.   <artifactId>easypoi-annotation</artifactId>
  14.   <version>3.2.0</version>
  15. </dependency>
复制代码

1.2准备domain

  1. public class EasyEmployee {
  2.     /*
  3.     name:列名
  4.     width:列宽
  5.     */
  6.     @Excel(name="邮箱",width = 25)
  7.     private String email;
  8.     @Excel(name="用户名")
  9.     private String username;
  10.     @Excel(name = "年龄")
  11.     private Integer age;
  12.     /*format:时间格式*/
  13.     @Excel(name = "生日",format = "yyyy-MM-dd")
  14.     private Date bornDate = new Date();
  15.    /* replace:值得替换  导出是{a_id,b_id} 导入反过来 就相当于如果值是true则显示男,如果值为false则显示女*/
  16.     @Excel(name="性别",replace = {"男_true","女_false"})
  17.     private Boolean sex;
  18.  
  19.  
  20.     public String getEmail() {
  21.         return email;
  22.     }
  23.  
  24.     public void setEmail(String email) {
  25.         this.email = email;
  26.     }
  27.  
  28.     public String getUsername() {
  29.         return username;
  30.     }
  31.  
  32.     public void setUsername(String username) {
  33.         this.username = username;
  34.     }
  35.  
  36.     public Integer getAge() {
  37.         return age;
  38.     }
  39.  
  40.     public void setAge(Integer age) {
  41.         this.age = age;
  42.     }
  43.  
  44.     public Date getBornDate() {
  45.         return bornDate;
  46.     }
  47.  
  48.     public void setBornDate(Date bornDate) {
  49.         this.bornDate = bornDate;
  50.     }
  51.  
  52.     public Boolean getSex() {
  53.         return sex;
  54.     }
  55.  
  56.     public void setSex(Boolean sex) {
  57.         this.sex = sex;
  58.     }
  59. }
复制代码

1.3测试

  1. @Test
  2. public void testExport() throws Exception {
  3.     List<EasyEmployee> employees = new ArrayList<>();
  4.     EasyEmployee e1 = new EasyEmployee();
  5.     e1.setUsername("李四");
  6.     e1.setEmail("12@qq.com");
  7.     e1.setAge(22);
  8.     e1.setBornDate(new Date());
  9.     e1.setSex(true);
  10.  
  11.  
  12.     EasyEmployee e2 = new EasyEmployee();
  13.     e2.setUsername("王耍耍");
  14.     e2.setAge(33);
  15.     e2.setBornDate(new Date());
  16.     e2.setSex(false);
  17.     e2.setEmail("22@qq.com");;
  18.     employees.add(e1);
  19.     employees.add(e2);
  20.  
  21.     /*
  22.         第一个参数: easypoi的基本配置(导出excel的基本配置)
  23.         第二个参数: 根据具体类型导出
  24.         第三个参数: 导出的数据
  25.      */
  26.     Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("员工列表信息","员工"),
  27.             EasyEmployee .class, employees);
  28.  
  29.     FileOutputStream fileOutputStream = new   FileOutputStream("employee.xlsx");
  30.     workbook.write(fileOutputStream);
  31.     fileOutputStream.close();
  32. }
复制代码

1.4查看效果

2.导出excel-关联对象与图片2.1准备domain

  1. @ExcelTarget("emp")//相当于给该实体类取一个名字
  2. public class EasyEmployee {
  3.     /*
  4.        name:列名
  5.        width:列宽
  6.    */
  7.     @Excel(name="邮箱",width = 25)
  8.     private String email;
  9.     @Excel(name="用户名")
  10.     private String username;
  11.     @Excel(name = "年龄")
  12.     private Integer age;
  13.     /*format:时间格式*/
  14.     @Excel(name = "生日",format = "yyyy-MM-dd")
  15.     private Date bornDate = new Date();
  16.     /* replace:值得替换  导出是{a_id,b_id} 导入反过来 就相当于如果值是true则显示男,如果值为false则显示女*/
  17.     @Excel(name="性别",replace = {"男_true","女_false"})
  18.     private Boolean sex;
  19.     //头像的url地址    savePath:你导入的图片存放的路径(对导入有效) type:导出类型 1 是文本 2 是图片,3 是函数,10 是数字 默认是文本
  20.     @Excel(name="头像", type = 2 ,height = 40,width = 30,savePath = "imgs/head")
  21.     private String imgUrl;
  22.  
  23.     @ExcelEntity//标记是不是导出excel 标记为实体类,一遍是一个内部属性类,标记是否继续穿透,(简单理解就是我导出关联对象中对应的属性)
  24.     private EasyDept dept;
  25.  
  26.  
  27.     public String getUsername() {
  28.         return username;
  29.     }
  30.  
  31.     public void setUsername(String username) {
  32.         this.username = username;
  33.     }
  34.  
  35.     public String getEmail() {
  36.         return email;
  37.     }
  38.  
  39.     public void setEmail(String email) {
  40.         this.email = email;
  41.     }
  42.  
  43.     public Integer getAge() {
  44.         return age;
  45.     }
  46.  
  47.     public void setAge(Integer age) {
  48.         this.age = age;
  49.     }
  50.  
  51.     public Boolean getSex() {
  52.         return sex;
  53.     }
  54.  
  55.     public void setSex(Boolean sex) {
  56.         this.sex = sex;
  57.     }
  58.  
  59.     public Date getBornDate() {
  60.         return bornDate;
  61.     }
  62.  
  63.     public void setBornDate(Date bornDate) {
  64.         this.bornDate = bornDate;
  65.     }
  66.  
  67.     public EasyDept getDept() {
  68.         return dept;
  69.     }
  70.  
  71.     public void setDept(EasyDept dept) {
  72.         this.dept = dept;
  73.     }
  74.  
  75.     public String getImgUrl() {
  76.         return imgUrl;
  77.     }
  78.  
  79.     public void setImgUrl(String imgUrl) {
  80.         this.imgUrl = imgUrl;
  81.     }
  82.  
  83. }
  84.  
  85. @ExcelTarget("dept")
  86. public class EasyDept {
  87. /*意思是:如果我导出EasyEmployee的时候一并把关联对象EasyDept导出来,那么我EasyDept中最终导出的名字就为部门
  88. 如果我单独只导出EasyDept那我的名字就叫做名字*/
  89.     @Excel(name = "部门_emp,名字_dept")
  90.     private String name;
  91.     @Excel(name = "部门地址_emp,地址_dept")
  92.     private String address;
  93.  
  94.     public String getName() {
  95.         return name;
  96.     }
  97.  
  98.     public void setName(String name) {
  99.         this.name = name;
  100.     }
  101.  
  102.     public String getAddress() {
  103.         return address;
  104.     }
  105.  
  106.     public void setAddress(String address) {
  107.         this.address = address;
  108.     }
  109.  
  110.     @Override
  111.     public String toString() {
  112.         return "EasyDept{" +
  113.                 "name='" + name + '\'' +
  114.                 ", address='" + address + '\'' +
  115.                 '}';
  116.     }
  117. }
复制代码

2.2测试

  1. @Test
  2. public void testExport() throws Exception {
  3.     List<EasyEmployee> employees = new ArrayList<>();
  4.     EasyEmployee e1 = new EasyEmployee();
  5.     e1.setUsername("李四");
  6.     e1.setEmail("12@qq.com");
  7.     e1.setAge(22);
  8.     e1.setBornDate(new Date());
  9.     e1.setSex(true);
  10.     EasyDept dept = new EasyDept();
  11.     dept.setName("开发部");
  12.     dept.setAddress("东莞");
  13.     e1.setDept(dept);
  14.     e1.setImgUrl("imgs/tx.jpg");
  15.  
  16.     EasyEmployee e2 = new EasyEmployee();
  17.     e2.setUsername("王耍耍");
  18.     e2.setAge(33);
  19.     e2.setBornDate(new Date());
  20.     e2.setSex(false);
  21.     e2.setEmail("22@qq.com");
  22.     dept = new EasyDept();
  23.     dept.setName("销售部");
  24.     dept.setAddress("北京");
  25.     e2.setDept(dept);
  26.     e2.setImgUrl("imgs/pp.jpg");
  27.     employees.add(e1);
  28.     employees.add(e2);
  29.  
  30.  
  31.     /*
  32.         第一个参数: easypoi的基本配置(导出excel的基本配置)
  33.         第二个参数: 根据具体类型导出
  34.         第三个参数: 导出的数据
  35.      */
  36.     Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("员工列表信息","员工"),
  37.             EasyEmployee .class, employees);
  38.  
  39.     FileOutputStream fileOutputStream = new FileOutputStream("employee.xlsx");
  40.     workbook.write(fileOutputStream);
  41.     fileOutputStream.close();
  42.  
  43. }
复制代码


3.单独导出部门

  1. @Test
  2. public void testExport1() throws Exception {
  3.     List<EasyDept> depts = new ArrayList<>();
  4.     EasyDept dept = new EasyDept();
  5.     dept.setName("公关部");
  6.     dept.setAddress("东莞");
  7.     depts.add(dept);
  8.     dept = new EasyDept();
  9.     dept.setName("销售部");
  10.     dept.setAddress("北京");
  11.     depts.add(dept);
  12.     Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(),
  13.             EasyDept .class, depts);
  14.  
  15.     FileOutputStream fileOutputStream = new FileOutputStream("dept.xlsx");
  16.     workbook.write(fileOutputStream);
  17.     fileOutputStream.close();
  18. }
复制代码


4.导入功能4.1测试

  1. @Test
  2. public void testImport() throws Exception{
  3.     //导入的基本信息
  4.     ImportParams params = new ImportParams();
  5.     //导入标题占一行
  6.     params.setTitleRows(1);
  7.     //导入头占一行
  8.     params.setHeadRows(1);
  9.     long start = new Date().getTime();
  10.     List<EasyEmployee> list = ExcelImportUtil.importExcel(
  11.             new FileInputStream("employee.xlsx"),//导入指定的文件
  12.             EasyEmployee.class,//导入的数据转为指定的类型
  13.             params);//导入的基本信息的设置
  14.  
  15.     for (EasyEmployee easyEmployee : list) {
  16.         System.out.println(easyEmployee);
  17.     }
  18. }
复制代码

4.2效果




#标签:java