Browse Source

fix: 解决导出文件的超链接有多个时报错的问题

zhongwei
liwen 6 months ago
parent
commit
c56363f902
  1. 5
      hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/service/impl/RehearsalRecordServiceImpl.java
  2. 14
      hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/utils/BaseUtil.java

5
hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/service/impl/RehearsalRecordServiceImpl.java

@ -161,7 +161,10 @@ public class RehearsalRecordServiceImpl extends ServiceImpl<RehearsalRecordMappe
} }
} else { } else {
// 添加空的ImageEntity,防止数据填入时显示"{{" // 添加空的ImageEntity,防止数据填入时显示"{{"
list.add(new ImageEntity()); ImageEntity imageEntity = new ImageEntity();
// 设置空字符串,防止空指针报错
imageEntity.setUrl("");
list.add(imageEntity);
} }
rehearsalRecordDetailVO.setImages(list); rehearsalRecordDetailVO.setImages(list);
// 记录:换行符使用\r\n // 记录:换行符使用\r\n

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

@ -128,18 +128,16 @@ public class BaseUtil {
for (int col = 0; col < table.getRow(row).getTableCells().size(); col++) { for (int col = 0; col < table.getRow(row).getTableCells().size(); col++) {
XWPFTableCell cell = table.getRow(row).getCell(col); XWPFTableCell cell = table.getRow(row).getCell(col);
for (XWPFParagraph p : cell.getParagraphs()) { for (XWPFParagraph p : cell.getParagraphs()) {
Iterator<XWPFRun> iterator = p.getRuns().iterator(); for (int index = 0; index < p.getRuns().size(); index++) {
while (iterator.hasNext()) { XWPFRun run = p.getRuns().get(index);
XWPFRun r = iterator.next(); String text = run.getText(0);
String text = r.getText(0);
if (findText.equals(text)) { if (findText.equals(text)) {
r.setText("", 0); // 清除原有文本 run.setText("", 0); // 清除原有文本
XWPFParagraph paragraph = r.getParagraph(); XWPFParagraph paragraph = run.getParagraph();
for (int i = 0; i < jsonArray.length(); i++) { for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i); JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.getString("name"); String name = jsonObject.getString("name");
String url = jsonObject.getString("url"); String url = jsonObject.getString("url");
url = URLEncoder.encode(url, "UTF-8");
String id = paragraph.getDocument().getPackagePart().addExternalRelationship(url, XWPFRelation.HYPERLINK.getRelation()).getId(); String id = paragraph.getDocument().getPackagePart().addExternalRelationship(url, XWPFRelation.HYPERLINK.getRelation()).getId();
CTHyperlink cLink = paragraph.getCTP().addNewHyperlink(); CTHyperlink cLink = paragraph.getCTP().addNewHyperlink();
cLink.setId(id); cLink.setId(id);
@ -148,7 +146,7 @@ public class BaseUtil {
CTR ctr = CTR.Factory.newInstance(); CTR ctr = CTR.Factory.newInstance();
ctr.setTArray(new CTText[]{ctText}); ctr.setTArray(new CTText[]{ctText});
cLink.setRArray(new CTR[]{ctr}); cLink.setRArray(new CTR[]{ctr});
if (i<jsonArray.length()-1) { if (i < jsonArray.length() - 1) {
paragraph.createRun().addBreak(); paragraph.createRun().addBreak();
} }
} }

Loading…
Cancel
Save