欢迎来到徐庆高(Tea)的个人博客网站
磨难很爱我,一度将我连根拔起。从惊慌失措到心力交瘁,我孤身一人,但并不孤独无依。依赖那些依赖我的人,信任那些信任我的人,帮助那些给予我帮助的人。如果我愿意,可以分裂成无数面镜子,让他们看见我,就像看见自己。察言观色和模仿学习是我的领域。像每个深受创伤的人那样,最终,我学会了随遇而安。
当前位置: 日志文章 > 详细内容

Java利用docx4j+Freemarker生成word文档

2025年04月07日 Java
技术方案java 1.8 + docx4j + freemarkermaven依赖<dependency> <groupid>org.freemarker</gro

技术方案

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>

创建模板文件

保存至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>
        <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.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();

            // 创建 word 文档包
            wordprocessingmlpackage wordmlpackage = wordprocessingmlpackage.createpackage();
            maindocumentpart maindocumentpart = wordmlpackage.getmaindocumentpart();

            // 创建 xhtml 导入器实现类实例
            xhtmlimporterimpl xhtmlimporter = new xhtmlimporterimpl(wordmlpackage);

            // 将 html 内容导入到 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内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!