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"は読み込まれません。
を '文字列のテキスト= r.getText(0);'明示的に実行からではなく、 'r.setText(テキストの後にのみ最初のテキスト部分を取得します、0)。 r.addBreak(); r.setText( "P01"); 'には2つのテキスト部分があります。あなたが[最小限の、完全で、検証可能な例](https://stackoverflow.com/help/mcve)を提供していないので本当に良い答えはできませんが、 'String text = r.text();'代わりに。 –