当前位置: 首页 > article >正文

【Poi-tl Documentation】自定义行删除标签

前置说明:

<dependency>
    <groupId>com.deepoove</groupId>
    <artifactId>poi-tl</artifactId>
    <version>1.12.1</version>
</dependency>

模板样式:
删除行表格测试.docx
image.png
实现思路:通过定制占位符,然后将占位符所在的行进行删除。

@Slf4j
public class CustomRenderPolicy implements RenderPolicy {

    @Override
    public void render(ElementTemplate elementTemplate, Object o, XWPFTemplate xwpfTemplate) {
        log.info("=====================CustomRenderPolicy=====================");
        RunTemplate runTemplate = (RunTemplate) elementTemplate;
        XWPFRun run = runTemplate.getRun();
        XWPFTableCell cell = (XWPFTableCell) ((XWPFParagraph) run.getParent()).getBody();
        int rowIndex = getRowIndex(cell.getTableRow());
        XWPFTable table = cell.getTableRow().getTable();
        // 问题1:
        table.removeRow(rowIndex);

    }

    private int getRowIndex(XWPFTableRow row) {
        List<XWPFTableRow> rows = row.getTable().getRows();
        return rows.indexOf(row);
    }
}


import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.config.ConfigureBuilder;
import com.deepoove.poi.render.RenderContext;
import com.deepoove.poi.xwpf.BodyContainer;
import com.deepoove.poi.xwpf.BodyContainerFactory;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import run.siyuan.poi.tl.policy.CustomRenderPolicy;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) throws IOException {
        // 读取模板文件
        FileInputStream fileInputStream = new FileInputStream("/Users/wuzhiqian/Desktop/HY/删除行表格测试.docx");

        // 创建模板配置
        ConfigureBuilder configureBuilder = Configure.builder();
        // 问题1 处理方案
        configureBuilder.setValidErrorHandler(new Configure.ClearHandler(){
            @Override
            public void handler(RenderContext<?> context) {
                System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");
                try {
                    XWPFRun run = context.getRun();
                    run.setText("/");
                    BodyContainer bodyContainer = BodyContainerFactory.getBodyContainer(run);
                    bodyContainer.clearPlaceholder(run);
                } catch (Exception e) {
                    System.out.println("标签不存在-------------------------------------------");
                }
            }
        });
        configureBuilder.addPlugin('!', new CustomRenderPolicy());

        // 创建模板上下文
        Map<String, Object> context = new HashMap<>();
        context.put("a", "a");

        // 使用模板引擎替换文本标签
        XWPFTemplate compile = XWPFTemplate.compile(fileInputStream, configureBuilder.build());
        compile.render(context);

        // 保存生成的文档
        FileOutputStream outputStream = new FileOutputStream("/Users/wuzhiqian/Desktop/HY/删除行表格测试_" + System.currentTimeMillis() + ".docx");
        compile.write(outputStream);
        outputStream.close();

        compile.close();
        fileInputStream.close();
    }
}

遇到的问题:
错误1:定制的占位符是 !开头的,而模板文件中后面还存在一个 {{aaaaaa}} 占位符, 此时调用删除行同样会把 {{aaaaaa}} 删除,而 {{aaaaa}} 是一个文本占位符对应的策略TextRenderPolicy,当该测路执行的时候发现该占位符没有对应值,则会执行错误处理,默认情况下的默认处理是:

    public static class ClearHandler implements ValidErrorHandler {
        public ClearHandler() {
        }

        public void handler(RenderContext<?> context) {
            XWPFRun run = context.getRun();
            BodyContainer bodyContainer = BodyContainerFactory.getBodyContainer(run);
            bodyContainer.clearPlaceholder(run);
        }
    }

在调用context.getRun();方法的时候就会发现 {{aaaaa}} 已经被删除了,才会报错。
处理方案:就是对会报错的方法进行处理,我这里是进行了捕获。


http://www.kler.cn/a/271253.html

相关文章:

  • OutputStreamWriter类
  • Linux-centos如何搭建yum源仓库
  • 最细节操作 Linux LVM 逻辑卷管理
  • 算法---滑动窗口练习-8(最小覆盖子串)
  • Explain 关键字
  • 【计算机网络】https的工作原理以及和http的区别
  • spacy进行简单的自然语言处理的学习
  • 【LeetCode热题100】23. 合并 K 个升序链表(链表)
  • 获取扇区航班数
  • hoverEnabled
  • 【刷题训练】牛客:JZ31 栈的压入、弹出序列
  • 用云服务器构建gpt和stable-diffusion大模型
  • C语言基础之单向链表
  • LabVIEW多表位数字温湿度计图像识别系统
  • 关于数据通信知识的补充——第二篇
  • 【IC设计】Verilog线性序列机点灯案例(三)(小梅哥课程)
  • github起源
  • 数学建模--MATLAB基本使用
  • idea Springboot 组卷管理系统LayUI框架开发mysql数据库web结构java编程计算机网页
  • 部署一个本地的ChatGPT(Ollama)