easyPoi官网:/
一.制作要导出的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 .URL;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;/*** 导出Word* @author Administrator**/@RequestMapping("/export/")@RestControllerpublic 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 4jodconverter.local.max-tasks-per-process=10jodconverter.local.port-numbers=8100
安装openoffice
/Howinfun/article/details/80759008
windows启动openoffice
/qq_43561507/article/details/106623176