From 1b8a2cd45d6f27f9e2616a59cd4d66277ac03854 Mon Sep 17 00:00:00 2001 From: liwen Date: Thu, 16 May 2024 10:55:42 +0800 Subject: [PATCH] =?UTF-8?q?update:=20=E5=AF=BC=E5=87=BA=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/hnac/hzims/safeproduct/utils/BaseUtil.java | 89 ++++++---------------- 1 file changed, 22 insertions(+), 67 deletions(-) diff --git a/hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/utils/BaseUtil.java b/hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/utils/BaseUtil.java index 0789275..3c84c29 100644 --- a/hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/utils/BaseUtil.java +++ b/hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/utils/BaseUtil.java @@ -6,9 +6,8 @@ import com.hnac.hzims.safeproduct.constants.SafeProductConstant; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.IOUtils; import org.apache.poi.xwpf.usermodel.*; -import com.alibaba.fastjson.JSONObject; -import com.alibaba.fastjson.JSONArray; -//import org.json.JSONObject; +import org.json.JSONArray; +import org.json.JSONObject; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHyperlink; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText; @@ -116,77 +115,20 @@ public class BaseUtil { * @param params 数据参数 * @return 文件 */ -// 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()) { -// for (int index = 0; index < p.getRuns().size(); index++) { -// XWPFRun run = p.getRuns().get(index); -// String text = run.getText(0); -// if (findText.equals(text)) { -// run.setText("", 0); // 清除原有文本 -// XWPFParagraph paragraph = run.getParagraph(); -// for (int i = 0; i < jsonArray.length(); i++) { -// JSONObject jsonObject = jsonArray.getJSONObject(i); -// String name = jsonObject.getString("name"); -// String url = jsonObject.getString("url"); -// // url编码格式化 -// String encodeName = URLEncoder.encode(name, "UTF-8").replaceAll("\\+", "%20"); -// String prefixString = url.substring(0, url.lastIndexOf("/") + 1); -// String encodeUrl = prefixString + encodeName; -// String id = paragraph.getDocument().getPackagePart().addExternalRelationship(encodeUrl, -// 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 < jsonArray.length() - 1) { -// paragraph.createRun().addBreak(); -// } -// } -// } -// } -// } -// } -// } -// } -// } -// } -// if (templatePath.equals("template/卫生自查表.docx")) { -// formatHygieneRecordTable(xwpfDocument); -// } -// } catch (Exception e) { -// log.error("数据写入异常: {}",e.getCause().toString()+ e.getStackTrace()); -// e.printStackTrace(); -// } -// return xwpfDocument; -// } - - public static XWPFDocument fillDocument(String templatePath, Map params, String findName) throws IOException { + public static XWPFDocument fillDocument(String templatePath, Map params, String findName) { XWPFDocument xwpfDocument = null; try { xwpfDocument = WordExportUtil.exportWord07(templatePath, params); if (findName != null && !findName.isEmpty()) { String findText = params.get(findName).toString(); if (findText != null && !findText.isEmpty()) { - JSONArray jsonArray = JSONArray.parseArray(findText); + JSONArray jsonArray = new JSONArray(findText); for (XWPFTable table : xwpfDocument.getTables()) { for (XWPFTableRow row : table.getRows()) { for (XWPFTableCell cell : row.getTableCells()) { for (XWPFParagraph p : cell.getParagraphs()) { - for (XWPFRun run : p.getRuns()) { + for (int index = 0; index < p.getRuns().size(); index++) { + XWPFRun run = p.getRuns().get(index); String text = run.getText(0); if (findText.equals(text)) { run.setText("", 0); // 清除原有文本 @@ -200,6 +142,9 @@ public class BaseUtil { } } } + if (templatePath.equals("template/卫生自查表.docx")) { + formatHygieneRecordTable(xwpfDocument); + } } catch (Exception e) { e.printStackTrace(); } @@ -208,7 +153,7 @@ public class BaseUtil { private static void insertJsonArrayIntoParagraph(JSONArray jsonArray, XWPFParagraph paragraph) throws IOException { - for (int i = 0; i < jsonArray.size(); i++) { + for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String name = jsonObject.getString("name"); String url = jsonObject.getString("url"); @@ -216,8 +161,18 @@ public class BaseUtil { String encodeName = URLEncoder.encode(name, "UTF-8").replaceAll("\\+", "%20"); String prefixString = url.substring(0, url.lastIndexOf("/") + 1); String encodeUrl = prefixString + encodeName; - XWPFRun newRun = paragraph.createRun(); - newRun.setText(encodeUrl); + String id = paragraph.getDocument().getPackagePart().addExternalRelationship(encodeUrl, + 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 < jsonArray.length() - 1) { + paragraph.createRun().addBreak(); + } } } /**