easyPoi官网:http://www.wupaas.com/

一.制作要导出的word模板(使用Word软件制作模板),把模板里的变量用{{字段名}}代替,把制作好的模版放到,如下图我放到static下的template中

模板

导入所需jar包

<!--   easypoi依赖     -->
<dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-base</artifactId><version>3.0.3</version>
</dependency>
<dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-web</artifactId><version>3.0.3</version>
</dependency>
<dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-annotation</artifactId><version>3.0.3</version>
</dependency><!--   模板有循环数据时的依赖     -->
<dependency><groupId>org.apache.poi</groupId><artifactId>ooxml-schemas</artifactId><version>1.1</version>
</dependency><!--   openoffice把word转为pdf依赖-->
<dependency><groupId>org.jodconverter</groupId><artifactId>jodconverter-core</artifactId><version>4.2.2</version>
</dependency>
<dependency><groupId>org.jodconverter</groupId><artifactId>jodconverter-spring-boot-starter</artifactId><version>4.2.2</version>
</dependency>
<dependency><groupId>org.jodconverter</groupId><artifactId>jodconverter-local</artifactId><version>4.2.2</version>
</dependency>

controller层

package com.wfh.controller;import cn.afterturn.easypoi.entity.ImageEntity;
import cn.afterturn.easypoi.word.WordExportUtil;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.jodconverter.DocumentConverter;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;/*** 导出Word* @author Administrator**/
@RequestMapping("/export/")
@RestController
public class ExportWordController {//openoffice所需的@Resourceprivate DocumentConverter converter;/*** 用户信息导出word  --- easypoi* @throws IOException*/@RequestMapping("/exportWord")public void exportUserWord2(HttpServletResponse response){Map<String, Object> params = new HashMap<>();//word模板地址放在src/main/resources/下,因为配置过静态资源映射,所以采用如下方式获取项目中的word模板地址:String templatePath = "/static/template/project.docx";//word模板地址params.put("title","装饰公司");//导出列表Map<String, String> project = new HashMap<>();project.put("name", "沁阳御书院");project.put("name1", "沁阳御书院11楼");project.put("numb", "ss-111");project.put("number", "x50");project.put("type", "55B固转平开窗");project.put("color", "棕色");project.put("num", "11樘");project.put("remark", "沁阳御书院");params.put("project", project);try {//图片按比例缩放URL url = ClassLoader.getSystemResource("static/template/m.png");File picture = new File(url.toURI());BufferedImage sourceImg = ImageIO.read(new FileInputStream(picture));// 渲染图片ImageEntity image = new ImageEntity();int heigthPic = 235;int width = sourceImg.getWidth();int heigth = sourceImg.getHeight();int widthPic = heigthPic * width/heigth;image.setHeight(heigthPic);image.setWidth(widthPic);image.setUrl("static/template/m.png");image.setType(ImageEntity.URL);params.put("img", image);// TODO 渲染其他类型的数据请参考官方文档//导出列表List<Map<String, Object>> jobs = new ArrayList<>();Map<String, Object> job;for (int i = 0; i < 5; i++) {job = new HashMap<>();job.put("code", "ID-" + i);job.put("name", "姓名:" + i);job.put("size", "姓名:" + i);job.put("count", "姓名:" + i);job.put("angle", "姓名:" + i);jobs.add(job);}params.put("jobs", jobs);//导出列表List<Map<String, Object>> ms = new ArrayList<>();Map<String, Object> m;for (int i = 0; i < 5; i++) {m = new HashMap<>();m.put("l", "ID-" + i);m.put("w", "ID-" + i);m.put("s", "ID-" + i);m.put("h", "ID-" + i);m.put("mj", "ID-" + i);m.put("g", "ID-" + i);ms.add(m);}params.put("ms", ms);//导出wordXWPFDocument xwpfDocument = null;xwpfDocument = WordExportUtil.exportWord07(templatePath, params);String tmpPath = "D:\\soft\\image.docx";FileOutputStream fos = new FileOutputStream(tmpPath);xwpfDocument.write(fos);fos.close();//需要转换的文件,word转pdfFile file = new File(tmpPath);//文件转化File fileNew = new File("D:\\soft\\test.pdf");converter.convert(file).to(fileNew).execute();//使用response,将pdf文件以流的方式发送的前端ServletOutputStream outputStream = response.getOutputStream();// 读取文件InputStream in = new FileInputStream(fileNew);// copy文件int i = IOUtils.copy(in, outputStream);System.out.println(i);in.close();outputStream.close();} catch (Exception e) {e.printStackTrace();}}
}

配置文件

jodconverter.local.enabled=true
#openoffice安装目录
jodconverter.local.office-home=F:/Program Files (x86)/OpenOffice 4
jodconverter.local.max-tasks-per-process=10
jodconverter.local.port-numbers=8100

安装openoffice

https://blog.csdn.net/Howinfun/article/details/80759008

windows启动openoffice

https://blog.csdn.net/qq_43561507/article/details/106623176

