2017-09-13 23 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"); 
       } 
      } 
     } 
    } 
}  

以下の段落でitemの次の名前が置き換えられるようにします。

@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);'明示的に実行からではなく、 'r.setText(テキストの後にのみ最初のテキスト部分を取得します、0)。 r.addBreak(); r.setText( "P01"); 'には2つのテキスト部分があります。あなたが[最小限の、完全で、検証可能な例](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ではなく、ループの一回だけと呼ばれ、私はそのようなリスト内のすべての項目名入れ:

@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); 
    } 
} 
関連する問題