web检测报告导出修改

This commit is contained in:
cwchen 2024-09-28 15:29:52 +08:00
parent f8fb10700c
commit e3742a8db3
5 changed files with 607 additions and 302 deletions

View File

@ -123,4 +123,22 @@ public interface TestReportManageDao {
* @return * @return
*/ */
TestReportManageDto downloadCertificateList(TestReportManageDto data); TestReportManageDto downloadCertificateList(TestReportManageDto data);
/**
* 获取基本信息
* @param id
* @return Map<String,Object>
* @author cwchen
* @date 2024/9/28 14:40
*/
Map<String, String> getBasicInfoMap(Long id);
/**
* 获取签名图片
* @param id
* @return List<String>
* @author cwchen
* @date 2024/9/28 15:09
*/
List<String> getSignImgs(Long id);
} }

View File

@ -24,16 +24,12 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.File; import java.io.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
import java.io.ByteArrayOutputStream;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
/** /**
@ -48,6 +44,9 @@ public class TestReportManageServiceImpl implements TestReportManageService {
@Resource @Resource
private TestReportManageDao testReportManageDao; private TestReportManageDao testReportManageDao;
@Resource(name = "CreateSheetUtil")
private CreateSheetUtil createSheetUtil;
/** /**
* 查询列表 * 查询列表
* @param bean * @param bean
@ -238,6 +237,7 @@ public class TestReportManageServiceImpl implements TestReportManageService {
* 生成检测报告 * 生成检测报告
*/ */
public void generateReport(TestReportManageDto bean,ZipOutputStream zos) throws IOException { public void generateReport(TestReportManageDto bean,ZipOutputStream zos) throws IOException {
try {
//先计算出总共有多少列和动态生成的有多少列 //先计算出总共有多少列和动态生成的有多少列
//总列数 //总列数
int totalNum = 0; int totalNum = 0;
@ -258,13 +258,39 @@ public class TestReportManageServiceImpl implements TestReportManageService {
List<ExperDevVo> devList=bean.getExperDevVos(); List<ExperDevVo> devList=bean.getExperDevVos();
XSSFWorkbook workbook = new XSSFWorkbook(); XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("检测报告"); // Sheet sheet = workbook.createSheet("检测报告");
/*******处理检测报告start******/
// 模板xlsx文件
XSSFWorkbook wb = null;
InputStream inputSteam = createSheetUtil.getInputSteam();
if(inputSteam!=null){
wb = new XSSFWorkbook(inputSteam);
}
Sheet oneSheet = null,twoSheet = null,sheet = null,fourSheet = null;
if(wb!=null){
sheet = wb.createSheet();
wb.removeSheetAt(2);
wb.setSheetName(3, bean.getSampleTools());
wb.cloneSheet(2);
wb.removeSheetAt(2);
wb.setSheetName(3, "封底");
createSheetUtil.handleOneSheetData(wb.getSheetAt(0),bean);
createSheetUtil.handleTwoSheetData(wb,wb.getSheetAt(1),bean);
}else{
oneSheet = workbook.createSheet();
twoSheet = workbook.createSheet();
sheet = workbook.createSheet();
fourSheet = workbook.createSheet();
workbook.setSheetName(0, "封页1");
workbook.setSheetName(1, "封页2");
workbook.setSheetName(2, bean.getSampleTools());
workbook.setSheetName(3, "封底");
}
/*******处理检测报告end******/
// 创建样式 // 创建样式
XSSFCellStyle style = workbook.createCellStyle(); XSSFCellStyle style = wb !=null?wb.createCellStyle():workbook.createCellStyle();
style.setBorderTop(BorderStyle.THIN); style.setBorderTop(BorderStyle.THIN);
style.setBorderBottom(BorderStyle.THIN); style.setBorderBottom(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN); style.setBorderLeft(BorderStyle.THIN);
@ -341,7 +367,6 @@ public class TestReportManageServiceImpl implements TestReportManageService {
applyBordersToMergedRegion(sheet, new CellRangeAddress(2,2,1,totalNum-1), style); applyBordersToMergedRegion(sheet, new CellRangeAddress(2,2,1,totalNum-1), style);
//第四行 //第四行
Row row4= sheet.createRow(3); Row row4= sheet.createRow(3);
@ -396,7 +421,7 @@ public class TestReportManageServiceImpl implements TestReportManageService {
//第五第六号非固定数据 //第五第六号非固定数据
AtomicInteger num= new AtomicInteger(5); AtomicInteger num= new AtomicInteger(5);
AtomicInteger rowNums= new AtomicInteger(5); AtomicInteger rowNums= new AtomicInteger(5);
configItemsVos.forEach(vo->{ for (ConfigItemsVo vo : configItemsVos) {
List<ConfigItemVo> childList=vo.getItemList(); List<ConfigItemVo> childList=vo.getItemList();
int rowNum=childList.size(); int rowNum=childList.size();
num.set(num.get() + 1); num.set(num.get() + 1);
@ -413,14 +438,14 @@ public class TestReportManageServiceImpl implements TestReportManageService {
cell4.setCellStyle(style); cell4.setCellStyle(style);
cell4.setCellValue(child.getItemName()); cell4.setCellValue(child.getItemName());
}); });
}); }
AtomicInteger row = new AtomicInteger(5); AtomicInteger row = new AtomicInteger(5);
AtomicInteger cellNum = new AtomicInteger(0); AtomicInteger cellNum = new AtomicInteger(0);
AtomicInteger serialNumber = new AtomicInteger(1); // 用于追踪序号 AtomicInteger serialNumber = new AtomicInteger(1); // 用于追踪序号
if (ListHelper.isNotEmpty(devList)) { if (ListHelper.isNotEmpty(devList)) {
devList.forEach(dev -> { for (ExperDevVo dev : devList) {
row.set(row.get() + 1); row.set(row.get() + 1);
cellNum.set(0); // 每次处理新行时重置 cellNum cellNum.set(0); // 每次处理新行时重置 cellNum
@ -470,7 +495,7 @@ public class TestReportManageServiceImpl implements TestReportManageService {
row77.setCellStyle(style); row77.setCellStyle(style);
cellNum.set(cellNum.get() + 1); cellNum.set(cellNum.get() + 1);
}); });
}); }
} }
// //
row.set(row.get() + 1); row.set(row.get() + 1);
@ -546,7 +571,11 @@ public class TestReportManageServiceImpl implements TestReportManageService {
// 保存到 ZIP 文件 // 保存到 ZIP 文件
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
if(wb!=null){
wb.write(baos);
}else{
workbook.write(baos); workbook.write(baos);
}
if (StringHelper.isNotEmpty(bean.getReportCode())){ if (StringHelper.isNotEmpty(bean.getReportCode())){
//将字符串中ML/BG-去除 //将字符串中ML/BG-去除
bean.setReportCode(bean.getReportCode().replace("ML/BG-","")); bean.setReportCode(bean.getReportCode().replace("ML/BG-",""));
@ -555,7 +584,14 @@ public class TestReportManageServiceImpl implements TestReportManageService {
zos.putNextEntry(entry); zos.putNextEntry(entry);
zos.write(baos.toByteArray()); zos.write(baos.toByteArray());
zos.closeEntry(); zos.closeEntry();
if(wb!=null){
wb.close();
}else{
workbook.close(); workbook.close();
}
} catch (IOException e) {
log.error(e.toString(),e);
}
} }

View File

@ -0,0 +1,212 @@
package com.bonus.aqgqj.utils;
import com.bonus.aqgqj.app.entity.TestReportManageAppVo;
import com.bonus.aqgqj.basis.dao.TestReportManageDao;
import com.bonus.aqgqj.basis.entity.dto.TestReportManageDto;
import com.bonus.aqgqj.basis.entity.vo.ExperBasisVo;
import com.bonus.aqgqj.basis.entity.vo.TestVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.repository.init.ResourceReader;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* @className:CreateSheetUtil
* @author:cwchen
* @date:2024-09-27-13:32
* @version:1.0
* @description:创建sheet页工具类
*/
@Component(value = "CreateSheetUtil")
@Slf4j
public class CreateSheetUtil {
@Resource
private TestReportManageDao testReportManageDao;
@Value("${file.linux.upload_path}")
private String uploadPath;
public InputStream getInputSteam() {
// 使用ClassLoader读取资源文件
InputStream inputStream = null;
try {
inputStream = this.getClass().getClassLoader().getResourceAsStream("temple/model_excel.xlsx");
// 确保inputStream不为null
if (inputStream != null) {
return inputStream;
}
} catch (Exception e) {
log.error(e.toString(), e);
}
return null;
}
public void handleOneSheetData(Sheet sheet, TestReportManageDto vo) {
Cell cell = sheet.getRow(2).getCell(0);
Cell cell2 = sheet.getRow(9).getCell(2);
Cell cell3 = sheet.getRow(13).getCell(2);
cell.setCellValue("");
cell2.setCellValue(vo.getSampleTools());
cell3.setCellValue("");
// System.err.println("数据:" + cell.getStringCellValue());
/*for (Row row : sheet) {
for (Cell cell : row) {
System.err.println(row.getRowNum()+"列:"+cell.getColumnIndex()+"数据:"+cell.getStringCellValue());
}
}*/
}
public void handleTwoSheetData(XSSFWorkbook workbook, Sheet sheet, TestReportManageDto vo) {
for (Row row : sheet) {
for (Cell cells : row) {
System.err.println(row.getRowNum() + "列:" + cells.getColumnIndex() + "数据:" + cells.getStringCellValue());
}
}
Map<String, String> dataMap = getBasicData(Long.parseLong(vo.getId() + ""));
// 委托单位
Cell cell = sheet.getRow(8).getCell(2);
cell.setCellValue(handleValue(dataMap.get("customName")));
// 委托单位地址
Cell cell2 = sheet.getRow(9).getCell(2);
cell2.setCellValue("");
// 样品名称
Cell cell3 = sheet.getRow(11).getCell(2);
cell3.setCellValue(vo.getSampleTools());
// 样品特性和状态
Cell cell4 = sheet.getRow(13).getCell(6);
// 到样日期
Cell cell5 = sheet.getRow(14).getCell(2);
cell5.setCellValue(vo.getCollectSamplesTime().replaceAll("-", "."));
// 检测日期
Cell cell6 = sheet.getRow(14).getCell(6);
cell6.setCellValue(vo.getExperTime().replaceAll("-", "."));
// 样品数量
Cell cell7 = sheet.getRow(15).getCell(6);
cell7.setCellValue(vo.getDevNum() + "");
// 检测依据
Cell cell8 = sheet.getRow(16).getCell(2);
cell8.setCellValue(handleBasis(vo));
// 检测结论 审核人
Cell cell9 = sheet.getRow(17).getCell(2);
cell9.setCellValue(cell9.getStringCellValue().replace("该样品按DL/T 976-2017标准所检项目合格。", handleValue(vo.getExperConclu())));
handlePicture(workbook, sheet, sheet.getRow(18), sheet.getRow(18).getCell(3), handleValue(dataMap.get("pzUrl")));
// 备注
Cell cell10 = sheet.getRow(19).getCell(2);
cell10.setCellValue(handleValue(vo.getRemarks()));
// 主检人 编制人 审核人
Cell cell11 = sheet.getRow(21).getCell(2);
Cell cell12 = sheet.getRow(21).getCell(4);
Cell cell13 = sheet.getRow(21).getCell(6);
handlePicture(workbook, sheet, sheet.getRow(21), cell11, handleValue(dataMap.get("zjUrl")));
handlePicture(workbook, sheet, sheet.getRow(21), cell12, handleValue(dataMap.get("bzUrl")));
handlePicture(workbook, sheet, sheet.getRow(21), cell13, handleValue(dataMap.get("shUrl")));
}
/**
* 设置检测依据值
*
* @param dto
* @return String
* @author cwchen
* @date 2024/9/27 18:12
*/
public static String handleBasis(TestReportManageDto dto) {
List<ExperBasisVo> basisVos = dto.getBasisVos();
if (CollectionUtils.isNotEmpty(basisVos)) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < basisVos.size(); i++) {
ExperBasisVo basisVo = basisVos.get(i);
if (i == basisVos.size() - 1) {
sb.append((i + 1)).append("").append(basisVo.getBasisName());
} else {
sb.append((i + 1)).append("").append(basisVo.getBasisName()).append("\n");
}
}
return sb.toString();
}
return "";
}
public void handlePicture(XSSFWorkbook workbook, Sheet sheet, Row row, Cell cell, String filePath) {
if (!new File(uploadPath + filePath).exists()) {
return;
}
try {
// 创建一个图片单元格的引用
CreationHelper createHelper = workbook.getCreationHelper();
ClientAnchor anchor = createHelper.createClientAnchor();
// 图片从哪一列开始
anchor.setCol1(cell.getColumnIndex());
// 图片从哪一行开始
anchor.setRow1(row.getRowNum());
// 图片从哪一列结束
anchor.setCol2(cell.getColumnIndex() + 1);
// 图片从哪一行结束
anchor.setRow2(row.getRowNum() + 1);
// 读取图片
InputStream inputStream = Files.newInputStream(Paths.get(uploadPath + filePath));
byte[] bytes = IOUtils.toByteArray(inputStream);
// 将图片添加到工作簿中
int pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
Drawing<?> drawing = sheet.createDrawingPatriarch();
Picture picture = drawing.createPicture(anchor, pictureIdx);
picture.resize(1, 1);
} catch (IOException e) {
log.error(e.toString(), e);
}
}
/**
* 获取基本信息
*
* @param id
* @return Map<String, Object>
* @author cwchen
* @date 2024/9/28 14:40
*/
public Map<String, String> getBasicData(Long id) {
Map<String, String> mapData = null;
try {
mapData = testReportManageDao.getBasicInfoMap(id);
List<String> urlList = testReportManageDao.getSignImgs(id);
mapData.put("pzUrl", urlList.get(1));
mapData.put("zjUrl", urlList.get(2));
mapData.put("bzUrl", urlList.get(2));
mapData.put("shUrl", urlList.get(0));
} catch (Exception e) {
log.error(e.toString(), e);
}
return mapData;
}
/**
* 空值处理
*
* @param value
* @return String
* @author cwchen
* @date 2024/9/28 14:39
*/
public String handleValue(String value) {
if (StringUtils.isEmpty(value)) {
return "";
}
return value;
}
}

