2026-06-04 789ee31d8548162f2b511460e6b1c1e436d00e68
src/main/java/com/ruoyi/quality/utils/QualityInspectTemplateExportHelper.java
@@ -1,7 +1,10 @@
package com.ruoyi.quality.utils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.HackLoopTableRenderPolicy;
import com.ruoyi.quality.mapper.QualityInspectMapper;
import com.ruoyi.quality.pojo.QualityInspect;
import com.ruoyi.quality.pojo.QualityInspectParam;
@@ -22,7 +25,7 @@
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
@@ -47,7 +50,54 @@
    }
    public void exportBaiShi(HttpServletResponse response, Long inspectId) {
        export(response, inspectId, BAISHI_TEMPLATE, "百事模版检验结果");
        if (inspectId == null) {
            throw new ServiceException("检验单ID不能为空");
        }
        QualityInspect inspect = qualityInspectMapper.selectById(inspectId);
        if (inspect == null) {
            throw new ServiceException("检验单不存在");
        }
        List<QualityInspectParam> paramList = qualityInspectParamService.list(
                Wrappers.<QualityInspectParam>lambdaQuery()
                        .eq(QualityInspectParam::getInspectId, inspectId)
                        .orderByAsc(QualityInspectParam::getId));
        int index = 1;
        for (QualityInspectParam detail : paramList) {
            detail.setIndex(index);
            index++;
        }
        try (InputStream inputStream = getClass().getResourceAsStream("/static/百事模版.docx")) {
            if (inputStream == null) {
                throw new ServiceException("模板文件不存在:/static/百事模版.docx");
            }
            Configure configure = Configure.builder()
                    .bind("paramList", new HackLoopTableRenderPolicy())
                    .build();
            XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render(
                    new HashMap<String, Object>() {{
                        put("inspect", inspect);
                        put("paramList", paramList);
                    }});
            response.reset();
            response.setContentType("application/msword");
            response.setCharacterEncoding("UTF-8");
            String encodedName = URLEncoder.encode("百事模版检验结果", StandardCharsets.UTF_8).replace("+", "%20");
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.setHeader("Content-Disposition", "attachment;filename=" + encodedName + ".docx");
            try (OutputStream outputStream = response.getOutputStream()) {
                template.write(outputStream);
                outputStream.flush();
            }
        } catch (IOException e) {
            throw new RuntimeException("导出失败", e);
        }
    }
    public void exportDaLi(HttpServletResponse response, Long inspectId) {