Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java XWPF文档替换循环中的段落_Java_Javafx_Apache Poi_Docx - Fatal编程技术网

Java XWPF文档替换循环中的段落

Java XWPF文档替换循环中的段落,java,javafx,apache-poi,docx,Java,Javafx,Apache Poi,Docx,我有一个包含项目的表。我想设置word文档中项目的名称,但每个项目都在一个新行中 因此,我创建了下面的空白: 当我的文本包含“P01”时,我用名称替换文本,添加新行并设置另一个文本“P01” 问题是: 它只替换项的第一个名称,而不替换其他名称。它没有读到我设置的新“P01”。我找到了答案,它不是最好的,但很有效 我更改了String[]的类型,而不是String,因此我可以这样做: public void findAndRemplaceString(XWPFDocument doc,String

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

因此,我创建了下面的空白:

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

问题是:


它只替换项的第一个名称,而不替换其他名称。它没有读到我设置的新“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
                    }
                }
            }
        }
    }
}
public void findAndRemplaceString(XWPFDocument doc,String champs[]){
对于(XWPFParagraph p:doc.getparagraph()){
java.util.List runs=p.getRuns();
如果(运行!=null){
对于(XWPFRun r:runs){
String text=r.getText(0);
如果(text!=null&&text.contains(“P01”)){

对于(int i=0;iThe
String text=r.getText(0);
只显式地从运行中获取第一个文本部分,但在
r.setText(text,0);r.addBreak();r.setText(“P01”);
之后,有两个文本部分。一个真正好的答案是不可能的,因为您没有提供文本,但您可以尝试
String text=r.text();
@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);
        }
    }
}  
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
                    }
                }
            }
        }
    }
}
@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);
    }
}