Microsoft Word Neu

Aus bestehenden Word-Dokument Text ändern:


public class ReplaceText {

public void replaceText(String a, String b) {

try {

XWPFDocument doc = new XWPFDocument(OPCPackage.open(new FileInputStream(„C:\\Users\\….docx“)));
for (XWPFTable tbl : doc.getTables()) {

for (XWPFTableRow row : tbl.getRows()) {

for (XWPFTableCell cell : row.getTableCells()) {

for (XWPFParagraph p : cell.getParagraphs()) {

for (XWPFRun r : p.getRuns()) {

String text = r.getText(0);
if (text != null && text.contains(a)) {

text = text.replace(a, b);
r.setText(text,0);

}

}

}

}

}

}

}

doc.write(new FileOutputStream(„C:\\Users\\….docx“));
doc.close();
} catch (Exception ex) {
ex.printStackTrace();
}

}

}