SpringBoot+EasyPOI word模板导出,含多张图片
这几天客户提出了新的需求,要求记录要能够导出word,并且里面包含的图片也要导出来,这里借用EasyPOI来进行操作。
参考文章:https://blog.csdn.net/qq_34752942/article/details/112203419
1、引入相关Jar包
引入EasyPOI必要的Jar包,这里就不再多说,不知道引哪些的话请自行百度。
注意:建议使用EasyPOI 4.3.0+ 版本的,因为word图片循环是4.3.0以后的版本才支持的。切记!!!
2、后台代码
2.1、导出对象类
这里简单改造一个项目中使用的对象。关于照片的两个字段说明一下,这里是项目中图片上传到服务器之后,把图片在服务器的存储路径保存到数据库中,如果上传了多张图片,那么把就用逗号把路径拼接成一个字符串放入数据库。
public class ProblemRectify {/*** 标段名*/private String bidName; /*** 问题描述*/private String description;/*** 问题照片,路径字符串,以逗号分隔*/private String pictures;/*** 问题发现时间*/private LocalDate checkDate;/*** 问题整改时限*/private LocalDate rectifyDate;/*** 整改方案*/private String rectifyPlan;/*** 整改结果*/private String rectifyResult;/*** 整改后照片,路径字符串,以逗号分隔*/private String rectifyPictures;/*** 整改完成时间*/private LocalDate rectifyCompleteDate;/*** 整改状态*/private String rectifyStatus;/*** 问题责任人名字*/private String liablePersonName;/*** 问题发现人名字*/private String findPersonName;/*** 问题类型*/private String typeName;/*** 问题来源*/private String sourceName;/*** 问题分类代码*/private String sortName;/*** 问题级别代码*/private String levelName;/*** 责任单位*/private String proOrganizationName;/*** 所在桩号*/private String pileNum;}
2.2、Word模板
要用EasyPOI的word模板导出,那么就要事先定义好word模板,并放入项目的指定位置。这里建议在resources下新建一个目录,我这里建的目录名是exportTemplate,大家可以根据实际需要自行创建。
Word模板如下图所示:
注意:关于EasyPOI模板指令的介绍,请参考EasyPOI的官网。仅说一下我在写word模板时遇到的问题:写表达式 {{属性名}} 时,不要有空格,然后就是在中文状态下输入{{和}},因为我切换到英文输入法输入时,表达式的值没法被值替换掉。
2.3、核心代码
Map<String,Object> params 是word模板导出时,用来装要导出的数据。需要注意的是,构造Map时,注意字段值的判空。由于文件是先按模板生成再下载,所以会生成一个临时的文件在指定位置,如果不需要的话,那就在文件下载之后将其删除。
void testExportData() {// 从数据库根据id获取数据,这里id我用了一个写死的,具体项目中,id请通过前端传入ProblemRectify rectifyInfo = problemRectifyService.getCheckRectifyInfo(131L);// 构造导出数据Map<String, Object> exportParams = new HashMap<>();exportParams.put("description", rectifyInfo.getDescription());exportParams.put("bidName", rectifyInfo.getBidName());exportParams.put("pileNum", rectifyInfo.getPileNum());exportParams.put("sourceName",rectifyInfo.getSourceName());exportParams.put("levelName",rectifyInfo.getLevelName());exportParams.put("typeName",rectifyInfo.getTypeName());exportParams.put("sortName",rectifyInfo.getSortName());DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");exportParams.put("checkDate",rectifyInfo.getCheckDate().format(dateTimeFormatter));exportParams.put("rectifyDate",rectifyInfo.getRectifyDate().format(dateTimeFormatter));exportParams.put("proOrganizationName",rectifyInfo.getProOrganizationName()));exportParams.put("findPersonName",rectifyInfo.getFindPersonName());exportParams.put("liablePersonName",rectifyInfo.getLiablePersonName());exportParams.put("rectifyStatus",rectifyInfo.getRectifyStatus());exportParams.put("rectifyCompleteDate",rectifyInfo.getRectifyCompleteDate().format(dateTimeFormatter));exportParams.put("rectifyPlan", StringUtils.isNotBlank(rectifyInfo.getRectifyPlan()) ? checkRectifyInfo.getRectifyPlan() : "无整改方案");exportParams.put("rectifyResult",StringUtils.isNotBlank(checkRectifyInfo.getRectifyResult()) ? rectifyInfo.getRectifyResult() : "无整改结果");// 整改前照片String picturePathStr = rectifyInfo.getPictures();List<ImageEntity> pictureImageList = initImageData(picturePathStr);exportParams.put("pictures",pictureImageList);// 整改后照片String rectifyPicturesPathStr = checkRectifyInfo.getRectifyPictures();List<ImageEntity> rectifyImageList = initImageData(rectifyPicturesPathStr);exportParams.put("rectifyImages",rectifyImageList);this.exportWord("exportTemplate/problemExportTemplate.docx","问题详情.docx",exportParams);System.out.println("文件导出成功!");}/***** 导出word数据* @param templatePath* @param fileName* @param params*/private void exportWord(String templatePath, String fileName, Map<String, Object> params) {Assert.notNull(templatePath, "模板路径不能为空!");Assert.notNull(fileName, "文件名称不能为空!");Assert.isTrue(fileName.endsWith(".docx"), "word导出请使用docx格式!");try {XWPFDocument doc = WordExportUtil.exportWord07(templatePath, params);String tempPath = fileName;FileOutputStream fos = new FileOutputStream(tempPath);doc.write(fos);// 设置强制下载不打开response.setContentType("application/force-download");// 设置文件名response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName,"UTF-8"));OutputStream out = response.getOutputStream();doc.write(out);out.close();fos.close();} catch (Exception e) {e.printStackTrace();} finally {delFileWord(tempPath,fileName);}}private List<ImageEntity> initImageData(String fileUrl) {List<ImageEntity> result = new ArrayList<>();if (StringUtils.isNotBlank(fileUrl)) {if (fileUrl.indexOf(",") > 0) {String[] fileUrlArr = fileUrl.split(",");for (int i = 0 ; i < fileUrlArr.length ; i++) {byte[] imageData = FDSUtils.downloadFile(fileUrlArr[i]);ImageEntity item = new ImageEntity();item.setWidth(300);item.setHeight(300);item.setData(imageData);item.setType(ImageEntity.Data);result.add(item);}} else {byte[] imageData = FDSUtils.downloadFile(fileUrl);ImageEntity item = new ImageEntity();item.setWidth(300);item.setHeight(300);item.setData(imageData);item.setType(ImageEntity.Data);result.add(item);}} else {ImageEntity item = new ImageEntity();item.setWidth(300);item.setHeight(300);item.setData(null);item.setType(ImageEntity.Data);result.add(item);}return result;}/**** 删除临时文件* @param filePath* @param fileName*/private static void delFileWord(String filePath, String fileName){File file =new File(filePath+fileName);File file1 =new File(filePath);file.delete();file1.delete();}
注意:这里代码需要注意的两个地方,一是模板路径,二是图片的设置,我这里是通过图片路径获取图片的byte[ ]数据,也就是代码中的fastDFSUtils.downloadFile(fileUrl)。因为项目中使用的fastDFS进行文件的上传和存储,写的有这个工具类。
但是,如果不想通过这种方法,也可以直接使用图片Url的方式,利用ImageEntity下的setUrl方法设置图片的存储的路径,然后通过setType将类型设置为ImageEntity.Url,这种方法我没有测试,大家可以测试一下。
SpringBoot+EasyPOI word模板导出,含多张图片相关推荐
- SpringBoot集成文件 - 如何基于POI-tl和word模板导出庞大的Word文件?
前文我们介绍了通过Apache POI通过来导出word的例子:那如果是word模板方式,有没有开源库通过模板方式导出word呢?poi-tl是一个基于Apache POI的Word模板引擎,也是一个 ...
- easypoi根据模板导出word
easypoi根据模板导出word 在工作中,模板导出必不或缺,实现模板导出的第三方工具包也有很多.例如:poi.freemaker.hutool的word工具类以及第三方报表工具.这里我简单介绍一下 ...
- 8、jeecg 笔记之 自定义word 模板导出(一)
8.jeecg 笔记之 自定义word 模板导出(一) 1.前言 jeecg 中已经自带 word 的导出导出功能,其所使用的也是 easypoi,尽管所导出的 word 能满足大部分需求, 但总是有 ...
- poi-tl,根据word模板导出word(表格行循环,表格无表头的情况)
最近项目里要做一个根据客户提供的word模板导出word的功能,方法有很多,比如easyPoi(对word的支持并不是很好),freeMark(太麻烦不想研究),以及poi-tl, 最后研究了半天发现 ...
- 【springboot+poi+poi-tl 模板导出wrod (包含图片集合、页眉、页脚)】
springboot+poi+poi-tl 模板导出wrod 1. maven依赖 <dependency><groupId>org.apache.poi</groupI ...
- Net Core DocXCore 实现word模板导出
实际工作中,往往有这样的需求,需要导出word,还有各种各样的样式,于是有了word模板导出. 实现以下几个需求: 1.表单导出 2.表格导出 3.表单表格混合导出 4.实际用例测试 解决方案: 实现 ...
- Easypoi使用模板导出文档或excel表格详解
Easypoi使用模板导出docx文档或excel表格详解 **doc或docx文档的模板导出** **Excel的模板导出** 话不多说先上依赖 <dependency><grou ...
- EasyPOI 根据模板导出excel时,无法自适应行高得解决方案记录
目录 EasyPOI 根据模板导出excel时,无法自适应行高得解决方案记录 首先说一下问题得场景 第一步 第二步 第三步 第四步 第五步 第六步 第七步 (就是这个方法!) EasyPOI 根据模板 ...
- 在docxtemplater 和 open-docxtemplater-image-module按word模板导出图片 遇到个坑记录下
在docxtemplater 和 open-docxtemplater-image-module按word模板导出图片 遇到个坑记录下 主要是一直提示 Cannot read property 'pa ...
最新文章
- 一行代码都不写!Github博客小白版入门教程
- ubuntu pip 安装
- 以后开药的时候,最后一定问一句,哪些药有激素
- Oracle中的pfile和spfile详解
- Entity Framework part2
- How to enable product text HTML editor
- 【LeetCode笔记】剑指 Offer 16. 数值的整数次方(Java、分治)
- 建立数字化、学习型人事平台,HR 与业务终于不再「隔空对话」
- python3 通过百度地图API获取城市POI点并存于CSV格式
- Leecode刷题热题HOT100(7)—— 整数反转
- python object类_Python中一切皆对象,这个对象究竟是什么?
- python模拟登录人人
- linux 服务编程,Linux高性能服务编程(I/O复用)
- idea设置Java版本
- vdbench 参数详解
- 永久关闭“WPS热点”的显示_我是亲民_新浪博客
- 室内红外线防盗报警器matlab,红外防盗报警系统毕业设计论文.doc
- 中国农业银行研发中心2021实习生笔试题
- Java并发包-java.util.concurrent详解
- 我喜欢用计算机300,我最喜欢的游戏作文300字(精选10篇)
热门文章
- [电脑问题]新固态硬盘安装系统以及分区,reboot and select proper boot device的问题处理
- manjaro 安装的艰辛历程,常用软件安装以及踩坑
- 安卓11 Sdcard文件读取权限问题
- 清晰地记录着这河水改道的历史
- bzoj 2096 [POI2004]ZAW——二进制枚举
- Keras Input Layer
- selenium+python 的微博自动转赞评功能实现
- 解决在MAC上输入法切换慢的问题
- 2020.5.31 牛客“科林明伦杯” A.点对最大值【树形dp】
- java将uuid转换成大写,python生成大写32位uuid代码