简体   繁体   English

Apache POI XWPF不替代而是连接

[英]Apache POI XWPF does not replace but concatenates

I'm new with Apache POI and I'm having trouble replacing text. 我是Apache POI的新手,无法替换文本。

I copied my code here Replacing a text in Apache POI XWPF not working 我在这里复制了代码在Apache POI XWPF中替换文本无法正常工作

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". 我得到“快速的棕色狐狸跳过下面的快速的棕色狐狸跳过”。

What is wrong? 怎么了?

So here's the code: 所以这是代码:

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));
    }
}

EDIT 1: THIS IS WEIRD! 编辑1:这很奇怪! I tried replacing on 2nd run works fine. 我尝试在第二次运行时更换效果很好。 Something is wrong with 1st run. 第一轮运行有问题。 Can anyone point it out? 有人可以指出吗?

I tried this. 我试过了 It works. 有用。

    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));
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM