XWPFDocument 循环替换段落

XWPFDocument replace paragraphe in a loop

我有一个包含项目的 table。我想在word文档中设置项目名称,但每一个都在一个新行中。

所以我创建了下面的空白:

当我的文本包含 "P01" 时,我用名称替换文本,添加新行并设置另一个文本 "P01".

public void findAndRemplaceString(XWPFDocument doc, String champs) throws IOException {
    for (XWPFParagraph p : doc.getParagraphs()) {
        java.util.List<XWPFRun> runs = p.getRuns();
        if (runs != null) {
            for (XWPFRun r : runs) {
                String text = r.getText(0);
                if (text != null && text.contains("P01")) {
                    text = text.replace("P01", champs);
                    System.out.println("text replaced");
                    r.setText(text, 0);
                    //add new line
                    r.addBreak();
                    //new "P01" added
                    r.setText("P01");
                }
            }
        }
    }
}    

以便在下面的段落中替换项目的下一个名称。

@FXML
void endButton(ActionEvent event) {
    String file = "model";
    for (Person item : table.getItems()) {
        //get the name of item
        String a = item.getName();
        // get the index of item
        int ind0 = table.getItems().indexOf(item);
        int ind1 = table.getItems().indexOf(item) + 1;
        try {
            XWPFDocument doc = new XWPFDocument(new FileInputStream(new File(file + ind0 + ".docx")));
            findAndRemplaceString(doc, a);
            FileOutputStream fileOutputStream = new FileOutputStream(new File(file + ind1 + ".docx"));
            doc.write(fileOutputStream);
            fileOutputStream.close();
            doc.close();
        } catch (Exception e) {
            System.out.println("erreur  " + e);
        }
    }
}  

问题是:

它只替换项目的名字而不是其他的。它不读取我设置的新 "P01"。

我找到了答案,它不是最好的但很有效。

我更改了 String[] 的类型而不是 String,这样我就可以这样做 :

public void findAndRemplaceString(XWPFDocument doc,String champs[]){       
    for (XWPFParagraph p : doc.getParagraphs()) {       
        java.util.List<XWPFRun> runs = p.getRuns();
        if (runs != null) {            
            for (XWPFRun r : runs) {            
                String text = r.getText(0);            
                if (text != null && text.contains("P01") ) {
                    for (int i=0;i<champs.length;i++){
                        text = text.replace("P01","");
                        r.setText(text,0);   //Replace the old text 
                        r.setText(champs[i]);//add the new text
                        r.addBreak();        //new line
                    }
                }
            }
        }
    }
}

当我点击按钮时,void findAndReplaceString 只被调用一次而不是循环调用,所以我将所有项目名称放在一个列表中,如下所示:

@FXML void endButton(ActionEvent event) {
List<String> list = new ArrayList<String>();        
for (Person item : table.getItems()) {       
    String a = item.getName();
    list.add(a);
}     
String[] simpleArray = new String[list.size()];
list.toArray(simpleArray);
try{   
    XWPFDocument doc = new XWPFDocument(new FileInputStream(new File("input.docx")));               
    findAndRemplaceString(doc,simpleArray);
    FileOutputStream fileOutputStream = new FileOutputStream(new File("output.docx")); 
    doc.write(fileOutputStream);
    fileOutputStream.close();
    doc.close();                    
}catch (Exception e) { 
    System.out.println("erreur  " + e);
    }
}