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