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

java如何根據(jù)模板導(dǎo)出數(shù)據(jù)到word文檔中(表格、自定義標(biāo)簽等)

 更新時間:2024年11月05日 11:11:47   作者:八月林城  
這篇文章主要介紹了關(guān)于java如何根據(jù)模板導(dǎo)出數(shù)據(jù)到word文檔中(表格、自定義標(biāo)簽等)的相關(guān)資料,主要包括創(chuàng)建docx文檔,配置模板信息,以及利用XDocReport+FreeMarker技術(shù)進(jìn)行實(shí)現(xiàn),詳細(xì)介紹了在Word模板中如何設(shè)置字段以及如何通過代碼填充這些字段,需要的朋友可以參考下

1、制作模板

1.1 創(chuàng)建docx文檔,并且創(chuàng)建表格

1.2 配置模板信息

將鼠標(biāo)的光標(biāo)移到要輸入數(shù)據(jù)的地方,點(diǎn)擊“插入”,點(diǎn)擊“文檔部件”,點(diǎn)擊“域”;選擇“郵件合并”,在域代碼的函數(shù)后加入自定義的字段名“${字段名}”。如果不用循環(huán)的話,直接字段名字節(jié)定義名稱就可以了,如果需要循環(huán),字段名設(shè)置為【${對象.屬性}】

若需要循環(huán)表格內(nèi)的內(nèi)容時,處理如下,“域”中“郵件合并”的域代碼使用“${對象.屬性}”格式,自此,一個簡單的模板制作完成

2、代碼實(shí)現(xiàn)

2.1 pom.xml引入依賴

XDocReport +FreeMarker,該技術(shù)組合既簡單又高效可實(shí)現(xiàn)word模板的編輯,docx和doc均可處理

<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.xdocreport.core</artifactId>
    <version>2.0.2</version>
</dependency>
<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.xdocreport.document</artifactId>
    <version>2.0.2</version>
</dependency>
<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.xdocreport.template</artifactId>
    <version>2.0.2</version>
</dependency>
<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.xdocreport.document.docx</artifactId>
    <version>2.0.2</version>
</dependency>
<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.xdocreport.template.freemarker</artifactId>
    <version>2.0.2</version>
</dependency>
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.23</version>
</dependency>

2.2 創(chuàng)建接收表格內(nèi)容的對象(也可以沒必要,在下面封裝中,也可以用map封裝)

package com.yhgc.domain;
import lombok.Data;

@Data
public class UserInfo {
	/** 姓名 */
    public String name;
    
	/** 性別 */
    public String sex;
    
	/** 年齡*/
    public String age;
}

2.3 調(diào)用的實(shí)現(xiàn)

@Test
public void testWord(){
    InputStream ins = null;
    OutputStream out = null;
    try {
        //獲取Word模板,模板存放路徑
        ins = new FileInputStream("E:\\export\\template\\模板1.docx");
        //注冊xdocreport實(shí)例并加載FreeMarker模板引擎
        IXDocReport report = XDocReportRegistry.getRegistry().loadReport(ins, TemplateEngineKind.Freemarker);
        //創(chuàng)建xdocreport上下文對象,用于存放具體數(shù)據(jù)
        IContext context = report.createContext();
        //創(chuàng)建要替換的文本變量
        List<UserInfo> userInfos = new ArrayList<>(); // 這里也可以使用Map集合
        UserInfo userInfo = new UserInfo();
        userInfo.setName("張三");
        userInfo.setAge("15");
        userInfo.setSex("男");
        userInfos.add(userInfo);
        UserInfo userInfo1 = new UserInfo();
        userInfo1.setName("李四");
        userInfo1.setAge("14");
        userInfo1.setSex("男");
        userInfos.add(userInfo1);
        UserInfo userInfo2 = new UserInfo();
        userInfo2.setName("李梅");
        userInfo2.setAge("16");
        userInfo2.setSex("女");
        userInfos.add(userInfo2);

        //此處的userInfo是word中命名的列表名
        context.put("userInfo", userInfos);
        context.put("title", "測試的標(biāo)題");
        //創(chuàng)建字段元數(shù)據(jù)
        FieldsMetadata fm = report.createFieldsMetadata();
        //Word模板中的表格數(shù)據(jù)對應(yīng)的集合類型
        fm.load("userInfo", UserInfo.class, true);
        //輸出到本地目錄
        String filePath = "e:\\export\\結(jié)果.docx";
        out = new FileOutputStream(new File(filePath));
        report.process(context, out);
    } catch (Exception e) {
        System.out.println("生成word發(fā)生異常"+e);
    }finally {
        try {
            if (ins != null){
                ins.close();
            }
            if (out != null){
                out.close();
            }
        } catch (IOException e) {
            System.out.println("文件流關(guān)閉失敗"+e);
        }
    }
}

