首页 星云 工具 资源 星选 资讯 热门工具
:

PDF转图片 完全免费 小红书视频下载 无水印 抖音视频下载 无水印 数字星空

Java基于模板导出Word文档Demo

后端 29.45KB 32 需要积分: 1
立即下载

资源介绍:

SpringBoot 项目,Java 基于模板导出 Word 文档 Demo 示例,使用 Freemarker 模板引擎实现,提取了 WordUtil 工具类。
package com.demo.wordex.controller; import com.demo.wordex.model.UserInfo; import com.demo.wordex.utils.WordUtil; import freemarker.cache.ClassTemplateLoader; import freemarker.template.Configuration; import freemarker.template.Template; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import java.io.BufferedWriter; import java.io.OutputStreamWriter; import java.io.Writer; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.time.LocalDate; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @author 秋牧 * 2024/7/22 16:49 */ @Slf4j @RestController @RequestMapping("/test") public class TestController { @Resource private WordUtil wordUtil; /** * 基于模板生成 Word 文档 * * @param response response */ @GetMapping("/generate_word") public void generateWord(HttpServletResponse response) { Map templateParam = new HashMap<>(5); templateParam.put("name", "小明"); templateParam.put("sex", "男"); templateParam.put("age", "18"); templateParam.put("birthday", "2024-07-22"); templateParam.put("cardNum", "440000000000001111"); try { // 设置 HTTP 响应的内容类型为 Microsoft Word 文档 response.setContentType("application/msword"); // 设置响应字符编码为 UTF-8 response.setCharacterEncoding("utf-8"); // 使用 URLEncoder 对文件名进行编码,以防止中文文件名在不同浏览器和操作系统下出现乱码问题 String filename = URLEncoder.encode("测试Word_" + System.currentTimeMillis(), "utf-8"); // 设置响应头,指定文档将以附件的形式下载,并定义文件名 response.setHeader("Content-disposition", "attachment;filename=" + filename + ".doc"); // 创建 Freemarker 的 Configuration 对象,设置默认的不兼容改进选项 Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS); configuration.setDefaultEncoding("utf-8"); // 设置模板加载器,加载模板文件 configuration.setTemplateLoader(new ClassTemplateLoader(getClass(), "/templates")); Template t = configuration.getTemplate("generate_word_test.xml", "utf-8"); // 创建 Writer 对象,用于将生成的文档写到输出流中,缓存区大小设为 10KB Writer out = new BufferedWriter(new OutputStreamWriter(response.getOutputStream(), StandardCharsets.UTF_8), 10240); // 将模型数据与模板结合生成 Word 文档,写入到 Writer 对象中 t.process(templateParam, out); out.close(); } catch (Exception e) { log.info("生成Word文档异常,异常原因:{}", e.getMessage(), e); throw new RuntimeException("生成Word文档异常,异常原因:" + e.getMessage()); } } /** * 基于模板生成 Word 文档(基于示例 1 提取 Word 工具类) * * @param response response */ @GetMapping("/generate_word_2") public void generateWord2(HttpServletResponse response) { Map templateParam = new HashMap<>(5); templateParam.put("name", "小明"); templateParam.put("sex", "男"); templateParam.put("age", "18"); templateParam.put("birthday", "2024-07-22"); templateParam.put("cardNum", "440000000000001111"); wordUtil.generate(response, "/templates", "generate_word_test.xml", templateParam, "测试Word"); } /** * 基于模板生成 Word 文档(使用对象入参) * * @param response response */ @GetMapping("/generate_word_3") public void generateWord3(HttpServletResponse response) { UserInfo userInfo = new UserInfo(); userInfo.setName("小明"); userInfo.setSex("男"); userInfo.setAge("18"); userInfo.setBirthday("2024-07-22"); userInfo.setCardNum("440000000000001111"); wordUtil.generate(response, "/templates", "generate_word_test.xml", userInfo, "测试Word"); } /** * 基于模板生成 Word 文档(生成动态列表) * * @param response response */ @GetMapping("/generate_dynamic_word") public void generateDynamicWord(HttpServletResponse response) { List userList = new ArrayList<>(); userList.add(new UserInfo("小明", "男", "18", "2024-07-22", "440000000000001111")); userList.add(new UserInfo("小红", "女", "20", "2024-07-26", "440000000000002222")); userList.add(new UserInfo("小白", "男", "17", "2024-07-24", "440000000000003333")); userList.add(new UserInfo("小蓝", "女", "19", "2024-07-23", "440000000000004444")); userList.add(new UserInfo("小黑", "男", "18", "2024-07-21", "440000000000005555")); Map templateParam = new HashMap<>(5); templateParam.put("date", LocalDate.now()); templateParam.put("total", userList.size()); templateParam.put("userList", userList); wordUtil.generate(response, "/templates", "dynamic_userlist.xml", templateParam, "测试动态列表Word"); } }

资源文件列表:

word-export-demo-master.zip 大约有27个文件
  1. word-export-demo-master/
  2. word-export-demo-master/.gitignore 490B
  3. word-export-demo-master/.idea/
  4. word-export-demo-master/.idea/.gitignore 176B
  5. word-export-demo-master/.idea/encodings.xml 345B
  6. word-export-demo-master/.idea/git_toolbox_blame.xml 171B
  7. word-export-demo-master/.idea/misc.xml 528B
  8. word-export-demo-master/.idea/vcs.xml 180B
  9. word-export-demo-master/pom.xml 1.9KB
  10. word-export-demo-master/src/
  11. word-export-demo-master/src/main/
  12. word-export-demo-master/src/main/java/
  13. word-export-demo-master/src/main/java/com/
  14. word-export-demo-master/src/main/java/com/demo/
  15. word-export-demo-master/src/main/java/com/demo/wordex/
  16. word-export-demo-master/src/main/java/com/demo/wordex/WordExportDemoApplication.java 395B
  17. word-export-demo-master/src/main/java/com/demo/wordex/controller/
  18. word-export-demo-master/src/main/java/com/demo/wordex/controller/TestController.java 5.48KB
  19. word-export-demo-master/src/main/java/com/demo/wordex/model/
  20. word-export-demo-master/src/main/java/com/demo/wordex/model/UserInfo.java 366B
  21. word-export-demo-master/src/main/java/com/demo/wordex/utils/
  22. word-export-demo-master/src/main/java/com/demo/wordex/utils/WordUtil.java 2.79KB
  23. word-export-demo-master/src/main/resources/
  24. word-export-demo-master/src/main/resources/application.yml 20B
  25. word-export-demo-master/src/main/resources/templates/
  26. word-export-demo-master/src/main/resources/templates/dynamic_userlist.xml 89.8KB
  27. word-export-demo-master/src/main/resources/templates/generate_word_test.xml 109.48KB
0评论
提交 加载更多评论
其他资源 ico图片资源Python应用程序图片
ico图片资源Python应用程序图片
尚硅谷2024最新版RabbitMQ笔记
尚硅谷2024最新版RabbitMQ笔记,原链接为https://pan.baidu.com/s/1CjTQGsRYsS8iPUEKUIi90w?pwd=yyds&_at_=1721955632782#list/path=%2Fsharelink4035995002-1084021945033434%2F%E5%B0%9A%E7%A1%85%E8%B0%B72024%E6%9C%80%E6%96%B0%E7%89%88RabbitMQ%E8%A7%86%E9%A2%91&parentPath=%2Fsharelink4035995002-1084021945033434
尚硅谷2024最新版RabbitMQ笔记 尚硅谷2024最新版RabbitMQ笔记 尚硅谷2024最新版RabbitMQ笔记
2024年钉钉杯大数据挑战赛初赛题目.zip
2024年钉钉杯大数据挑战赛初赛题目.zip
vue_homework02.zip
vue_homework02.zip
vue_homework02.zip vue_homework02.zip vue_homework02.zip
Nginx-Fancyindex-Theme
nginx中fancyindex模块的主题,该主题本人以汉化,技术有限可能不完整
letsvpn-2.24.3.apk (2).zip
letsvpn-2.24.3.apk (2).zip
自己重新修改代码1234
自己重新修改代码1234
HI3519V101 DATASHEET
HI3519V101 DATASHEET