java - XWPFDocument 循环替换段落

标签 java javafx apache-poi docx

我有一个包含项目的表。我想在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);
    }
}

关于java - XWPFDocument 循环替换段落,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46201217/

相关文章:

java - JavaFX 中的标签和文本差异

css - 不显示 float TabPane 的样式

java - 使用 apache POI 读取 ms excel 时不保留前导零

java - 新闻的代码拆分/格式 - 需要权威提及

java - Tomcat 8 速度随着时间的推移而降低

java - 表示我的 yaml 文件的更好方法是什么

java - 如何更新 JSON 文档中的对象列表

java - 终止属性监听器中的线程 (JavaFX 8)

java - 如何使用 Apache POI 选择和加粗整个工作表

java - 如何一次导出/生成两个文件?