View File

@ -276,4 +276,43 @@
GROUP BY ted.exper_id GROUP BY ted.exper_id
LIMIT 1 LIMIT 1
</select> </select>
<!--获取基本信息-->
<select id="getBasicInfoMap" resultType="java.util.Map">
SELECT te.id AS id,
tc.custom_name AS customName
FROM tb_exper te
LEFT JOIN tb_sample ts ON te.sample_id = ts.id
LEFT JOIN tb_custom tc ON ts.custom_id = tc.id
WHERE te.id = #{id}
LIMIT 1
</select>
<!--获取签名图片 审核人 审批人 主检人-->
<select id="getSignImgs" resultType="java.lang.String">
(
SELECT IFNULL(su.sign_img,'')
FROM tb_audit_history tah
LEFT JOIN sys_user su ON tah.audit_user_id = su.id
WHERE tah.exper_id = #{id} AND tah.audit_status = 1 AND tah.audit_type = 2
ORDER BY audit_time DESC
LIMIT 1
)
UNION ALL
(
SELECT IFNULL(su.sign_img,'')
FROM tb_audit_history tah
LEFT JOIN sys_user su ON tah.audit_user_id = su.id
WHERE tah.exper_id = #{id} AND tah.audit_status = 1 AND tah.audit_type = 3
ORDER BY audit_time DESC
LIMIT 1
)
UNION ALL
(
SELECT IFNULL(su.sign_img,'')
FROM tb_exper te
LEFT JOIN sys_user su ON te.update_user = su.id
WHERE te.id = #{id} AND te.del_flag = 0
ORDER BY audit_time DESC
LIMIT 1
)
</select>
</mapper> </mapper>

Binary file not shown.