1. 使用poi-ti填充word模板
- 依赖
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.5.1</version>
</dependency>
-
实现
- 填充实体类
UserContract contract = new UserContract(); String contractNo = order.getOrderNo(); contract.setOrderNo(contractNo); // 添加门店的信息 contract.setStoreName(store.getStoreName()); contract.setStoreAddress(store.getDeliveryAddress()); contract.setStorePhone(store.getStoreMobile()); contract.setStoreHead(store.getStoreHead()); contract.setStoreCourt(store.getStoreCourt()); // 添加用户的信息 contract.setUserName(user.getName()); contract.setUserIdCard(user.getIdcard()); contract.setUserPhone(user.getMobile()); contract.setUserAddress(user.getDeliveryAddress()); // 添加商品信息 contract.setPhoneType(order.getPhoneModel() + "_" + order.getPhoneSpecification()); contract.setPhoneNum(order.getPhoneCount()); contract.setPhoneSp(order.getPhoneSpecification()); contract.setPhonePrice(String.valueOf(order.getOrderAmountTotal().subtract(BigDecimal.valueOf(1000)))); contract.setAllPrice(String.valueOf(order.getOrderAmountTotal().subtract(BigDecimal.valueOf(1000)))); contract.setAllAmount(String.valueOf(order.getOrderAmountTotal())); // 添加分期数最后一期 contract.setLastPayTime(lastPayTime.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"))); contract.setFirstPayDay(order.getPayOnceTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); contract.setFirstAmount(String.valueOf(order.getPayOnceAmount())); contract.setResidualAmount(String.valueOf(order.getInstallmentAmountTotal())); contract.setPhasesNumber(order.getInstallmentNum()); contract.setFixedDay(order.getInstallmentPayDay()); List<RowRenderData> listRowRenderData = getListRowRenderData(order.getInstallmentNum(), order.getInstallmentAmount()); MiniTableRenderData tableRenderData = new MiniTableRenderData(listRowRenderData); // 添加表格数据 contract.setBillTable(tableRenderData); // 设置月份 contract.setFixedMonth(order.getInstallmentMonthType()); return contract;
2.获取文件并填充输出到response流中
File file = new File(template_contract);
// 设置response输入流
OutputStream outputStream = response.getOutputStream();
// word
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment;filename=\"" + URLEncoder.encode(filename, "UTF-8") + ".docx" + "\"");
// 开始生成新模板
XWPFTemplate template = XWPFTemplate.compile(file);
template.render(contract);
template.write(outputStream);
2. 使用poi将word转成pdf
-
依赖
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>ooxml-schemas</artifactId> <version>1.3</version> </dependency>
-
实现
File file = new File(filename); if (!file.exists()) { throw new IOException("word版本合同丢失"); } FileInputStream fileInputStream = new FileInputStream(file); ByteArrayOutputStream outPDF = new ByteArrayOutputStream(); XWPFDocument xwpfDocument = new XWPFDocument(fileInputStream); PdfOptions options = PdfOptions.create(); PdfConverter.getInstance().convert(xwpfDocument, outPDF, options); FileOutputStream fileOutputStream = new FileOutputStream(filename.replace("docx", "pdf")); fileOutputStream.write(outPDF.toByteArray()); outPDF.close(); fileOutputStream.close(); fileInputStream.close(); return filename.replace("docx", "pdf");