Browse Source

fix: 解决导出文件无图片时报错的问题

zhongwei
liwen 6 months ago
parent
commit
86147df422
  1. 16
      hzims-service-api/safeproduct-api/src/main/java/com/hnac/hzims/safeproduct/vo/RehearsalRecordDetailImgVO.java
  2. 15
      hzims-service-api/safeproduct-api/src/main/java/com/hnac/hzims/safeproduct/vo/RehearsalRecordDetailStrVO.java
  3. 3
      hzims-service-api/safeproduct-api/src/main/java/com/hnac/hzims/safeproduct/vo/RehearsalRecordDetailVO.java
  4. 43
      hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/service/impl/RehearsalRecordServiceImpl.java
  5. 33
      hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/utils/BaseUtil.java
  6. BIN
      hzims-service/safeproduct/src/main/resources/template/演练记录表.docx

16
hzims-service-api/safeproduct-api/src/main/java/com/hnac/hzims/safeproduct/vo/RehearsalRecordDetailImgVO.java

@ -0,0 +1,16 @@
package com.hnac.hzims.safeproduct.vo;
import cn.afterturn.easypoi.entity.ImageEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;
@Data
@ApiModel(value = "演练记录表VO类")
public class RehearsalRecordDetailImgVO extends RehearsalRecordDetailVO{
@ApiModelProperty("图片列表")
private List<ImageEntity> images;
}

15
hzims-service-api/safeproduct-api/src/main/java/com/hnac/hzims/safeproduct/vo/RehearsalRecordDetailStrVO.java

@ -0,0 +1,15 @@
package com.hnac.hzims.safeproduct.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;
@Data
@ApiModel(value = "演练记录表VO类")
public class RehearsalRecordDetailStrVO extends RehearsalRecordDetailVO{
@ApiModelProperty("图片列表")
private List<String> images;
}

3
hzims-service-api/safeproduct-api/src/main/java/com/hnac/hzims/safeproduct/vo/RehearsalRecordDetailVO.java

