import org.apache.poi.POIXMLDocument; import org.apache.poi.xwpf.usermodel.*; import org.openxmlformats.schemas.wordprocessingml.x2006.main.*; import java.io.FileOutputStream; import java.math.BigInteger; import java.util.*; public class DOCWriter { public static void searchAndReplace(String srcPath, String destPath, Map<String, String> map, List<String[]> list) { try { XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage(srcPath)); // 替换段落中的指定文字 Iterator<XWPFParagraph> itPara = document.getParagraphsIterator(); while (itPara.hasNext()) { XWPFParagraph paragraph = (XWPFParagraph) itPara.next(); //String s = paragraph.getParagraphText(); Set<String> set = map.keySet(); Iterator<String> iterator = set.iterator(); while (iterator.hasNext()) { String key = iterator.next(); List<XWPFRun> run = paragraph.getRuns(); for(int i = 0; i < run.size(); i++) { if(run.get(i).getText(run.get(i).getTextPosition()) != null && run.get(i).getText(run.get(i).getTextPosition()).equals(key)) { /**参数0表示生成的文字是要从哪一个地方开始放置,设置文字从位置0开始 * 就可以把原来的文字全部替换掉了 * */ run.get(i).setText(map.get(key),0); } } } } // 获取表格数量 List<XWPFTable> tables = document.getTables(); for (int i = 0; i < tables.size(); i++) { XWPFTable table = tables.get(i); int rows = table.getNumberOfRows(); // 替换表格中的特殊字段 for (int k = 0; k < rows; k++) { XWPFTableRow row = table.getRow(k); List<XWPFTableCell> cells = row.getTableCells(); for (XWPFTableCell cell : cells) { for (Map.Entry<String, String> e : map.entrySet()) { if (cell.getText().equals(e.getKey())) { cell.removeParagraph(0); cell.setText(e.getValue()); } } } } CTTbl ctTbl = table.getCTTbl(); CTTblPr ctTblPr = ctTbl.getTblPr() == null ? ctTbl.addNewTblPr() : ctTbl.getTblPr(); CTTblWidth ctTblWidth = ctTblPr.isSetTblW() ? ctTblPr.getTblW() : ctTblPr.addNewTblW(); CTJc ctJc = ctTblPr.addNewJc(); ctJc.setVal(STJc.Enum.forString("center")); ctTblWidth.setW(new BigInteger("8000")); ctTblWidth.setType(STTblWidth.DXA); // 第三个表格为动态表格 if (list != null && list.size() > 0 && i == 2) { for (int m = 0; m < list.size(); m++) { XWPFTableRow tableRow = table.getRow(m); for (int n = 0; n < 6; n++) { XWPFTableCell cell02 = tableRow.getCell(n); cell02.setText(list.get(m)[n]); } } } } FileOutputStream outStream = new FileOutputStream(destPath); document.write(outStream); outStream.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { Map<String, String> map = new HashMap<>(); map.put("${no}", "684364643146346434"); map.put("${loanname}", "ejsdf"); map.put("${cardName}", "684364643146346434"); map.put("${bankName}", "湖湘财富"); String srcPath = "F:\\湖湘财富借款协议(借款人版本)最终版2.0.docx"; String destPath = "D:\\2.doc"; List<String[]> list01 = new ArrayList<>(); list01.add(new String[]{"A","美女好看", "sdfew", "234234", "sdfr4", "123"}); list01.add(new String[]{"A","美女好看", "sdfew", "234234", "sdfr4", "123"}); list01.add(new String[]{"A","美女好看", "sdfew", "234234", "sdfr4", "123"}); list01.add(new String[]{"A","美女好看", "sdfew", "234234", "sdfr4", "123"}); searchAndReplace(srcPath, destPath, map, list01); } }
使用POI替换word中的特定字符/文字
最新推荐文章于 2025-05-13 15:38:51 发布