Java利用docx4j+Freemarker生成word文檔
更新時間:2025年04月07日 15:04:06 作者:ghostmen
這篇文章主要為大家詳細介紹了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/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}的簡歷</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>
<!-- 聯(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>實現(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 導入器實現(xiàn)類實例
XHTMLImporterImpl xhtmlImporter = new XHTMLImporterImpl(wordMLPackage);
// 將 HTML 內(nèi)容導入到 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);
}
}
}到此這篇關于Java利用docx4j+Freemarker生成word文檔的文章就介紹到這了,更多相關Java生成word內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringMVC中的handlerMappings對象用法
這篇文章主要介紹了SpringMVC中的handlerMappings對象用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
spring cloud 之 Feign 使用HTTP請求遠程服務的實現(xiàn)方法
下面小編就為大家?guī)硪黄猻pring cloud 之 Feign 使用HTTP請求遠程服務的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
SpringBoot使用@Slf4j注解實現(xiàn)日志輸出的示例代碼
@Slf4j 是 Lombok 庫中的一個注解,它極大地簡化了日志記錄的代碼,通過使用這個注解,Lombok 會自動在你的類中注入一個靜態(tài)的日志對象,本文給大家介紹了SpringBoot使用@Slf4j注解實現(xiàn)日志輸出的方法,需要的朋友可以參考下2024-10-10
Java thrift服務器和客戶端創(chuàng)建實例代碼
Thrift是一個軟件框架,用來進行可擴展且跨語言的服務的開發(fā)。接下來通過本文給大家介紹Java thrift服務器和客戶端創(chuàng)建實例代碼,需要的朋友參考下吧2017-04-04
java獲取新insert數(shù)據(jù)自增id的實現(xiàn)方法
這篇文章主要介紹了java獲取新insert數(shù)據(jù)自增id的實現(xiàn)方法,在具體生成id的時候,我們的操作順序一般是:先在主表中插入記錄,然后獲得自動生成的id,以它為基礎插入從表的記錄,需要的朋友可以參考下2019-06-06

