最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java使用poi-tl庫操作wold文檔的方法

 更新時間:2025年11月26日 11:48:51   作者:布Coder  
java項目實際開發(fā)中經(jīng)常會遇到制作word表單且表格數(shù)據(jù)行循環(huán)功能,這篇文章主要介紹了Java使用poi-tl庫操作wold文檔的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下

poi-tl 是一個基于Apache POI的Word模板引擎,也是一個免費(fèi)開源的Java類庫,

適用范圍:

在線文檔生成、合同簽訂、信息替換方面具有很大優(yōu)勢、采用 標(biāo)簽-替換的實現(xiàn)策略,簡單易上手、避免實際業(yè)務(wù)中的重復(fù)開發(fā). 業(yè)務(wù)與設(shè)計解藕.

開始操作

引入 poi-tl 項目

        <poi-version>5.4.0</poi-version>
        
        -- poi 項目依賴

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>${poi-version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>${poi-version}</version>
        </dependency>

        -- poi-tl 項目依賴
        <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.12.2</version>
        </dependency>

本講介紹模版生成(poi - tl支持無模版生成)

讀取模版文件、并且定義結(jié)果輸出

        XWPFTemplate
                .compile("模版文檔.docx")
                .render(map)
                .writeAndClose(new FileOutputStream("01.docx"));
  • XWPFTemplate :  模版操作類
    • compile : 模版文檔
    • render : 數(shù)據(jù)
    • writeAndClose : 結(jié)果輸出

普通圖文本替換 (文檔內(nèi)容 {{key}})

模版文件

代碼

        HashMap<String, Object> renderMap = new HashMap<>();

        renderMap.put("text","我是被替換的內(nèi)容");

        XWPFTemplate
                .compile("空白文檔.docx")
                .render(renderMap).
                writeAndClose(new FileOutputStream("01.docx"));

輸出文件

列表內(nèi)容生成 {{#key}}

模版

代碼實現(xiàn)

        HashMap<String, Object> renderMap = new HashMap<>();

        renderMap.put("text", "我是被替換的內(nèi)容");

        HashMap<String, Object> ones = new HashMap<>();

        ones.put("name", "981129-1");
        ones.put("date", new Date());

        ArrayList<Object> list = new ArrayList<>();
        list.add(ones);
        list.add(ones);
        list.add(ones);
        list.add(ones);

        renderMap.put("list", list);

        // 表格循環(huán)插件
        LoopRowTableRenderPolicy rowTableRenderPolicy = new LoopRowTableRenderPolicy();
        Configure configure = Configure.builder()
                .bind("list", rowTableRenderPolicy)
                .build();


        XWPFTemplate
                .compile("空白文檔.docx", configure)
                .render(renderMap).
                writeAndClose(new FileOutputStream("01.docx"));

復(fù)雜結(jié)構(gòu)列表內(nèi)容生成 (循環(huán)生成普通文本 + 列表)

自定義實現(xiàn)(代碼)

package com.bu.poi.world.plugns;

import com.bu.poi.world.VsEty;
import com.bu.poi.world.domain.DtEntity;
import com.bu.poi.world.domain.ObjEty;
import com.deepoove.poi.policy.AbstractRenderPolicy;
import com.deepoove.poi.render.RenderContext;
import com.deepoove.poi.template.run.RunTemplate;
import com.deepoove.poi.xwpf.BodyContainer;
import com.deepoove.poi.xwpf.BodyContainerFactory;
import com.deepoove.poi.xwpf.XWPFParagraphWrapper;
import org.apache.poi.xwpf.usermodel.*;

import java.util.List;

/**
 * @author haizhuangbu
 * @date 03 8月 2025 06:46
 * @mark CustomTableRenderPolicy
 */
public class CustomTableRenderPolicy extends AbstractRenderPolicy<VsEty> {

    @Override
    protected void afterRender(RenderContext<VsEty> context) {
        clearPlaceholder(context, true);
    }

    @Override
    public void doRender(RenderContext<VsEty> renderContext) {

        // 編輯行
        XWPFRun run = renderContext.getRun();


        RunTemplate runTemplate = (RunTemplate) renderContext.getEleTemplate();


        // 數(shù)據(jù)
        VsEty data = renderContext.getData();

        BodyContainer bodyContainer = BodyContainerFactory.getBodyContainer(run);


        List<DtEntity> list = data.getList();
        for (DtEntity dtEntity : list) {

            XWPFRun nvRun = createRun(runTemplate, (XWPFParagraph) run.getParent());

            nvRun.setText(dtEntity.getTitle() + "\n");

            bodyContainer.insertNewParagraph(nvRun);
            nvRun.addBreak();

            XWPFRun nvRun1 = createRun(runTemplate, (XWPFParagraph) nvRun.getParent());

            nvRun1.setText(dtEntity.getIdx() + "\n");
            bodyContainer.insertNewParagraph(nvRun1);
            nvRun1.addBreak();


            List<ObjEty> objEtyList = dtEntity.getList();

            for (ObjEty objEty : objEtyList) {
//                createRun(run);
//                XWPFRun tbRun = createRun(runTemplate, (XWPFParagraph) nvRun1.getParent());
                XWPFRun tbRun = createRun(run);
                XWPFTable table = bodyContainer.insertNewTable(tbRun, 0, 0);
                createRow(table, objEty);
                table.setWidth(8000);

            }


        }


    }

    private XWPFRun createRun(RunTemplate runTemplate, XWPFParagraph parent) {
        XWPFParagraphWrapper paragraphWrapper = new XWPFParagraphWrapper(parent);
        return paragraphWrapper.insertNewRun(runTemplate.getRunPos());

    }

    public void createRow(XWPFTable table, ObjEty objEty) {

        table.setInsideHBorder(XWPFTable.XWPFBorderType.SINGLE, 10, 2, "E8E8E8");
        table.setLeftBorder(XWPFTable.XWPFBorderType.SINGLE, 10, 2, "E8E8E8");
        table.setRightBorder(XWPFTable.XWPFBorderType.SINGLE, 10, 2, "E8E8E8");
        table.setTopBorder(XWPFTable.XWPFBorderType.SINGLE, 10, 2, "E8E8E8");
        table.setBottomBorder(XWPFTable.XWPFBorderType.SINGLE, 10, 2, "E8E8E8");
        XWPFTableRow r1 = table.getRow(0);


        XWPFTableCell left = r1.getCell(0);
        left.setText("姓名");


        XWPFTableCell right = r1.createCell();
        right.setText(objEty.getName());


        XWPFTableRow r2 = table.createRow();
        XWPFTableCell r2Left = r2.getCell(0);
        r2Left.setText("碼值");
        XWPFTableCell r2Right = r2.getCell(1);
        r2Right.setText(objEty.getTCode());

        XWPFTableRow r3 = table.createRow();
        XWPFTableCell r3Left = r3.getCell(0);
        r3Left.setText("中文名");
        XWPFTableCell r3Right = r3.getCell(1);
        r3Right.setText(objEty.getTName());


    }


    public XWPFRun createRun(XWPFRun run) {
        XWPFDocument document = run.getDocument();
        XWPFParagraph paragraph = document.createParagraph();
        return paragraph.createRun();
    }


}
        VsEty vsEty = new VsEty();

        ArrayList<DtEntity> dtEntities = new ArrayList<>();


        DtEntity dtEntity = new DtEntity();
        dtEntity.setIdx("我是下標(biāo)\n");
        dtEntity.setTitle("我是標(biāo)題\n");

        List<ObjEty> objEties = new ArrayList<>();

        ObjEty objEty = new ObjEty();
        objEty.setName("name");
        objEty.setTName("name1");
        objEty.setTCode("code1");


        ObjEty objEty2 = new ObjEty();
        objEty2.setName("name");
        objEty2.setTName("name1");
        objEty2.setTCode("code1");

        objEties.add(objEty);
        objEties.add(objEty2);


        dtEntity.setList(objEties);

        dtEntities.add(dtEntity);


        vsEty.setList(dtEntities);

        renderMap.put("report", vsEty);

        CustomTableRenderPolicy customTableRenderPolicy = new CustomTableRenderPolicy();

        // 表格循環(huán)插件
        LoopRowTableRenderPolicy rowTableRenderPolicy = new LoopRowTableRenderPolicy();
        Configure configure = Configure.builder()
                .bind("list", rowTableRenderPolicy)
                .bind("report", customTableRenderPolicy)
                .build();


        XWPFTemplate
                .compile("空白文檔.docx", configure)
                .render(renderMap).
                writeAndClose(new FileOutputStream("01.docx"));

模版

輸出樣式

官方文檔地址:https://deepoove.com/poi-tl/#loop-col-table

總結(jié)

到此這篇關(guān)于Java使用poi-tl庫操作wold文檔方法的文章就介紹到這了,更多相關(guān)Java poi-tl操作word文檔內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 關(guān)于@RequestParam的主要用法詳解

    關(guān)于@RequestParam的主要用法詳解

    這篇文章主要介紹了關(guān)于@RequestParam的主要用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • SpringBoot+MyBatis處理JSON字段的完整指南

    SpringBoot+MyBatis處理JSON字段的完整指南

    本文主要介紹了SpringBoot+MyBatis處理JSON字段的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2026-03-03
  • Java中使用StackWalker和Stream API進(jìn)行堆棧遍歷

    Java中使用StackWalker和Stream API進(jìn)行堆棧遍歷

    StackWalking API是添加到Java中最酷的(并且對大多數(shù)開發(fā)人員來說完全不切實際,一般不會用,除非深層跟蹤調(diào)優(yōu))的功能之一。在這篇簡短的文章中,我們將看到它是什么以及使用它有多么容易,很快的認(rèn)識它
    2018-09-09
  • java中的數(shù)組初始化賦初值方式

    java中的數(shù)組初始化賦初值方式

    這篇文章主要介紹了java中的數(shù)組初始化賦初值方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • Java使用反射來獲取泛型信息示例

    Java使用反射來獲取泛型信息示例

    這篇文章主要介紹了Java使用反射來獲取泛型信息,結(jié)合實例形式分析了java基于反射操作泛型信息的相關(guān)實現(xiàn)技巧與注意事項,需要的朋友可以參考下
    2019-07-07
  • 學(xué)習(xí)Java之Java中的異常處理機(jī)制詳解

    學(xué)習(xí)Java之Java中的異常處理機(jī)制詳解

    在本文中,小編將帶領(lǐng)大家來學(xué)習(xí)Java的異常處理機(jī)制,包括異常機(jī)制、異常類型、如何捕獲異常、如何拋出異常以及如何創(chuàng)建自定義異常等核心內(nèi)容,感興趣的同學(xué)跟著小編一起來看看吧
    2023-08-08
  • SpringMVC源碼之HandlerMapping處理器映射器解析

    SpringMVC源碼之HandlerMapping處理器映射器解析

    這篇文章主要介紹了SpringMVC源碼之HandlerMapping處理器映射器解析,在Spring?MVC中,HandlerMapping處理器映射器用于確定請求處理器對象,請求處理器可以是任何對象,只要它們使用了@Controller注解或注解@RequestMapping,需要的朋友可以參考下
    2023-08-08
  • SpringBoot超詳細(xì)講解Thymeleaf模板引擎

    SpringBoot超詳細(xì)講解Thymeleaf模板引擎

    這篇文章主要分享了Spring Boot整合使用Thymeleaf,Thymeleaf是新一代的Java模板引擎,類似于Velocity、FreeMarker等傳統(tǒng)引擎,關(guān)于其更多相關(guān)內(nèi)容,需要的小伙伴可以參考一下
    2022-07-07
  • MyBatis中的SQL映射文件如何配置參數(shù)映射和使用方法

    MyBatis中的SQL映射文件如何配置參數(shù)映射和使用方法

    MyBatis 是一種開源的 Java 持久化框架,它可以自動將數(shù)據(jù)庫中的數(shù)據(jù)映射到 Java 對象中,并且使得 Java 對象可以非常方便地存儲到數(shù)據(jù)庫中,本文將介紹 MyBatis 中 SQL 映射文件的參數(shù)映射配置和使用方法,需要的朋友可以參考下
    2023-07-07
  • SpringBoot中引入MyBatisPlus的常規(guī)操作

    SpringBoot中引入MyBatisPlus的常規(guī)操作

    這篇文章主要介紹了SpringBoot中引入MyBatisPlus的常規(guī)操作,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11

最新評論

荥阳市| 涟源市| 墨竹工卡县| 房山区| 驻马店市| 田东县| 敦化市| 孟津县| 奉贤区| 集安市| 湖口县| 宿松县| 视频| 新安县| 江达县| 龙陵县| 眉山市| 黄浦区| 汉沽区| 巴中市| 元朗区| 鹤岗市| 平果县| 酒泉市| 阿巴嘎旗| 凌源市| 怀安县| 丹阳市| 广丰县| 临邑县| 常宁市| 宜州市| 广河县| 邵东县| 老河口市| 嘉祥县| 乌海市| 民乐县| 凤山县| 巨鹿县| 石泉县|