1.word模板设计填充字段加{{填充字段名}}
2.后端依赖
<poi-tl.version>1.7.3</poi-tl.version>
<poi.version>4.1.2</poi.version>
<dependency> <groupId>com.deepoove</groupId> <artifactId>poi-tl</artifactId> <version>${poi-tl.version}</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>${poi.version}</version> </dependency>
3.后端代码
3.1工具类
package com.ruoyi.common.utils.poi; import com.deepoove.poi.XWPFTemplate; import com.deepoove.poi.config.Configure; import com.deepoove.poi.policy.HackLoopTableRenderPolicy; import com.ruoyi.common.utils.uuid.UUID; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.math.BigDecimal; import java.nio.file.Files; import java.nio.file.Paths; public class WordUtil { public static void test(Object data, String filePath, HttpServletResponse response) { HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy(); Configure config = Configure.newBuilder().bind("list", policy).build(); XWPFTemplate template = XWPFTemplate.compile(filePath, config).render(data);//调用模板,填充数据 /*src\main\resources\wordTemplate\test.docx*/ String uuid = UUID.randomUUID().toString(); try { FileOutputStream out = new FileOutputStream("C:\\Users\\ZDHS\\Desktop\\"+uuid+".docx");//要导出的文件名 template.write(out); out.flush(); out.close(); template.close(); /*==*/ String fileName = "Operator.docx"; // 文件的默认保存名 // 读到流中 InputStream inStream = new FileInputStream("C:\\Users\\ZDHS\\Desktop\\"+uuid+".docx");// 文件的存放路径 // 设置输出的格式 response.reset(); response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); // 循环取出流中的数据 byte[] b = new byte[100]; int len; try { while ((len = inStream.read(b)) > 0) response.getOutputStream().write(b, 0, len); inStream.close(); } catch (IOException e) { e.printStackTrace(); } /*==*/ } catch (IOException e) { e.printStackTrace(); } try { Files.delete(Paths.get("C:\\Users\\ZDHS\\Desktop\\" + uuid + ".docx")); } catch (IOException e) { e.printStackTrace(); } } }
3.2接口类
//@PreAuthorize("@ss.hasPermi('system:selection:export')") //@Log(title = "技术参数", businessType = BusinessType.EXPORT) @PostMapping("/wordTemplate") public void wordTemplate(HttpServletResponse response, HttpServletRequest request, HistoryVo history) { /*todo:处理数据*/ WordUtil.test(history,"todo:文件路径",response); }
4.前端事件方法
handleDownload(row) {
this.queryParams = row
console.log(row)
this.download('system/selection/wordTemplate', {
...this.queryParams
}, `selection_${new Date().getTime()}.docx`)文章来源:https://www.toymoban.com/news/detail-546463.html
},文章来源地址https://www.toymoban.com/news/detail-546463.html
到了这里,关于WORD模板替换,将文件给前端下载的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!