EasyPoi系列之框架集成及基础使用
EasyPoi系列之框架集成及基础使用
- 1 EasyPoi
- 1.1 gitee仓库地址
- 2 EasyPoi集成至SpringBoot
- 2.1 maven引入jar包
- 3 EasyPoi Excel导出
- 3.1 基于实体对象导出
- 3.1.1 @Excel 注解
- 3.1.2 编写实体
- 3.1.3 编写导出方法
- 3.1.4 导出效果
- 3.2 基于模板导出
- 3.2.1 编写模板文件
- 3.2.2 编写导出方法
- 3.2.3 导出效果
- 4 EasyPoi 数据导入
- 4.1 导入数据准备
- 4.2 编写接口
- 4.3 导入测试
- 4.3.1 编写apiFox
- 4.3.2 导入结果
- 4.3.3 修改 "核定金额" 为“核定金额1”
- 5 EasyPoi 模板填充
- 5.1 模板表达式
- 5.2 Excel模板填充导出
- 5.3 Word模板填充导出
- 5.3.1 模板准备
- 5.3.2 编写导出代码
- 5.3.3 导出效果
- 6 总结
1 EasyPoi
官方介绍为:EasyPoi Excel和 Word简易工具类
easypoi功能如同名字easy,主打的功能就是容易,让一个没见接触过poi的人员 就可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,通过简单的注解和模板 语言(熟悉的表达式语法),完成以前复杂的写法。
1.1 gitee仓库地址
https://gitee.com/wupaas/easypoi
2 EasyPoi集成至SpringBoot
2.1 maven引入jar包
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.4.0</version>
</dependency>
3 EasyPoi Excel导出
3.1 基于实体对象导出
3.1.1 @Excel 注解
代码位于:cn.afterturn.easypoi.excel.annotation.Excel,用于通过注解标识实体字段,核心属性如下:
- name:用于表示Excel表头
- width:导出时在excel中每个列的宽 单位为字符,一个汉字=2个字符
- suffix:文字后缀,如% 90 变成90%
- isWrap:是否换行 即支持\n
- type:导出类型 1 是文本 2 是图片,3 是函数,10 是数字, 11 特殊符号 默认是文本
- enumExportField:枚举导出使用的字段
3.1.2 编写实体
import cn.afterturn.easypoi.excel.annotation.Excel;
import lombok.Data;
/**
* 导出实体
*/
@Data
public class ExportEntity {
@Excel(name = "序号",width = 20)
private Integer index;
@Excel(name = "资金性质",width = 50)
private String accountType;
@Excel(name = "项目名称",width = 50)
private String projectName;
@Excel(name = "申请金额",width = 50)
private String amountApplied;
@Excel(name = "核定金额",width = 50)
private String approvedAmount;
}
3.1.3 编写导出方法
/**
* 构建到处数据(模拟数据库查询)
* @param num
* @return
*/
private List<ExportEntity> buildExportData(int num){
List<ExportEntity> exportData = new ArrayList<>();
ExportEntity exportEntity = null;
for (int i = 0; i < num; i++) {
exportEntity = new ExportEntity();
exportEntity.setIndex(i+1);
exportEntity.setAccountType("资金性质"+i);
exportEntity.setProjectName("项目名称"+i);
exportEntity.setAmountApplied("申请金额"+i);
exportEntity.setApprovedAmount("核定金额"+i);
exportData.add(exportEntity);
}
return exportData;
}
/**
* 通过实体导出Excel
* @param response
*/
@RequestMapping("/excel/exportByEntity")
public void exportByEntity(HttpServletResponse response) throws Exception {
ExportParams exportParams = new ExportParams();
List<ExportEntity> datas = this.buildExportData(10);
Workbook workbook = null;
try{
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("测试exportByEntity", "utf-8") + ".xlsx");
workbook = ExcelExportUtil.exportExcel(exportParams, ExportEntity.class, datas);
workbook.write(response.getOutputStream());
}finally {
IoUtil.close(workbook);
}
}
3.1.4 导出效果
浏览器访问 http://ip:port/excel/exportByEntity(根据实际情况进行替换),导出文件效果如下:
3.2 基于模板导出
3.2.1 编写模板文件
具体模板语法在后续会进行讲解
3.2.2 编写导出方法
/**
* 构建到处数据(模拟数据库查询)
* @param num
* @return
*/
private List<ExportEntity> buildExportData(int num){
List<ExportEntity> exportData = new ArrayList<>();
ExportEntity exportEntity = null;
for (int i = 0; i < num; i++) {
exportEntity = new ExportEntity();
exportEntity.setIndex(i+1);
exportEntity.setAccountType("资金性质"+i);
exportEntity.setProjectName("项目名称"+i);
exportEntity.setAmountApplied("申请金额"+i);
exportEntity.setApprovedAmount("核定金额"+i);
exportData.add(exportEntity);
}
return exportData;
}
/**
* 通过模板导出
* @param response
*/
@RequestMapping("/excel/exportTemplate")
public void exportByTemplate(HttpServletResponse response) throws Exception{
//exportTemplate.xlsx 放在resources目录下
TemplateExportParams templateExportParams = new TemplateExportParams("templates/exportTemplate.xlsx");
//定义map对象
Map<String,Object> data = new HashMap<>();
List<ExportEntity> datas = this.buildExportData(10);
//将导出数据put至map中,注意此处的datas与模板$fe: 后的变量名称一致
data.put("datas",datas);
Workbook workbook = null;
try{
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("测试exportTemplate", "utf-8") + ".xlsx");
workbook = ExcelExportUtil.exportExcel(templateExportParams, data);
workbook.write(response.getOutputStream());
}finally {
IoUtil.close(workbook);
}
}
3.2.3 导出效果
浏览器访问 http://ip:port/excel/exportByEntity(根据实际情况进行替换),导出文件效果如下:
注意:通过模板导出,可以用于针对复杂表头,使用@Excel注解无法或不好实现场景。
4 EasyPoi 数据导入
4.1 导入数据准备
以3.1中导出的Excel为例,如下图:
4.2 编写接口
ExportEntity为3.1中导出文件时,定义的实体类
/**
* excel导入
* @param response
*/
@PostMapping("/excel/importData")
@ResponseBody
public List<ExportEntity> importData(HttpServletResponse response, MultipartFile file) throws Exception{
ImportParams importParams = new ImportParams();
List<ExportEntity> datas = ExcelImportUtil.importExcel(file.getInputStream(), ExportEntity.class, importParams);
return datas;
}
4.3 导入测试
4.3.1 编写apiFox
在ApiFox中新建请求如下图,选择对应的导入文件,点击发送
4.3.2 导入结果
如下图,通过图中可看出,数据已从Excel中读入到实体集合中
4.3.3 修改 “核定金额” 为“核定金额1”
将“核定金额” 修改为 “核定金额1”,如下图:
再次进行导入,可以看到,由于字段不匹配,未能成功导入:
注意:通过Excel进行导入时候,1、需要保证导入的实体注解上的内容要与Excel表头文本一致(前后空格会自动进行处理),2、如果存在多列相同的表头,会以最后一列为匹配数据进行导入。
5 EasyPoi 模板填充
5.1 模板表达式
- 空格分割
- 三目运算 {{test ? obj:obj2}}
- n: 表示 这个cell是数值类型 {{n:}}
- le: 代表长度{{le:()}} 在if/else 运用{{le:() > 8 ? obj1 : obj2}}
- fd: 格式化时间 {{fd:(obj;yyyy-MM-dd)}}
- fn: 格式化数字 {{fn:(obj;###.00)}}
- fe: 遍历数据,创建row
- !fe: 遍历数据不创建row
- $fe: 下移插入,把当前行,下面的行全部下移.size()行,然后插入
- !if: 删除当前列 {{!if:(test)}}
- 单引号表示常量值 ‘’ 比如’1’ 那么输出的就是 1
- &NULL& 控制
- ]] 换行符
5.2 Excel模板填充导出
excel模板填充,可以参考3.2 基于模板导出
5.3 Word模板填充导出
5.3.1 模板准备
编写 纳税信息.docx,放置在resources/templates目录下,内容如下:
5.3.2 编写导出代码
/**
* word 测试
*/
@Controller
@RequestMapping("/word")
public class WordTestController {
/**
* 通过模板填充word文档
* @param response
*/
@RequestMapping("/exportByTemplate")
public void exportByTemplate(HttpServletResponse response){
try {
//模拟从数据库读取数据
List<Map<String,String>> taxlist = new ArrayList<>();
Map<String,String> tax1= new HashMap<>();
tax1.put("type","税种1");
tax1.put("presum","1");
tax1.put("thissum","2");
tax1.put("curmonth","1");
tax1.put("now","2025-02-18");
taxlist.add(tax1);
Map<String,String> tax2= new HashMap<>();
tax2.put("type","税种2");
tax2.put("presum","3");
tax2.put("thissum","4");
tax2.put("curmonth","2");
tax2.put("now","2025-02-19");
taxlist.add(tax2);
Map<String,Object> data = new HashMap<>();
data.put("taxlist",taxlist);
Map<String,String> total= new HashMap<>();
total.put("totalpreyear","4");
total.put("totalthisyear","5");
data.put("total",total);
data.put("totalpreyear","测试3");
data.put("totalthisyear","测试4");
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("测试exportByTemplate", "utf-8") + ".docx");
//传入模板地址与模板变量进行填充
try(XWPFDocument head = WordExportUtil.exportWord07("templates/纳税信息.docx",data )){
head.write(response.getOutputStream());
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
5.3.3 导出效果
浏览器访问 http://ip:port/word/exportByTemplate(根据实际情况进行替换),导出文件效果如下:
注意:通过模板导出,可以用于订单填充、合同数据填充等应用场景,能够较好的保留原格式;结合笔者文章 “kkFileView二开之word转pdf接口”,可以快速实现合同文件生成的需求
6 总结
工欲善其事必先利其器。在日常工作中,需要多了解相关的三方组件,以便于在将来遇到类似的需求的时候,能够尽快的提出对应的解决方案,以展现自己的专业能力。