Apache POI를 사용하여 템플릿에서 docx 파일을 생성하고 있습니다. 단락의 모든 텍스트를 대체하는 분명한 방법이없는 것 같고 문서가 거의 없다. 지금은 단락을 반복하면서 각 단락의 실행을 반복 한 다음 각 실행의 텍스트를 반복하여 문서를 읽을 수 있습니다 ... 이것은 잘 작동하며 실행중인 텍스트의 내용을 바꿀 수는 있지만 템플릿 자리 표시 자 (예 : <>)는 여러 번 실행으로 나뉠 수 있으므로 실제로 일치하고 바꾸기가 복잡합니다. XWPFParagraph의 내용을 설정하는 방법이 있습니까? 또는 적어도 단락에서 모든 실행을 잠그고 내 자신의 실행을 만드는 방법은?Apache POI : 단락 텍스트 바꾸기
public static void main(String[] args) {
InputStream fs = null;
try {
fs = new FileInputStream("C:\\sample1.docx");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
XWPFDocument doc = null;
try {
doc = new XWPFDocument(fs);
} catch (IOException e) {
e.printStackTrace();
}
for (int i = 0; i < doc.getParagraphs().length; i++) {
XWPFParagraph paragraph = doc.getParagraphs()[i];
paragraph.getCTP().getRArray().
// This will output the paragraph's contents.
System.out.println(paragraph.getParagraphText());
for (int j = 0; j < paragraph.getCTP().getRArray().length; j++) {
CTR run = paragraph.getCTP().getRArray()[j];
for (int k = 0; k < run.getTArray().length; k++) {
CTText text = run.getTArray()[k];
// This will output the text contents
System.out.println(text.getStringValue());
// And this will set its contents
text.setStringValue("Success!");
}
}
}
try {
doc.write(new FileOutputStream("C:\\output.docx"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}