替换单个table
private void changeTableMessage(Map<String, String> params, XWPFTable table, boolean isBold, Integer fontSize) {
int count = table.getNumberOfRows();//获取table的行数
for (int i = 0; i < count; i++) {
XWPFTableRow row = table.getRow(i);
List<XWPFTableCell> cells = row.getTableCells();
for (XWPFTableCell cell : cells) {//遍历每行的值并进行替换
for (Map.Entry<String, String> e : params.entrySet()) {
if (cell.getText().equals(e.getKey())) {
XWPFParagraph newPara = new XWPFParagraph(cell.getCTTc().addNewP(), cell);
XWPFRun r1 = newPara.createRun();
r1.setBold(isBold);
if (fontSize != null) {
r1.setFontSize(fontSize);
}
r1.setText(e.getValue());
cell.removeParagraph(0);
cell.setParagraph(newPara);
}
}
}
}
}
替换多个table
private void changeMessage(Map<String, String> params, XWPFDocument document) {
Iterator<XWPFTable> itTable = document.getTablesIterator();
while (itTable.hasNext()) {
XWPFTable table = (XWPFTable) itTable.next();
changeTableMessage(params, table, false, null);
}
}