easyPoi报表制作工具,其操作简单,容易上手,不同于POI报表导出那么繁琐,需要自己写相应的工具类。easypoi导出分为3部分,基础导出,模板导出,HTML导出,先总结基础导出。
基础导出我们可以导出图片,实体属性含有实体或者是集合的情况;先看一个例子
导出第一步,添加maven的依赖
<!--easypoi 导出excel依赖-->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.2.0</version>
</dependency>
第二步 为实体类添加注解
package com.xash.quartzDemo.entity;
import java.util.List;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelCollection;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
@ExcelTarget("sysPERMISSION")
@Data
public class SysPermission {
@Excel(name="主键id",needMerge = true)
private int id;
@Excel(name="权限",orderNum="4",needMerge = true)
private String permission;
@Excel(name="描述",needMerge = true)
private String description;
@Excel(name="角色关联id",needMerge = true)
private int rid;
@Excel(name="是否有效",needMerge = true)
private int available;
@ExcelCollection(name="用户角色")
private List<SysRoles> role;
}
第三步 可以导出excel了,具体代码如下
@GetMapping("export1")
public String export1(HttpServletRequest request,HttpServletResponse response) {
List<SysPermission> list=permissionService.selectPermission();
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("权限信息","权限"),
SysPermission .class, list);
if(workbook==null) {
return "fail";
}
String excelName="ExportExcel";
// 重置响应对象
response.reset();
// 当前日期,用于导出文件名称
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String dateStr = "["+excelName+"-"+sdf.format(new Date())+"]";
// 指定下载的文件名--设置响应头
response.setHeader("Content-Disposition", "attachment;filename=" +dateStr+".xls");
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// 写出数据输出流到页面
try {
OutputStream output = response.getOutputStream();
BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
workbook.write(bufferedOutPut);
bufferedOutPut.flush();
bufferedOutPut.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
return "index4";
}
注:我这里用的是get请求,后端回报错,但是导出报表正常,要用post请求,就好了
具体注解含义:如下
@ExcelTarget
限定一个到处实体的注解,以及一些通用设置,作用于最外面的实体
@ExcelEntity
标记是不是导出excel 标记为实体类,一遍是一个内部属性类,标记是否继续穿透,可以自定义内部id
@ExcelCollection
一对多的集合注解,用以标记集合是否被数据以及集合的整体排序
@ExcelIgnore
忽略这个属性,多使用需循环引用中
现在介绍一个@ExcelCollection注解的用法,demo如下
package com.xash.quartzDemo.entity;
import java.util.List;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelCollection;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
@ExcelTarget("sysPERMISSION")
@Data
public class SysPermission {
@Excel(name="主键id",needMerge = true)
private int id;
@Excel(name="权限",orderNum="4",needMerge = true)
private String permission;
@Excel(name="描述",needMerge = true)
private String description;
@Excel(name="角色关联id",needMerge = true)
private int rid;
@Excel(name="是否有效",needMerge = true)
private int available;
@ExcelCollection(name="用户角色")
private List<SysRoles> role;
}
package com.xash.quartzDemo.entity;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import lombok.Data;
@Data
@ExcelTarget("角色信息")
public class SysRoles {
@Excel(name="id")
private int id;
@Excel(name="角色")
private String role;
@Excel(name="描述")
private String description;
@Excel(name="父id")
private int pid;
@Excel(name="是否有效")
private int available;
}
具体导出方法:
/**
* excel导出功能
*/
@GetMapping("export")
//@ResponseBody
public String exportPerssionTable(HttpServletRequest request,HttpServletResponse response) {
String rid=request.getParameter("rid");
List<SysPermission> list=permissionService.selectPermission();
List<SysPermission>list1=new ArrayList<>();
for(SysPermission permission:list) {
List <SysRoles> rolelist=new ArrayList<>();
for(int i=0;i<3;i++) {
SysRoles role = new SysRoles();
role.setId(i);
role.setAvailable(1);
role.setDescription("管理员");
role.setPid(0);
role.setRole("系统管理员");
rolelist.add(role);
}
permission.setRole(rolelist);
list1.add(permission);
}
// Workbook workbook1=new ExportSheet().exportSheets(rid, list1);
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("2412312", "测试", "测试"),
SysPermission.class, list1);
if(workbook==null) {
return "fail";
}
String excelName="测试ExportExcel";
// 重置响应对象
response.reset();
// 当前日期,用于导出文件名称
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String dateStr = "["+excelName+"-"+sdf.format(new Date())+"]";
// 指定下载的文件名--设置响应头
response.setHeader("Content-Disposition", "attachment;filename=" +dateStr+".xls");
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// 写出数据输出流到页面
try {
OutputStream output = response.getOutputStream();
BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
workbook.write(bufferedOutPut);
bufferedOutPut.flush();
bufferedOutPut.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
return "success";
}
这样就可以了,输入相应的url就可以访问并下载了
接下来,可以很轻松的导出图片,具体操作如下
创建一个实体类
package com.xash.quartzDemo.entity;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import lombok.Data;
@ExcelTarget("PicEntity")
@Data
public class PicEntity {
@Excel(name="风景")
private String name;
@Excel(name="地址")
private String address;
@Excel(name="效果图" ,type=2,width=40,height=20,imageType=2)
private Byte[] picCode;
@Excel(name="效果图" ,type=2,width=40,height=20,imageType=1)
private String picSrc;
public PicEntity(String name, String address, String picSrc) {
super();
this.name = name;
this.address = address;
this.picSrc = picSrc;
}
public PicEntity(String name, String address, Byte[] picCode) {
super();
this.name = name;
this.address = address;
this.picCode = picCode;
}
}
准备图片资源,放到自己指定的路径D:/google/3.jpg
开始导出
@GetMapping("exportpic")
public String exportpic(HttpServletResponse response) {
List<PicEntity> list=new ArrayList<>();
list.add(new PicEntity("海阔天空", "海南岛", "D:/google/3.jpg"));
list.add(new PicEntity("一页扁舟", "江苏", "D:/google/2.jpg"));
list.add(new PicEntity("十里桃花", "黄山", "D:/google/1.jpg"));
ExportParams params=new ExportParams("风景山水画","欣赏");
params.setStyle(ExcelStyleUtil.class);
Workbook workbook=ExcelExportUtil.exportExcel(params,PicEntity.class, list);
if(workbook==null) {
return "fail";
}
ExcelExportStylerBorderImpl style =new ExcelExportStylerBorderImpl(workbook);
style.getTitleStyle((short)200);
style.getHeaderStyle((short)200);
String excelName="ExportExcel";
// 重置响应对象
response.reset();
// 当前日期,用于导出文件名称
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String dateStr = "["+excelName+"-"+sdf.format(new Date())+"]";
// 指定下载的文件名--设置响应头
response.setHeader("Content-Disposition", "attachment;filename=" +dateStr+".xls");
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// 写出数据输出流到页面
try {
OutputStream output = response.getOutputStream();
BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
workbook.write(bufferedOutPut);
bufferedOutPut.flush();
bufferedOutPut.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
return "index4";
}
type =2 该字段类型为图片,imageType=1 (默认可以不填),表示从file读取,字段类型是个字符串类型 可以用相对路径也可以用绝对路径,绝对路径优先依次获取
image 类型的cell最好设置好宽和高,会百分百缩放到cell那么大,不是原尺寸,这里注意下
具体连接,请看官网:http://easypoi.mydoc.io/