本文主要是介绍Java利用docx4j+Freemarker生成word文档,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
《Java利用docx4j+Freemarker生成word文档》这篇文章主要为大家详细介绍了Java如何利用docx4j+Freemarker生成word文档,文中的示例代码讲解详细,感兴趣的小伙伴...
技术方案
Java 1.8 + docx4j + Freemarker
maven依赖
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.31</version> <!-- 请使用最新版本 --> </dependency> <!-- https://mvnrepository.com/China编程artifact/org.docx4j/docx4j --> <dependency> <groupId>org.docx4j</groupId> <artifactId>docx4j</artifactId> <version>6.1.2</version> </dependency> <dependency> <groupId>org.docx4j</groupId> <artifactId>docx4j-ImportXhtml</artifactId> <version>6.1.0</version> </dependency> <!-- slf4j for logging --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.36</version> </dependency>
创建模板文件
保存至src/main/resources/templates/,文件名为template.html
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"></meta> <meta name="viewport" content="width=device-width, initial-scale=1.0"></meta> <title>${name}的简历</title> <style> @page { size: A4; margin: 5mm 10mm; /* 上下和左右两个方向的边距分别为 10mm 和 20mm */ } body { font-family: Arial, sans-serif; margin: 0; padding: 0; background-color: #f4f4f9; } .container { width: 80%; margin: 0 auto; padding: 20px; background-color: #ffffff; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); } h1 { text-align: center; color: #333; } .section-title { color: #4CAF50; margin-top: 20px; } .section-content { margin: 10px 0; } .contact-info, .skills, .experience, .education { margin-bottom: 20px; } .experience, .education { margin-top: 10px; } ul { list-style-type: none; padding-left: 0; } li { margin-bottom: 10px; } .job, .degree { font-weight: bold; } </style> </head> <body> <div class="container"> <h1>${name}的简历</h1> <!-- 联系信息 --> <div class="contact-info"> <h2 class="section-title">联系信息</h2> www.chinasem.cn <p>邮箱: ${email}</p> <p>电话: ${phone}</p> </div> <!-- 工作经历 --> <div class="experience"> <h2 class="section-title">工作经历</h2> <#list experience as job> <div class="job"> <p><strong>${job.position}</strong> 在 ${job.company}(${job.startYear} - ${job.endYear})</p> <p>${job.description}</p> </div> </#list> </div> <!-- 教育背景 --> <div class="education"> <h2 class="section-title">教育背景</h2> <#list education as degree> <div class="degree"> <p><strong>${degree.degree}</strong> ${degree.school}(${degree.year}年)</p> </div> </#list> </div> </div> </body> </html>
实现代码
package com.ruoyi.system.utils; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; import org.docx4j.Docx4J; import org.docx4j.convert.in.Xhtml.XHTMLImporterImpl; import org.docx4j.openpackaging.exceptions.Docx4JException; import org.docx4j.openpackaging.exceptions.InvalidFormatException; import org.docx4j.openpackaging.packages.WordprocessingMLPackage; import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; import java.io.File; import java.io.IOException; import java.ijavascripto.StrinandroidgWriter; import java.util.Map; /** * @Program: RuoYi-master * @ClassName: GenerateWord * @author: zhouzihao * @date: 2025年2月20日, 0020 下午 03:25 * @version: 1.0.0 * @Description: * @Time: 2025-02-20 15:25 */ public class GenerateWordUtil { // 模板路径 private static final String templatesPath = "/templates"; // 模板文件 private static final String templatesFile = "template.html"; public static void generateWord(Map<String, Object> data, String filePName) { // 配置 Freemarker Configuration cfg = new Configuration(Configuration.VERSION_2_3_30); cfg.setClassForTemplateLoading(GenerateWordUtil.class, templatesPath); // 获取 HTML 模板 try { Template template = cfg.getTemplate(templatesFile); // 使用 Freemarker 填充模板 StringWriter writer = new StringWriter(); template.process(data, writer); String htmlContent = writer.toString(); // 创建 Word 文档包 WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(); MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart(); // 创建 XHTML 导入器实现类实例 XHTMLImporterImpl xhtmlImporter = new XHTMLImporterImpl(wordMLPackage); // 将 HTMLjavascript 内容导入到 Word 文档中 java.util.List<Object> convertedXHTML = xhtmlImporter.convert(htmlContent, null); // 将转换后的内容添加到文档主体中 mainDocumentPart.getContent().addAll(convertedXHTML); // 保存生成的 Word 文档 File outputFile = new File(filePName); Docx4J.save(wordMLPackage, outputFile, Docx4J.FLAG_NONE); System.out.println("Word 文档生成成功:" + outputFile.getAbsolutePath()); } catch (IOException e) { throw new RuntimeException(e); } catch (TemplateException e) { throw new RuntimeException(e); } catch (InvalidFormatException e) { throw new RuntimeException(e); } catch (Docx4JException e) { throw new RuntimeException(e); } } }
到此这篇关于Java利用docx4j+Freemarker生成word文档的文章就介绍到这了,更多相关Java生成word内容请搜索China编程(www.chinasem.cn)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程China编程(www.chinasem.cn)!
这篇关于Java利用docx4j+Freemarker生成word文档的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!