Browse Source

update: 导出功能代码调整

zhongwei
liwen 7 months ago
parent
commit
ecd2ed1a7c
  1. 56
      hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/utils/BaseUtil.java

56
hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/utils/BaseUtil.java

@ -119,14 +119,13 @@ public class BaseUtil {
XWPFDocument xwpfDocument = null; XWPFDocument xwpfDocument = null;
try { try {
xwpfDocument = WordExportUtil.exportWord07(templatePath, params); xwpfDocument = WordExportUtil.exportWord07(templatePath, params);
if (StringUtils.isNotEmpty(findName)) { if (findName != null && !findName.isEmpty()) {
String findText = params.get(findName).toString(); String findText = params.get(findName).toString();
if (StringUtils.isNotEmpty(findText)) { if (findText != null && !findText.isEmpty()) {
JSONArray jsonArray = new JSONArray(findText); JSONArray jsonArray = new JSONArray(findText);
for (XWPFTable table : xwpfDocument.getTables()) { for (XWPFTable table : xwpfDocument.getTables()) {
for (int row = 0; row < table.getNumberOfRows(); row++) { for (XWPFTableRow row : table.getRows()) {
for (int col = 0; col < table.getRow(row).getTableCells().size(); col++) { for (XWPFTableCell cell : row.getTableCells()) {
XWPFTableCell cell = table.getRow(row).getCell(col);
for (XWPFParagraph p : cell.getParagraphs()) { for (XWPFParagraph p : cell.getParagraphs()) {
for (int index = 0; index < p.getRuns().size(); index++) { for (int index = 0; index < p.getRuns().size(); index++) {
XWPFRun run = p.getRuns().get(index); XWPFRun run = p.getRuns().get(index);
@ -134,27 +133,7 @@ public class BaseUtil {
if (findText.equals(text)) { if (findText.equals(text)) {
run.setText("", 0); // 清除原有文本 run.setText("", 0); // 清除原有文本
XWPFParagraph paragraph = run.getParagraph(); XWPFParagraph paragraph = run.getParagraph();
for (int i = 0; i < jsonArray.length(); i++) { insertJsonArrayIntoParagraph(jsonArray, paragraph);
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();
}
}
} }
} }
} }
@ -167,12 +146,35 @@ public class BaseUtil {
formatHygieneRecordTable(xwpfDocument); formatHygieneRecordTable(xwpfDocument);
} }
} catch (Exception e) { } catch (Exception e) {
log.error("数据写入异常: {}",e.getCause().toString()+ e.getStackTrace());
e.printStackTrace(); e.printStackTrace();
} }
return xwpfDocument; return xwpfDocument;
} }
private static void insertJsonArrayIntoParagraph(JSONArray jsonArray, XWPFParagraph paragraph) throws IOException {
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();
}
}
}
/** /**
* 卫生自查表格式处理 * 卫生自查表格式处理
* @param xwpfDocument word文件类 * @param xwpfDocument word文件类

Loading…
Cancel
Save