2.4 實(shí)現(xiàn)效果

總結(jié) 

到此這篇關(guān)于java如何根據(jù)模板導(dǎo)出數(shù)據(jù)到word文檔中的文章就介紹到這了,更多相關(guān)java根據(jù)模板導(dǎo)出數(shù)據(jù)到word內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java報(bào)錯org.hibernate.TypeMismatchException的解決方法

    Java報(bào)錯org.hibernate.TypeMismatchException的解決方法

    在Java開發(fā)領(lǐng)域,尤其是涉及到數(shù)據(jù)持久化的項(xiàng)目中,Hibernate是一款廣泛使用的強(qiáng)大工具,然而,可能會在使用過程中遭遇各種報(bào)錯,其中org.hibernate.TypeMismatchException就是一個讓人頭疼的問題,下面我們一起深入剖析這個報(bào)錯信息
    2024-11-11
  • 梳理總結(jié)Java?static關(guān)鍵字的方法作用

    梳理總結(jié)Java?static關(guān)鍵字的方法作用

    這篇文章主要介紹了梳理總結(jié)Java?static關(guān)鍵字的方法作用,?static?關(guān)鍵字可以用來修飾的成員變量和成員方法,被修飾的成員是屬于類的,而不是單單是屬于某個對象的
    2022-06-06
  • 詳解Java synchronized關(guān)鍵字的用法

    詳解Java synchronized關(guān)鍵字的用法

    在多線程編程中常常使用鎖機(jī)制來確保同一時刻只有一個線程能夠修改共享內(nèi)存,在Java中一般是使用synchronized作為鎖機(jī)制,下面就讓我們來學(xué)習(xí)一下如何使用synchronized實(shí)現(xiàn)線程安全吧
    2023-08-08
  • SpringBoot定時任務(wù)詳解與案例代碼

    SpringBoot定時任務(wù)詳解與案例代碼

    SpringBoot是一個流行的Java開發(fā)框架,它提供了許多便捷的特性來簡化開發(fā)過程,其中之一就是定時任務(wù)的支持,讓開發(fā)人員可以輕松地在應(yīng)用程序中執(zhí)行定時任務(wù),本文將詳細(xì)介紹如何在Spring?Boot中使用定時任務(wù),并提供相關(guān)的代碼示例
    2023-06-06
  • zookeeper集群搭建超詳細(xì)過程

    zookeeper集群搭建超詳細(xì)過程

    這篇文章主要介紹了zookeeper集群搭建超詳細(xì)過程,本文對zookeeper集群測試通過圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • Spring集成事務(wù)代碼實(shí)例

    Spring集成事務(wù)代碼實(shí)例

    這篇文章主要介紹了Spring集成事務(wù)代碼實(shí)例,pring事務(wù)的本質(zhì)其實(shí)就是數(shù)據(jù)庫對事務(wù)的支持,使用JDBC的事務(wù)管理機(jī)制,就是利用java.sql.Connection對象完成對事務(wù)的提交,需要的朋友可以參考下
    2023-10-10
  • 一文弄懂Maven依賴范圍

    一文弄懂Maven依賴范圍

    本文詳細(xì)介紹了Maven依賴范圍的概念、應(yīng)用及其在項(xiàng)目構(gòu)建和管理中的重要性,依賴范圍包括compile、provided、runtime、test和system等類型,每種范圍定義了依賴在不同構(gòu)建階段的可用性和打包行為,感興趣的可以了解一下
    2024-11-11
  • java讀取excel表格的方法

    java讀取excel表格的方法

    這篇文章主要為大家詳細(xì)介紹了java讀取excel表格的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • SpringBoot 整合Tess4J庫實(shí)現(xiàn)圖片文字識別案例詳解

    SpringBoot 整合Tess4J庫實(shí)現(xiàn)圖片文字識別案例詳解

    Tess4J是一個基于Tesseract OCR引擎的Java接口,可以用來識別圖像中的文本,說白了,就是封裝了它的API,讓Java可以直接調(diào)用,今天給大家分享一個SpringBoot整合Tess4j庫實(shí)現(xiàn)圖片文字識別的小案例
    2023-10-10
  • 微信支付H5調(diào)用支付詳解(java版)

    微信支付H5調(diào)用支付詳解(java版)

    本篇文章主要介紹了微信支付H5調(diào)用支付詳解,小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧。
    2016-12-12

最新評論