poi-tl替换文本_n_格式为下划线文本

 一段文字如下:

Confucius(孔子) _31_  Chinese teacher, editor, politician and philosopher of  the Spring and Autumn period of _32_ history. Confucius(孔子) _33__  Chinese teacher, editor, politician and philosopher of  the Spring and Autumn period of  __34_ history.

需求是将其中的 _数字_ 格式替换为即数字下划线格式

 先看代码:

package com.my.utils;

import com.alibaba.fastjson.JSONObject;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.Paragraphs;
import com.deepoove.poi.data.TextRenderData;
import com.deepoove.poi.data.Texts;
import com.deepoove.poi.data.style.Style;
import com.deepoove.poi.policy.ParagraphRenderPolicy;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xwpf.usermodel.UnderlinePatterns;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <pre>
 * Description : word文档生成工具类
 * @Author :kevy
 * Date : 2021/8/19 0019 13:46
 * Version : 1.0
 * </pre>
 */
public class DocUtil {
    public static final String SPT = "@@@";
    public static final String UNDERLINE_RULE = "[_]+[\\d]+[_]+";

    public static void main(String[] args) throws Exception {
        String tplPath = "E:\\test.docx";
        String outPath = "E:\\out.docx";
        String t = "Confucius(孔子) _31_  Chinese teacher, editor, politician and philosopher of  the Spring and Autumn period of _32_ history" +
                ".Confucius(孔子) _33__  Chinese teacher, editor, politician and philosopher of  the Spring and Autumn period of  __34_ history.";

        Configure config = Configure.builder().bind("title", new ParagraphRenderPolicy()).useSpringEL(false).build();
        JSONObject a = new JSONObject();
        a.put("title", Paragraphs.of().addTexts(dealUnderlineText(t)).create());
        XWPFTemplate template = XWPFTemplate.compile(tplPath, config).render(a);
        template.writeAndClose(new FileOutputStream(outPath));
    }

    /**
     * @Description 处理下划线文本
     * @Author kevy
     * @Date 2021/8/26 0026 11:57
     * @param t
     **/
    public static List<TextRenderData> dealUnderlineText(String t) {
        Matcher m = Pattern.compile(UNDERLINE_RULE).matcher(t);
        List<String> items = new ArrayList<>();
        while (m.find()) {
            items.add(m.group());
        }
        for (String s : items) {
            t = t.replace(s, SPT + s + SPT);
        }
        List<TextRenderData> list = new ArrayList<>();
        String[] arr = t.split(SPT);
        for (String s : arr) {
            if (StringUtils.isNotBlank(s)) {
                if (items.contains(s)) {
                    list.add(Texts.of("  " + s.replaceAll("_", "").replaceAll("\\s", "") + "  ").style(
                            Style.builder().buildUnderlineColor("000000").buildUnderlinePatterns(UnderlinePatterns.SINGLE).build()).create());
                } else {
                    list.add(Texts.of(s).create());
                }
            }
        }
        return list;
    }

}

 word模板格式如下:

生成效果如下:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
poi-tl是一种基于Apache POI和FreeMarker模板引擎的Java模板引擎,它可以帮助我们方便地生成各种格式的文档,包括Word、Excel和PowerPoint等。如果您想要替换Word文档中的图片,可以通过以下步骤实现: 1. 在Word文档中找到需要替换的图片,并记下它的编号或名称; 2. 在POI-TL模板中添一个标签,用于指定图片的位置,例如:${picture}; 3. 在Java代码中使用POI-TL提供的API,通过标签名称找到对应的位置,并将新的图片插入到该位置。 以下是具体的Java代码实现示例: ```java // 创建POI-TL配置对象 Configuration config = new Configuration(); // 设置POI-TL使用的模板文件所在目录 config.setTemplateDir("templateDir"); // 设置POI-TL使用的模板文件名 config.setTemplateName("templateName.docx"); // 创建数据模型对象 Map<String, Object> data = new HashMap<>(); // 将需要替换的图片名称或编号放入数据模型中 data.put("picture", "newPicture.jpg"); // 创建POI-TL对象 DocxRenderData docx = new DocxRenderData(new File("templateName.docx"), data); // 将POI-TL对象写入新的Word文档中 XWPFTemplate template = XWPFTemplate.compile("output.docx", config).render(docx); template.write(new FileOutputStream("output.docx")); template.close(); ``` 在上面的代码中,我们首先创建了一个POI-TL配置对象,并设置了模板文件所在目录和模板文件名。然后创建了一个数据模型对象,并将需要替换的图片名称或编号放入该对象中。接着,我们创建了一个POI-TL对象,并将其写入新的Word文档中。 在模板文件中,我们使用了`${picture}`标签来指定图片的位置。在Java代码中,我们使用了POI-TL提供的API来找到该标签,并将新的图片插入到该位置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

KV_T

您的鼓励会激发我的创作热情笑脸

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值