Java利用docx4j+Freemarker生成word文檔
技術(shù)方案
java 1.8 + docx4j + Freemarker
maven依賴(lài)
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.31</version> <!-- 請(qǐng)使用最新版本 --> </dependency> <!-- https://mvnrepository.com/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>
創(chuàng)建模板文件
保存至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}的簡(jiǎn)歷</title> <style> @page { size: A4; margin: 5mm 10mm; /* 上下和左右兩個(gè)方向的邊距分別為 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}的簡(jiǎn)歷</h1> <!-- 聯(lián)系信息 --> <div class="contact-info"> <h2 class="section-title">聯(lián)系信息</h2> <p>郵箱: ${email}</p> <p>電話: ${phone}</p> </div> <!-- 工作經(jīng)歷 --> <div class="experience"> <h2 class="section-title">工作經(jīng)歷</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>
實(shí)現(xiàn)代碼
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.io.StringWriter; 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(); // 創(chuàng)建 Word 文檔包 WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(); MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart(); // 創(chuàng)建 XHTML 導(dǎo)入器實(shí)現(xiàn)類(lèi)實(shí)例 XHTMLImporterImpl xhtmlImporter = new XHTMLImporterImpl(wordMLPackage); // 將 HTML 內(nèi)容導(dǎo)入到 Word 文檔中 java.util.List<Object> convertedXHTML = xhtmlImporter.convert(htmlContent, null); // 將轉(zhuǎn)換后的內(nèi)容添加到文檔主體中 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); } } }
到此這篇關(guān)于Java利用docx4j+Freemarker生成word文檔的文章就介紹到這了,更多相關(guān)Java生成word內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringMVC中的handlerMappings對(duì)象用法
這篇文章主要介紹了SpringMVC中的handlerMappings對(duì)象用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09Maven如何解決添加依賴(lài)之后沒(méi)有加載jar包報(bào)錯(cuò)問(wèn)題
這篇文章主要介紹了Maven如何解決添加依賴(lài)之后沒(méi)有加載jar包報(bào)錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05spring cloud 之 Feign 使用HTTP請(qǐng)求遠(yuǎn)程服務(wù)的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇spring cloud 之 Feign 使用HTTP請(qǐng)求遠(yuǎn)程服務(wù)的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06SpringBoot使用@Slf4j注解實(shí)現(xiàn)日志輸出的示例代碼
@Slf4j 是 Lombok 庫(kù)中的一個(gè)注解,它極大地簡(jiǎn)化了日志記錄的代碼,通過(guò)使用這個(gè)注解,Lombok 會(huì)自動(dòng)在你的類(lèi)中注入一個(gè)靜態(tài)的日志對(duì)象,本文給大家介紹了SpringBoot使用@Slf4j注解實(shí)現(xiàn)日志輸出的方法,需要的朋友可以參考下2024-10-10Java thrift服務(wù)器和客戶端創(chuàng)建實(shí)例代碼
Thrift是一個(gè)軟件框架,用來(lái)進(jìn)行可擴(kuò)展且跨語(yǔ)言的服務(wù)的開(kāi)發(fā)。接下來(lái)通過(guò)本文給大家介紹Java thrift服務(wù)器和客戶端創(chuàng)建實(shí)例代碼,需要的朋友參考下吧2017-04-04springmvc+kindeditor文件上傳實(shí)例詳解
這篇文章主要為大家詳細(xì)介紹了springmvc+kindeditor文件上傳實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08java獲取新insert數(shù)據(jù)自增id的實(shí)現(xiàn)方法
這篇文章主要介紹了java獲取新insert數(shù)據(jù)自增id的實(shí)現(xiàn)方法,在具體生成id的時(shí)候,我們的操作順序一般是:先在主表中插入記錄,然后獲得自動(dòng)生成的id,以它為基礎(chǔ)插入從表的記錄,需要的朋友可以參考下2019-06-06