2017-09-13 4 views
0

항목이 포함 된 테이블이 있습니다. 나는 단어 문서에있는 항목의 이름을 새 행에 설정하려고합니다. XWPFDocument 루프에서 paragraphe 바꾸기

내 텍스트가 포함

은 "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"은 읽지 않습니다.

+1

은'문자열 텍스트 = r.getText (0) : 그래서 그런 목록에있는 모든 항목의 이름을 넣어 , 0); r.addBreak(); r.setText ("P01"); 두 개의 텍스트 부분이 있습니다. [최소, 완전하고 검증 가능한 예제] (https://stackoverflow.com/help/mcve)를 제공하지 않아서 정말 좋은 답변을 드릴 수는 없지만'String text = r.text(); 대신. –

답변

0

답변을 찾았지만 가장 좋지는 않지만 작동합니다. ,

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

내가 버튼을 클릭하면 무효 findAndReplaceString라는 한 번만 대신 루핑 : 내가 이런 식으로 할 수 있도록

나는, 문자열 대신 문자열 []의 유형을 변경 한 `명시 적으로 실행에서하지만 r.setText`이후 첫 번째 텍스트 부분을 가져옵니다 (텍스트,

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