替换Apache POI XWPF中的文本无法正常工作

Mic*_*ael 3 java apache ms-word apache-poi

我目前正在尝试处理上一篇名为" 替换Apache POI XWPF中的文本"的文章中提到的代码.

我已经尝试了以下它可以工作,但我不知道我是否遗漏了任何东西.当我运行代码时,文本不会被替换,而是添加到搜索结尾.例如,我创建了一个基本的word文档,并输入了文本"test".在下面的代码中,当我运行它时,我最终得到带有文本"testDOG"的新文档.

我不得不将原始代码从String text = r.getText(0)更改为String text = r.toString(),因为我在运行代码时不断收到NullError.

import java.io.*;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;


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(OPCPackage.open(filepath));
    for (XWPFParagraph p : doc.getParagraphs()){
        for (XWPFRun r : p.getRuns()){
            String text = r.toString();
            if(text.contains("test")) {
                text = text.replace("test", "DOG");
                r.setText(text);
            }
        }
    }
   doc.write(new FileOutputStream(outpath));
}
Run Code Online (Sandbox Code Playgroud)

编辑:感谢大家的帮助.我浏览了一下,在Apache POI中找到了替换表列值的解决方案

Jos*_*osh 5

此方法在段落中替换搜索字符串,并且能够使用跨越多个Run的字符串.

  private long replaceInParagraphs(Map<String, String> replacements, List<XWPFParagraph> xwpfParagraphs) {
    long count = 0;
    for (XWPFParagraph paragraph : xwpfParagraphs) {
      List<XWPFRun> runs = paragraph.getRuns();

      for (Map.Entry<String, String> replPair : replacements.entrySet()) {    
        String find = replPair.getKey();
        String repl = replPair.getValue();
        TextSegement found = paragraph.searchText(find, new PositionInParagraph());
        if ( found != null ) {
          count++;
          if ( found.getBeginRun() == found.getEndRun() ) {
            // whole search string is in one Run
            XWPFRun run = runs.get(found.getBeginRun());
            String runText = run.getText(run.getTextPosition());
            String replaced = runText.replace(find, repl);
            run.setText(replaced, 0);
          } else {
            // The search string spans over more than one Run
            // Put the Strings together
            StringBuilder b = new StringBuilder();
            for (int runPos = found.getBeginRun(); runPos <= found.getEndRun(); runPos++) {
              XWPFRun run = runs.get(runPos);
              b.append(run.getText(run.getTextPosition()));
            }                       
            String connectedRuns = b.toString();
            String replaced = connectedRuns.replace(find, repl);

            // The first Run receives the replaced String of all connected Runs
            XWPFRun partOne = runs.get(found.getBeginRun());
            partOne.setText(replaced, 0);
            // Removing the text in the other Runs.
            for (int runPos = found.getBeginRun()+1; runPos <= found.getEndRun(); runPos++) {
              XWPFRun partNext = runs.get(runPos);
              partNext.setText("", 0);
            }                          
          }
        }
      }      
    }
    return count;
  }
Run Code Online (Sandbox Code Playgroud)

  • 很不错!将此实现与 /sf/answers/2109181231/ 中的逻辑相结合,现在我们有一种方法可以进行全局搜索和替换:) (5认同)
  • 值得注意的是,“run.getPosition()”在大多数情况下返回 -1。但当每次运行只有一个文本位置时,它不会起作用。但是,从技术上讲,它可以有任意数量的 textPositions,我也经历过这样的情况。因此,最好的方法是使用“getCTR ()”进行运行,并遍历每次运行以获取文本位置的计数。textPositions 的数量等于 ``ctrRun.sizeOfTArray() `` (2认同)

Jam*_*esB 4

你的逻辑不太对。您需要先整理运行中的所有文本,然后进行替换。您还需要删除该段落的所有运行,并在找到“test”匹配时添加新的单个运行。

试试这个:

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));
    }
}
Run Code Online (Sandbox Code Playgroud)