@ -55,9 +55,6 @@ public class RehearsalRecordDetailVO {
@ApiModelProperty("演练科目")
private String subject;
@ApiModelProperty("图片列表")
private List<ImageEntity> images;
@ApiModelProperty("年份")
private String year;

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

@ -19,10 +19,13 @@ import com.hnac.hzims.safeproduct.mapper.RehearsalRecordMapper;
import com.hnac.hzims.safeproduct.service.IRehearsalRecordService;
import com.hnac.hzims.safeproduct.utils.BaseUtil;
import com.hnac.hzims.safeproduct.utils.TimeUtils;
import com.hnac.hzims.safeproduct.vo.RehearsalRecordDetailImgVO;
import com.hnac.hzims.safeproduct.vo.RehearsalRecordDetailStrVO;
import com.hnac.hzims.safeproduct.vo.RehearsalRecordDetailVO;
import com.hnac.hzims.safeproduct.vo.RehearsalRecordlVO;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.mp.support.Query;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@ -150,23 +153,6 @@ public class RehearsalRecordServiceImpl extends ServiceImpl<RehearsalRecordMappe
if (StringUtils.isNotEmpty(rehearsalRecordDetailVO.getActualEndTime())){
rehearsalRecordDetailVO.setActualEndTime(TimeUtils.format(rehearsalRecordDetailVO.getActualEndTime()));
}
// 图片
List<ImageEntity> list = new ArrayList<>();
if (StringUtils.isNotEmpty(rehearsalRecordDetailVO.getImgPath())) {
String[] imgArr = rehearsalRecordDetailVO.getImgPath().split(",");
for (String img : imgArr) {
ImageEntity imageEntity = new ImageEntity(BaseUtil.imgToByte(savePath +
BaseUtil.downloadFileByUrl(img, savePath)), 180, 150);
list.add(imageEntity);
}
} else {
// 添加空的ImageEntity,防止数据填入时显示"{{"
ImageEntity imageEntity = new ImageEntity();
// 设置空字符串,防止空指针报错
imageEntity.setUrl("");
list.add(imageEntity);
}
rehearsalRecordDetailVO.setImages(list);
// 记录:换行符使用\r\n
String standardRecord = rehearsalRecordDetailVO.getRecord().replaceAll("\n", "\r\n");
rehearsalRecordDetailVO.setRecord(standardRecord);
@ -186,7 +172,28 @@ public class RehearsalRecordServiceImpl extends ServiceImpl<RehearsalRecordMappe
} else {
rehearsalRecordDetailVO.setRehearsalMethod(RehearsalMethodEnum.OFFLINE.getDesc());
}
Map<String, Object> params = BaseUtil.obj2Map(rehearsalRecordDetailVO);
// 图片
Map<String, Object> params;
if (StringUtils.isNotEmpty(rehearsalRecordDetailVO.getImgPath())) {
List<ImageEntity> list = new ArrayList<>();
RehearsalRecordDetailImgVO imgVO = new RehearsalRecordDetailImgVO();
BeanUtils.copyProperties(rehearsalRecordDetailVO, imgVO);
String[] imgArr = rehearsalRecordDetailVO.getImgPath().split(",");
for (String img : imgArr) {
ImageEntity imageEntity = new ImageEntity(BaseUtil.imgToByte(savePath +
BaseUtil.downloadFileByUrl(img, savePath)), 180, 150);
list.add(imageEntity);
}
imgVO.setImages(list);
params = BaseUtil.obj2Map(imgVO);
} else {
List<String> list = new ArrayList<>();
list.add("");
RehearsalRecordDetailStrVO strVO = new RehearsalRecordDetailStrVO();
BeanUtils.copyProperties(rehearsalRecordDetailVO, strVO);
strVO.setImages(list);
params = BaseUtil.obj2Map(strVO);
}
String templateFile = "template/演练记录表" + SafeProductConstant.DOCX_SUFFIX;
String wordPath = savePath + "/演练记录表" + SafeProductConstant.DOCX_SUFFIX;
String pdfPath = savePath + "/演练记录表" + SafeProductConstant.PDF_SUFFIX;

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

@ -119,6 +119,7 @@ public class BaseUtil {
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()) {
@ -142,6 +143,7 @@ public class BaseUtil {
}
}
}
// 表格处理
if (templatePath.equals("template/卫生自查表.docx")) {
formatHygieneRecordTable(xwpfDocument);
}
@ -151,6 +153,36 @@ public class BaseUtil {
return xwpfDocument;
}
/**
* 车辆检查表格式处理
* @param xwpfDocument word文件类
*/
private static void formatCarCheckRecordTable(XWPFDocument xwpfDocument) {
// 获取文档中所有的表格
List<XWPFTable> tableList = xwpfDocument.getTables();
if (tableList.size() > 0) {
// 循环表格
for (XWPFTable table : tableList) {
// 获取表格所有行数
List<XWPFTableRow> rows = table.getRows();
// 第五行到倒数第三行为需合并区域
for (int row = 5; row < rows.size() - 2;) {
XWPFTableCell startCell = rows.get(row).getCell(0);
String startText = startCell.getText();
int index = row + 1;
for (; index < rows.size(); index++) {
XWPFTableCell endCell = rows.get(index).getCell(0);
String endText = endCell.getText();
if (!startText.equals(endText)) {
break;
}
}
mergeCellsVertically(table, 0, row, index - 1);
row = index;
}
}
}
}
private static void insertJsonArrayIntoParagraph(JSONArray jsonArray, XWPFParagraph paragraph) throws IOException {
for (int i = 0; i < jsonArray.length(); i++) {
@ -175,6 +207,7 @@ public class BaseUtil {
}
}
}
/**
* 卫生自查表格式处理
* @param xwpfDocument word文件类

BIN
hzims-service/safeproduct/src/main/resources/template/演练记录表.docx

Binary file not shown.
Loading…
Cancel
Save