1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
| import org.apache.poi.xwpf.usermodel.*;
import java.util.HashMap; import java.util.List; import java.util.Map;
public class WordUtil {
public static void replaceInAllParagraphs(List<XWPFParagraph> xwpfParagraphList, Map<String, String> params) { for (XWPFParagraph paragraph : xwpfParagraphList) { if (paragraph.getText() == null paragraph.getText().equals("")) { continue; } for (String key : params.keySet()) { if (paragraph.getText().contains(key)) { replaceInParagraph(paragraph, key, params.get(key)); } } } }
public static void replaceInParagraph(XWPFParagraph xwpfParagraph, String oldString, String newString) { Map<String, Integer> pos_map = findSubRunPosInParagraph(xwpfParagraph, oldString); if (pos_map != null) { List<XWPFRun> runs = xwpfParagraph.getRuns(); XWPFRun modelRun = runs.get(pos_map.get("end_pos")); XWPFRun xwpfRun = xwpfParagraph.insertNewRun(pos_map.get("end_pos") + 1); xwpfRun.setText(newString); if (modelRun.getFontSize() != -1) { xwpfRun.setFontSize(modelRun.getFontSize()); } xwpfRun.setFontFamily(modelRun.getFontFamily()); for (int i = pos_map.get("end_pos"); i >= pos_map.get("start_pos"); i--) { xwpfParagraph.removeRun(i); } } }
public static Map<String, Integer> findSubRunPosInParagraph(XWPFParagraph xwpfParagraph, String substring) { List<XWPFRun> runs = xwpfParagraph.getRuns(); int start_pos = 0; int end_pos = 0; String subtemp = ""; for (int i = 0; i < runs.size(); i++) { subtemp = ""; start_pos = i; for (int j = i; j < runs.size(); j++) { if (runs.get(j).getText(runs.get(j).getTextPosition()) == null) { continue; } subtemp += runs.get(j).getText(runs.get(j).getTextPosition()); if (subtemp.equals(substring)) { end_pos = j; Map<String, Integer> map = new HashMap<>(); map.put("start_pos", start_pos); map.put("end_pos", end_pos); return map; } } } return null; }
public static void replaceInTables(List<XWPFTable> xwpfTableList, Map<String, String> params) { for (XWPFTable table : xwpfTableList) { replaceInTable(table, params); } }
public static void replaceInTable(XWPFTable xwpfTable, Map<String, String> params) { List<XWPFTableRow> rows = xwpfTable.getRows(); replaceInRows(rows, params); }
public static void replaceInRows(List<XWPFTableRow> rows, Map<String, String> params) { for (int i = 0; i < rows.size(); i++) { XWPFTableRow row = rows.get(i); replaceInCells(row.getTableCells(), params); } }
public static void replaceInCells(List<XWPFTableCell> xwpfTableCellList, Map<String, String> params) { for (XWPFTableCell cell : xwpfTableCellList) { replaceInCell(cell, params);
} }
public static void replaceInCell(XWPFTableCell cell, Map<String, String> params) { List<XWPFParagraph> cellParagraphs = cell.getParagraphs(); replaceInAllParagraphs(cellParagraphs, params); } }
|