diff --git a/hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/service/impl/CarCheckRecordServiceImpl.java b/hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/service/impl/CarCheckRecordServiceImpl.java index 6c55284..3330e59 100644 --- a/hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/service/impl/CarCheckRecordServiceImpl.java +++ b/hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/service/impl/CarCheckRecordServiceImpl.java @@ -208,7 +208,7 @@ public class CarCheckRecordServiceImpl extends ServiceImpl params, String wordPath, String pdfPath, HttpServletResponse response) { + public static void exportDocument(String templateFile, Map params, String wordPath, String pdfPath,String findName, HttpServletResponse response) { try { List res = new ArrayList<>(); - XWPFDocument document = BaseUtil.fillDocument(templateFile, params); + XWPFDocument document = BaseUtil.fillDocument(templateFile, params,findName); res.add(document); BaseUtil.exportWord(res, wordPath); } catch (Exception e) { @@ -112,15 +114,55 @@ public class BaseUtil { * @param params 数据参数 * @return 文件 */ - public static XWPFDocument fillDocument(String templatePath, Map params) { + public static XWPFDocument fillDocument(String templatePath, Map params, String findName) { XWPFDocument xwpfDocument = null; try { xwpfDocument = WordExportUtil.exportWord07(templatePath, params); + if (StringUtils.isNotEmpty(findName)) { + String findText = params.get(findName).toString(); + if (StringUtils.isNotEmpty(findText)) { + JSONArray jsonArray = new JSONArray(findText); + for (XWPFTable table : xwpfDocument.getTables()) { + for (int row = 0; row < table.getNumberOfRows(); row++) { + for (int col = 0; col < table.getRow(row).getTableCells().size(); col++) { + XWPFTableCell cell = table.getRow(row).getCell(col); + for (XWPFParagraph p : cell.getParagraphs()) { + Iterator iterator = p.getRuns().iterator(); + while (iterator.hasNext()) { + XWPFRun r = iterator.next(); + String text = r.getText(0); + if (findText.equals(text)) { + r.setText("", 0); // 清除原有文本 + XWPFParagraph paragraph = r.getParagraph(); + for (int i = 0; i < jsonArray.length(); i++) { + JSONObject jsonObject = jsonArray.getJSONObject(i); + String name = jsonObject.getString("name"); + String url = jsonObject.getString("url"); + String id = paragraph.getDocument().getPackagePart().addExternalRelationship(url, XWPFRelation.HYPERLINK.getRelation()).getId(); + CTHyperlink cLink = paragraph.getCTP().addNewHyperlink(); + cLink.setId(id); + CTText ctText = CTText.Factory.newInstance(); + ctText.setStringValue(name); + CTR ctr = CTR.Factory.newInstance(); + ctr.setTArray(new CTText[]{ctText}); + cLink.setRArray(new CTR[]{ctr}); + if(i