欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java利用docx4j+Freemarker生成word文檔

 更新時間:2025年04月07日 15:04:06   作者:ghostmen  
這篇文章主要為大家詳細(xì)介紹了Java如何利用docx4j+Freemarker生成word文檔,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

技術(shù)方案

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 導(dǎo)入器實現(xiàn)類實例
            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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringMVC中的handlerMappings對象用法

    SpringMVC中的handlerMappings對象用法

    這篇文章主要介紹了SpringMVC中的handlerMappings對象用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Maven如何解決添加依賴之后沒有加載jar包報錯問題

    Maven如何解決添加依賴之后沒有加載jar包報錯問題

    這篇文章主要介紹了Maven如何解決添加依賴之后沒有加載jar包報錯問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • spring cloud 之 Feign 使用HTTP請求遠(yuǎn)程服務(wù)的實現(xiàn)方法

    spring cloud 之 Feign 使用HTTP請求遠(yuǎn)程服務(wù)的實現(xiàn)方法

    下面小編就為大家?guī)硪黄猻pring cloud 之 Feign 使用HTTP請求遠(yuǎn)程服務(wù)的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • java中String的一些方法深入解析

    java中String的一些方法深入解析

    以下是對java中String的一些方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以參考下
    2013-07-07
  • SpringBoot使用@Slf4j注解實現(xiàn)日志輸出的示例代碼

    SpringBoot使用@Slf4j注解實現(xiàn)日志輸出的示例代碼

    @Slf4j 是 Lombok 庫中的一個注解,它極大地簡化了日志記錄的代碼,通過使用這個注解,Lombok 會自動在你的類中注入一個靜態(tài)的日志對象,本文給大家介紹了SpringBoot使用@Slf4j注解實現(xiàn)日志輸出的方法,需要的朋友可以參考下
    2024-10-10
  • Java thrift服務(wù)器和客戶端創(chuàng)建實例代碼

    Java thrift服務(wù)器和客戶端創(chuàng)建實例代碼

    Thrift是一個軟件框架,用來進(jìn)行可擴(kuò)展且跨語言的服務(wù)的開發(fā)。接下來通過本文給大家介紹Java thrift服務(wù)器和客戶端創(chuàng)建實例代碼,需要的朋友參考下吧
    2017-04-04
  • springmvc+kindeditor文件上傳實例詳解

    springmvc+kindeditor文件上傳實例詳解

    這篇文章主要為大家詳細(xì)介紹了springmvc+kindeditor文件上傳實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • 詳解SpringBoot自定義配置與整合Druid

    詳解SpringBoot自定義配置與整合Druid

    Druid是alibaba開源平臺上一個數(shù)據(jù)庫連接池實現(xiàn),結(jié)合了C3P0,DBCP等DB池的優(yōu)點,同時也有Web監(jiān)控界面。這篇文章主要介紹了Java之SpringBoot自定義配置與整合Druid的相關(guān)知識,需要的朋友可以參考下
    2021-09-09
  • java獲取新insert數(shù)據(jù)自增id的實現(xiàn)方法

    java獲取新insert數(shù)據(jù)自增id的實現(xiàn)方法

    這篇文章主要介紹了java獲取新insert數(shù)據(jù)自增id的實現(xiàn)方法,在具體生成id的時候,我們的操作順序一般是:先在主表中插入記錄,然后獲得自動生成的id,以它為基礎(chǔ)插入從表的記錄,需要的朋友可以參考下
    2019-06-06
  • SpringBoot中的自動配置原理詳解

    SpringBoot中的自動配置原理詳解

    這篇文章主要介紹了SpringBoot中的自動配置原理詳解,springboot的自動配置類直觀的表現(xiàn)就是通過一系列的注解,使得springboot項目在啟動的時候從配置文件中加載需要自動配置的類,注入容器中,需要的朋友可以參考下
    2024-01-01

最新評論