当前位置:   article > 正文

Spring Boot + FreeMarker 实现按模板导出 Word 文档_springboot页面实现word导出功能

springboot页面实现word导出功能

SpringBoot是一个简化新Spring应用的初始搭建以及开发过程的框架,而FreeMarker是一个基于模板引擎的Java库,可以用来生成HTML网页、配置文件、电子邮件以及各种格式的文本文档。在这篇文章中,我们将结合使用SpringBoot和FreeMarker来生成和导出Word文档。

项目设置

首先,我们需要创建一个SpringBoot项目,并添加必要的依赖。我们使用 Maven作为构建工具。

Maven 依赖

在pom.xml文件中添加以下依赖:

  1. <dependencies>
  2. <!-- Spring Boot Starter Web -->
  3. <dependency>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-starter-web</artifactId>
  6. </dependency>
  7. <!-- FreeMarker -->
  8. <dependency>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-freemarker</artifactId>
  11. </dependency>
  12. <!-- Apache POI for Word document creation -->
  13. <dependency>
  14. <groupId>org.apache.poi</groupId>
  15. <artifactId>poi-ooxml</artifactId>
  16. <version>5.0.0</version>
  17. </dependency>
  18. </dependencies>

创建 FreeMarker 模板

接下来,我们需要创建一个FreeMarker模板文件。假设我们要生成一份简单的报告,模板文件report.ftl可以如下所示:

  1. <#-- report.ftl -->
  2. <html>
  3. <head>
  4. <title>${title}</title>
  5. </head>
  6. <body>
  7. <h1>${title}</h1>
  8. <p>${content}</p>
  9. </body>
  10. </html>

将该模板文件放在src/main/resources/templates目录下。

实现导出功能

在这一步,我们将创建一个控制器来处理导出请求,并生成Word文档。

创建控制器

  1. package com.example.demo.controller;
  2. import freemarker.template.Configuration;
  3. import freemarker.template.Template;
  4. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  5. import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  6. import org.apache.poi.xwpf.usermodel.XWPFRun;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.web.bind.annotation.GetMapping;
  9. import org.springframework.web.bind.annotation.RestController;
  10. import javax.servlet.http.HttpServletResponse;
  11. import java.io.ByteArrayOutputStream;
  12. import java.io.OutputStreamWriter;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15. @RestController
  16. public class ExportController {
  17. @Autowired
  18. private Configuration freemarkerConfig;
  19. @GetMapping("/export")
  20. public void exportWord(HttpServletResponse response) throws Exception {
  21. // 设置 FreeMarker 模板参数
  22. Map<String, Object> dataModel = new HashMap<>();
  23. dataModel.put("title", "报告标题");
  24. dataModel.put("content", "这是报告的内容。");
  25. // 获取 FreeMarker 模板
  26. Template template = freemarkerConfig.getTemplate("report.ftl");
  27. // 将模板数据输出到字符串
  28. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  29. try (OutputStreamWriter outputStreamWriter = new OutputStreamWriter(byteArrayOutputStream)) {
  30. template.process(dataModel, outputStreamWriter);
  31. }
  32. // 创建 Word 文档
  33. XWPFDocument document = new XWPFDocument();
  34. XWPFParagraph paragraph = document.createParagraph();
  35. XWPFRun run = paragraph.createRun();
  36. run.setText(byteArrayOutputStream.toString());
  37. // 设置响应头
  38. response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
  39. response.setHeader("Content-Disposition", "attachment; filename=report.docx");
  40. // 将 Word 文档写入响应输出流
  41. document.write(response.getOutputStream());
  42. response.getOutputStream().flush();
  43. document.close();
  44. }
  45. }

配置 FreeMarker

在src/main/resources目录下创建application.properties文件,添加以下配置:

  1. spring.freemarker.template-loader-path=classpath:/templates
  2. spring.freemarker.default-encoding=UTF-8

运行与测试

启动SpringBoot应用,并在浏览器中访问http://localhost:8080/export,浏览器将下载生成的Word文档。

总结

本文介绍了如何使用SpringBoot和FreeMarker实现按模板导出Word文档。通过这种方式,我们可以轻松地生成复杂的文档内容,并导出为用户友好的 Word 格式。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/853397
推荐阅读
相关标签
  

闽ICP备14008679号

        
cppcmd=keepalive&