Apache的POI XWPF不会取代而是会连接 [英] Apache POI XWPF does not replace but concatenates

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

问题描述

我是新与Apache POI和我无法替换文本。

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

我在这里<复制的我的code href=\"http://stackoverflow.com/questions/24203087/replacing-a-text-in-apache-poi-xwpf-not-working\">Replacing在Apache的POI XWPF文本不工作

它的工作原理,但它不会取代文本,但并置它。
所以,如果我有快速的棕色狐狸跳过了,并以下的替代过。我得到快速的棕色狐狸跳过下overQuick的棕色狐狸跳过。

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".

什么是错的?

因此​​,这里的code:

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

编辑1:这很奇怪!我试着更换2日运行正常工作。什么是错的与第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?

推荐答案

这个我试过。它的工作原理。

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

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

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