Apache POI XWPF 不替换而是连接 [英] Apache POI XWPF does not replace but concatenates

查看:25
本文介绍了Apache POI XWPF 不替换而是连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Apache POI 的新手,在替换文本时遇到问题.

I'm new with Apache POI and I'm having trouble replacing text.

我在这里复制了我的代码 替换 Apache POI 中的文本XWPF 不工作

I copied my code here Replacing a text in Apache POI XWPF not working

它可以工作,但它不会替换文本而是连接它.所以如果我有Quick brown fox jumps over"并将over"替换为under".我得到快速的棕色狐狸跳过快速的棕色狐狸跳下".

It works but it does not replace the text but concatenates it. So if I have "Quick brown fox jumps over" and replace "over" with "under". I get "Quick brown fox jumps overQuick brown fox jumps under".

怎么了?

代码如下:

public class testPOI {

    public static void main(String[] args) throws Exception{

        String filepath = "F:\\MASTER_DOC.docx";
        String outpath = "F:\\Test.docx";

        XWPFDocument doc = new XWPFDocument(new FileInputStream(filepath));
        for (XWPFParagraph p : doc.getParagraphs()){

            int numberOfRuns = p.getRuns().size();

            // Collate text of all runs
            StringBuilder sb = new StringBuilder();
            for (XWPFRun r : p.getRuns()){
                int pos = r.getTextPosition();
                if(r.getText(pos) != null) {
                    sb.append(r.getText(pos));
                }
            }

            // Continue if there is text and contains "test"
            if(sb.length() > 0 && sb.toString().contains("test")) {
                // Remove all existing runs
                for(int i = 0; i < numberOfRuns; i++) {
                    p.removeRun(i);
                }
                String text = sb.toString().replace("test", "DOG");
                // Add new run with updated text
                XWPFRun run = p.createRun();
                run.setText(text);
                p.addRun(run);
            }
        }
       doc.write(new FileOutputStream(outpath));
    }
}

编辑 1:这很奇怪!我尝试在第二次运行时更换工作正常.第一次运行有问题.谁能指点一下?

EDIT 1: THIS IS WEIRD! I tried replacing on 2nd run works fine. Something is wrong with 1st run. Can anyone point it out?

推荐答案

我试过了.它有效.

    public class testPOI {

public static void main(String[] args) throws Exception{

    String filepath = "F:\\MASTER_DOC.docx";
    String outpath = "F:\\Test.docx";

    XWPFDocument doc = new XWPFDocument(new FileInputStream(filepath));
    for (XWPFParagraph p : doc.getParagraphs()){

        int numberOfRuns = p.getRuns().size();

        // Collate text of all runs
        StringBuilder sb = new StringBuilder();
        for (XWPFRun r : p.getRuns()){
            int pos = r.getTextPosition();
            if(r.getText(pos) != null) {
                sb.append(r.getText(pos));
            }
        }

        // Continue if there is text and contains "test"
        if(sb.length() > 0 && sb.toString().contains("test")) {
            // Remove all existing runs
            for(int i = numberOfRuns; i >=0 ; i--) {
                p.removeRun(i);
            }
            String text = sb.toString().replace("test", "DOG");
            // Add new run with updated text
            XWPFRun run = p.createRun();
            run.setText(text);
            p.addRun(run);
        }
    }
   doc.write(new FileOutputStream(outpath));
}
}

这篇关于Apache POI XWPF 不替换而是连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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