【poi-tl 最新详细使用教程】
本文通过简单介绍几个示例 实现 word简单导出
①maven引用
com.deepoove poi-tl 1.9.0 org.apache.poi poi-ooxml 4.1.2
②word导出公共方法
public static void exportWordToPath(@NotNull String templateName,@NotNull Map dataMap,@NotNull HttpServletResponse response, String fileName) throws IOException {XWPFTemplate xwpfTemplate = null;OutputStream outputStream = response.getOutputStream();try {ListRenderPolicy policy = new ListRenderPolicy();// 配置Configure config = Configure.newBuilder().bind("yqst", policy).build();InputStream inputStream = getTemplateFile(templateName);// 编译模板,填充数据xwpfTemplate = XWPFTemplate.compile(inputStream, config).render(dataMap);//设置Http响应头告诉浏览器下载这个附件fileName = new String(fileName.getBytes(), StandardCharsets.ISO_8859_1);response.setHeader("Content-Disposition", "attachment;Filename=" + fileName + ".docx");response.setContentType("application/octet-stream");OutputStream toClient = new BufferedOutputStream(response.getOutputStream());xwpfTemplate.write(toClient);toClient.flush();} catch (IOException e) {response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);throw new RuntimeException("生成文件失败!");} finally {if (xwpfTemplate != null) {xwpfTemplate.close();}outputStream.close();}}
③导出示例
@GetMapping("/exportWord")public void exportWord(HttpServletResponse response) throws IOException {Map map = new HashMap<>();//简单的文字导出map.put("name","这是一个简单的导出");//list导出 config需绑定 对象List
④模板示例

看下导出结果:

有不懂的可以留言~
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
