如何使用POI 3.8替换占位符的docx的头在Java中 [英] How to replace placeholders in header of docx in java using poi 3.8

查看:1301
本文介绍了如何使用POI 3.8替换占位符的docx的头在Java中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我绑在DOCX file.I头更换令牌已经在处理段落和表格,但它不是采摘头数据令牌替换。即时通讯使用Apache POI 3.8和使用eclipse ID在Java编码。
感谢名单

I'm tying to replace tokens in the header of docx file.I have handled the token replacement in paragraphs and tables but its not picking the header data. Im using apache poi 3.8 and coding in java using eclipse ID. Thanx

推荐答案

我不知道你是否已经得到了这个问题的解决方案。但是,我一直试图替换文档标题标记和它为我工作。

I don't know if you have got the solution for this question. But, I've tried to replace tokens in document header and it worked for me.

public XWPFDocument setHeader(XWPFDocument document, String token, String textToReplace){
    XWPFHeaderFooterPolicy policy= document.getHeaderFooterPolicy();
    XWPFHeader header = policy.getHeader(0);
    replaceInParagraphs(header.getParagraphs(), token, textToReplace);
    return document;
}

private void replaceInParagraphs(List<XWPFParagraph> paragraphs, String placeHolder, String replaceText){
    for (XWPFParagraph xwpfParagraph : paragraphs) {
        List<XWPFRun> runs = xwpfParagraph.getRuns();
        for (XWPFRun run : runs) {
            String runText = run.getText(run.getTextPosition());

            if(placeHolder !="" && !placeHolder.isEmpty()){
                if(runText != null &&
                        Pattern.compile(placeHolder, Pattern.CASE_INSENSITIVE).matcher(runText).find()){
                    runText = replaceText;
                }
            }
            run.setText(runText, 0);
        }
    }
}

希望这有助于。 :)

Hope this helps. :)

这篇关于如何使用POI 3.8替换占位符的docx的头在Java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