使用easyPoi导出word文档并使用openoffice把word转换为pdf格式相关推荐

  1. html中只能上传文件word,HTML文件表单,接受Word文档(HTML file form, accept Word documents)...

    HTML文件表单,接受Word文档(HTML file form, accept Word documents) 我在这里遇到了一个奇怪的问题. (不管怎样,或者我只是忽略了一些非常简单的东西.) 我 ...

  2. wps的ppt怎么存html,如何将网页快速转换为WPS与WORD文档 ppt怎么转换成word文档

    导读:小编根据大家的需要整理了一份关于<如何将网页快速转换为WPS与WORD文档 ppt怎么转换成word文档>的内容,具体内容: 看到图文并茂的网页,想把它全部或部分转换为WPS或者WO ...

  3. Word文档怎么删除html标签,word怎么清除所有格式

    在 Word 中,选择性粘贴有三种格式 分别为保留原格式.合并格式和保留原文本,另外,还可以选择"HTML 格式.无格式文本.无格式的 Unicode 文本.带格式文本(RTF)和图片(增强 ...

  4. word计算机桌面加密,word文档加密,怎么让word自动加密 -电脑资料

    Word提供了加密的功能,但不能自动给文档加密, 第一步:在"工具"菜单中选择"宏"选项卡,单击"宏"命令,键入宏的名称"AllA ...

  5. word怎么显示计算机数字,如何使word文档自动显示字数统计 Word自动统计文档字数在哪里...

    如何使word文档自动显示字数统计 Word自动统计文档字数在哪里 Word中有一个非常实用的字数统计功能,如要统计一个文档中的字数,直接在菜单栏中单击"审阅→字数统计",便可得到 ...

  6. java检查word文档内容缺失_恢复Word文档内容需要了解的知识

    Word文档是每一位电脑用户都有机会接触的文字编辑工具,它对我们的生活有着重要影响,不管是在学习还是在工作上.如果我们遇到了Word文档内容丢失或者Word文档内容乱码的情况,应该怎么解决呢?请继续往 ...

  7. Py自动化办公—Word文档替换、Excel表格读取、Pdf文件生成和Email自动邮件发送实战案例...

    点击上方"Python爬虫与数据挖掘",进行关注 回复"书籍"即可获赠Python从入门到进阶共10本电子书 今 日 鸡 汤 平阳歌舞新承宠,帘外春寒赐锦袍. ...

  8. word文档docx密码解开,word文档docx不能复制打印、word文档docx忘记密码怎么办?

    wps解密word文档docx文件,忘记word文档docx密码怎么办?[解密神器]进阶解密word文档docx,用「密码帝」 就可以啦!手机电脑都可以用,一键轻松在线解密!不用下载安装任何软件!

  9. word文档docx解密软件,word文档docx权限密码多少?

    word文档docx在线解开,word文档docx不能复制打印怎么办?[真的很好用]word文档docx解除密码app,安利「密码帝」 无需下载安装软件,百度搜索密码帝就可以了,手机电脑都可以,一键轻 ...

最新文章

  1. java 是怎么在中删除下拉列表_删除在一个下拉列表字段中选择的选项,并且不显示在该行中存在的其他下拉列表中不工作-jquery...
  2. bzoj 1412 [ZJOI2009]狼和羊的故事 最小割建图
  3. 最新发布丨游戏市场“超预期”增长背景下,如何加速产品精品化 运营精细化?
  4. LSTM简介以及数学推导(FULL BPTT)
  5. OpenCV使用F变换过滤
  6. Opencv学习笔记之OpenCV介绍
  7. 中国软件20年,向金山和中国软件英雄致敬!
  8. linux-vim-进入编辑模式的多种方法
  9. COCO 54.7mAP!DetectoRS目标检测:改进主干网,成就新高度!
  10. JavaScript 演练(4). 回调函数
  11. 叠加多个装饰器——day13
  12. java方法前面加上x_@Autowired 写在构造方法上
  13. php读写明华读卡器,rwCard 明华USBIC卡读卡器IC卡读写程序 - 下载 - 搜珍网
  14. 论文阅读之Siamese RPN以及一些其他内容的补充
  15. matlab改变直方图数量级,直方图规定化——Matlab实现及其原理
  16. 双线 路由表 linux,linux双线ip设置(不需额外增加路由表)
  17. 线性分式变换(linear fractional transformation)
  18. 基于单片机心率监测的LED灯辅助睡眠系统设备-毕业设计
  19. AD fanout 各选项说明
  20. (二)QT5.14.2连接MySQL并使用QtableView显示数据表内容

热门文章

  1. html往WPS粘贴格式乱,WPS文档怎么清除格式解决复制粘贴格式错乱的问题
  2. 图解如何调整光驱激光头功率
  3. 北京地铁10号线地图(7月最新线路图、站名)
  4. HTML期末大作业~web前端开发个人博客HTML整站模板~Web大学生网页成品 ~个人主页博客网页HTML设计制作
  5. 基于拟蒙特卡洛模拟法的随机潮流计算matlab程序
  6. 更新华为云跑jd脚本
  7. 索尼摄像机MTS视频文件片段碎片恢复无缝组合修复技术
  8. Android之实现RecyclerView拖拽效果和固定部分元素不进行交换位置(包含源代码下载地址)
  9. [Halcon小结] 判断Halcon区域region是否为空
  10. 智能RGV的动态调度策略