From cc8e609a594ffbdeda038a787b8a104a573463d6 Mon Sep 17 00:00:00 2001 From: ty <1577900710@qq.com> Date: Wed, 15 May 2024 10:57:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=9A=E5=AF=BC=E5=87=BA?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E7=B1=BB=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/hnac/hzims/safeproduct/utils/BaseUtil.java | 119 ++++++++++++++------- 1 file changed, 83 insertions(+), 36 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 7a4cbca..0789275 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,8 +6,9 @@ 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 org.json.JSONArray; -import org.json.JSONObject; +import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.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; @@ -115,46 +116,82 @@ public class BaseUtil { * @param params 数据参数 * @return 文件 */ - public static XWPFDocument fillDocument(String templatePath, Map params, String findName) { +// 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 { XWPFDocument xwpfDocument = null; try { xwpfDocument = WordExportUtil.exportWord07(templatePath, params); - if (StringUtils.isNotEmpty(findName)) { + if (findName != null && !findName.isEmpty()) { String findText = params.get(findName).toString(); - if (StringUtils.isNotEmpty(findText)) { - JSONArray jsonArray = new JSONArray(findText); + if (findText != null && !findText.isEmpty()) { + JSONArray jsonArray = JSONArray.parseArray(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 (XWPFTableRow row : table.getRows()) { + for (XWPFTableCell cell : row.getTableCells()) { for (XWPFParagraph p : cell.getParagraphs()) { - for (int index = 0; index < p.getRuns().size(); index++) { - XWPFRun run = p.getRuns().get(index); + for (XWPFRun run : p.getRuns()) { 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(); - } - } + insertJsonArrayIntoParagraph(jsonArray, paragraph); } } } @@ -163,16 +200,26 @@ public class BaseUtil { } } } - if (templatePath.equals("template/卫生自查表.docx")) { - formatHygieneRecordTable(xwpfDocument); - } } catch (Exception e) { - log.error("数据写入异常: {}",e.getCause().toString()+ e.getStackTrace()); e.printStackTrace(); } return xwpfDocument; } + + private static void insertJsonArrayIntoParagraph(JSONArray jsonArray, XWPFParagraph paragraph) throws IOException { + for (int i = 0; i < jsonArray.size(); 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; + XWPFRun newRun = paragraph.createRun(); + newRun.setText(encodeUrl); + } + } /** * 卫生自查表格式处理 * @param xwpfDocument word文件类