From 61e5bac7dbdac1c761b424b187e6cdd170c17a3a Mon Sep 17 00:00:00 2001 From: yuance <182865460@qq.com> Date: Tue, 4 Nov 2025 16:24:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=B8=89=E9=87=8D=E4=B8=80?= =?UTF-8?q?=E5=A4=A7=E3=80=81=E9=87=8D=E5=A4=A7=E7=BB=8F=E6=B5=8E=E5=86=B3?= =?UTF-8?q?=E7=AD=96=E8=B0=83=E6=9F=A5=E8=A1=A8=E5=AF=BC=E5=87=BA=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gxwebsoft/ai/config/AuditExcelStyle.java | 120 +++++++++++++++ .../controller/AuditContent3Controller.java | 128 ++++++++++++++-- .../dto/export/DecisionTableExportEntity.java | 40 +++++ .../ai/dto/export/TripleOneExportEntity.java | 34 +++++ .../gxwebsoft/ai/utils/ExcelExportTool.java | 139 ++++++++++++++++++ 5 files changed, 452 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/gxwebsoft/ai/config/AuditExcelStyle.java create mode 100644 src/main/java/com/gxwebsoft/ai/dto/export/DecisionTableExportEntity.java create mode 100644 src/main/java/com/gxwebsoft/ai/dto/export/TripleOneExportEntity.java create mode 100644 src/main/java/com/gxwebsoft/ai/utils/ExcelExportTool.java diff --git a/src/main/java/com/gxwebsoft/ai/config/AuditExcelStyle.java b/src/main/java/com/gxwebsoft/ai/config/AuditExcelStyle.java new file mode 100644 index 0000000..cfe4535 --- /dev/null +++ b/src/main/java/com/gxwebsoft/ai/config/AuditExcelStyle.java @@ -0,0 +1,120 @@ +package com.gxwebsoft.ai.config; + +import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; +import cn.afterturn.easypoi.excel.entity.params.ExcelForEachParams; +import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler; +import org.apache.poi.ss.usermodel.*; + +/** + * 自定义Excel导出样式 + */ +public class AuditExcelStyle implements IExcelExportStyler { + + private Workbook workbook; + + public AuditExcelStyle(Workbook workbook) { + this.workbook = workbook; + } + + @Override + public CellStyle getHeaderStyle(short color) { + CellStyle style = workbook.createCellStyle(); + + // 设置边框 + style.setBorderBottom(BorderStyle.THIN); + style.setBorderLeft(BorderStyle.THIN); + style.setBorderRight(BorderStyle.THIN); + style.setBorderTop(BorderStyle.THIN); + + // 设置背景色 - 浅蓝色 + style.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex()); + style.setFillPattern(FillPatternType.SOLID_FOREGROUND); + + // 设置字体 + Font font = workbook.createFont(); + font.setBold(true); + font.setFontHeightInPoints((short) 11); + font.setColor(IndexedColors.BLACK.getIndex()); + style.setFont(font); + + // 居中对齐 + style.setAlignment(HorizontalAlignment.CENTER); + style.setVerticalAlignment(VerticalAlignment.CENTER); + + return style; + } + + @Override + public CellStyle getTitleStyle(short color) { + CellStyle style = workbook.createCellStyle(); + + // 设置边框 + style.setBorderBottom(BorderStyle.MEDIUM); + style.setBorderLeft(BorderStyle.MEDIUM); + style.setBorderRight(BorderStyle.MEDIUM); + style.setBorderTop(BorderStyle.MEDIUM); + + // 设置背景色 - 深蓝色 + style.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex()); + style.setFillPattern(FillPatternType.SOLID_FOREGROUND); + + // 设置字体 + Font font = workbook.createFont(); + font.setBold(true); + font.setFontHeightInPoints((short) 14); + font.setColor(IndexedColors.BLACK.getIndex()); + style.setFont(font); + + // 居中对齐 + style.setAlignment(HorizontalAlignment.CENTER); + style.setVerticalAlignment(VerticalAlignment.CENTER); + + return style; + } + + @Override + public CellStyle getStyles(boolean noneStyler, ExcelExportEntity entity) { + CellStyle style = workbook.createCellStyle(); + + // 设置边框 + style.setBorderBottom(BorderStyle.THIN); + style.setBorderLeft(BorderStyle.THIN); + style.setBorderRight(BorderStyle.THIN); + style.setBorderTop(BorderStyle.THIN); + + // 设置字体 + Font font = workbook.createFont(); + font.setFontHeightInPoints((short) 10); + style.setFont(font); + + // 自动换行 + style.setWrapText(true); + + // 垂直居中 + style.setVerticalAlignment(VerticalAlignment.CENTER); + + // 根据列名设置不同的水平对齐方式 + if (entity != null && entity.getName() != null) { + switch (entity.getName()) { + case "类别": + case "测试结果": + style.setAlignment(HorizontalAlignment.CENTER); + break; + default: + style.setAlignment(HorizontalAlignment.LEFT); + } + } + + return style; + } + + @Override + public CellStyle getStyles(Cell cell, int dataRow, ExcelExportEntity entity, Object obj, Object data) { + return getStyles(false, entity); + } + + @Override + public CellStyle getTemplateStyles(boolean isSingle, ExcelForEachParams excelForEachParams) { + return getStyles(false, null); + } +} \ No newline at end of file diff --git a/src/main/java/com/gxwebsoft/ai/controller/AuditContent3Controller.java b/src/main/java/com/gxwebsoft/ai/controller/AuditContent3Controller.java index e906400..f08b956 100644 --- a/src/main/java/com/gxwebsoft/ai/controller/AuditContent3Controller.java +++ b/src/main/java/com/gxwebsoft/ai/controller/AuditContent3Controller.java @@ -3,29 +3,29 @@ package com.gxwebsoft.ai.controller; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.gxwebsoft.ai.dto.AuditContentRequest; +import com.gxwebsoft.ai.dto.export.TripleOneExportEntity; +import com.gxwebsoft.ai.dto.export.DecisionTableExportEntity; import com.gxwebsoft.ai.entity.AiCloudDoc; import com.gxwebsoft.ai.entity.AiCloudFile; +import com.gxwebsoft.ai.utils.ExcelExportTool; import com.gxwebsoft.common.core.web.ApiResult; import com.gxwebsoft.common.core.web.BaseController; import com.gxwebsoft.common.system.entity.User; - -import cn.hutool.core.util.StrUtil; -import lombok.extern.slf4j.Slf4j; - import com.gxwebsoft.ai.service.AiCloudDocService; import com.gxwebsoft.ai.service.AiCloudFileService; import com.gxwebsoft.ai.service.AuditContent3TripleService; import com.gxwebsoft.ai.service.AuditContent3DecisionService; import com.gxwebsoft.ai.service.KnowledgeBaseService; - import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; - -import java.util.*; -import java.util.stream.Collectors; - +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import cn.hutool.core.util.StrUtil; + +import javax.servlet.http.HttpServletResponse; +import java.util.*; +import java.util.stream.Collectors; /** * 审计内容3控制器 - 三重一大制度对比分析 & 重大经济决策调查表 @@ -166,4 +166,114 @@ public class AuditContent3Controller extends BaseController { } } } + + /** + * 导出三重一大制度对比分析表到Excel + */ + @Operation(summary = "导出三重一大制度对比分析表到Excel") + @PostMapping("/exportTripleOneTable") + public void exportTripleOneTable(@RequestBody Map request, HttpServletResponse response) { + List> dataList = (List>) request.get("data"); + String companyName = (String) request.get("companyName"); + + // 转换为实体列表 + List exportData = convertToTripleOneEntityList(dataList); + + // 使用工具类导出 + String fileName = "三重一大制度对比分析表_" + (companyName != null ? companyName : "未知公司"); + String title = companyName != null ? companyName + " - 三重一大制度对比分析表" : "三重一大制度对比分析表"; + + ExcelExportTool.exportExcel(exportData, TripleOneExportEntity.class, fileName, "三重一大制度对比分析表", title, response); + } + + /** + * 导出重大经济决策调查表到Excel + */ + @Operation(summary = "导出重大经济决策调查表到Excel") + @PostMapping("/exportDecisionTable") + public void exportDecisionTable(@RequestBody Map request, HttpServletResponse response) { + List> dataList = (List>) request.get("data"); + String companyName = (String) request.get("companyName"); + + // 转换为实体列表 + List exportData = convertToDecisionTableEntityList(dataList); + + // 使用工具类导出 + String fileName = "重大经济决策调查表_" + (companyName != null ? companyName : "未知公司"); + String title = companyName != null ? companyName + " - 重大经济决策调查表" : "重大经济决策调查表"; + + ExcelExportTool.exportExcel(exportData, DecisionTableExportEntity.class, fileName, "重大经济决策调查表", title, response); + } + + /** + * 转换为三重一大实体列表 + */ + private List convertToTripleOneEntityList(List> originalData) { + return originalData.stream().map(this::convertToTripleOneEntity).collect(Collectors.toList()); + } + + /** + * 转换为重大经济决策调查表实体列表 + */ + private List convertToDecisionTableEntityList(List> originalData) { + return originalData.stream().map(this::convertToDecisionTableEntity).collect(Collectors.toList()); + } + + /** + * 单个Map转换为三重一大实体 + */ + private TripleOneExportEntity convertToTripleOneEntity(Map item) { + TripleOneExportEntity entity = new TripleOneExportEntity(); + entity.setCategory(getStringValue(item, "category")); + entity.setPolicyContent(getStringValue(item, "policyContent")); + entity.setGroupSystem(getStringValue(item, "groupSystem")); + entity.setCompanyFormulation(getStringValue(item, "companyFormulation")); + entity.setCheckEvidence(getStringValue(item, "checkEvidence")); + entity.setTestResult(getStringValue(item, "testResult")); + entity.setWorkPaperIndex(formatWorkPaperIndex(item.get("workPaperIndex"))); + return entity; + } + + /** + * 单个Map转换为重大经济决策调查表实体 + */ + private DecisionTableExportEntity convertToDecisionTableEntity(Map item) { + DecisionTableExportEntity entity = new DecisionTableExportEntity(); + entity.setIndex(getStringValue(item, "index")); + entity.setDecisionItem(getStringValue(item, "name")); + entity.setMeetingTime(getStringValue(item, "meetingTime")); + entity.setDecisionAmount(getStringValue(item, "decisionAmount")); + entity.setProcedure(getStringValue(item, "procedure")); + entity.setExecutionStatus(getStringValue(item, "executionStatus")); + entity.setGood(getStringValue(item, "goods")); + entity.setNormal(getStringValue(item, "normal")); + entity.setBad(getStringValue(item, "bad")); + return entity; + } + + /** + * 安全获取字符串值 + */ + private String getStringValue(Map map, String key) { + Object value = map.get(key); + return value != null ? value.toString() : ""; + } + + /** + * 格式化工作底稿索引(如果是数组则转换为字符串) + */ + private String formatWorkPaperIndex(Object workPaperIndex) { + if (workPaperIndex == null) { + return ""; + } + + if (workPaperIndex instanceof List) { + List list = (List) workPaperIndex; + return String.join(", ", list.stream() + .map(Object::toString) + .collect(Collectors.toList())); + } + + return workPaperIndex.toString(); + } } \ No newline at end of file diff --git a/src/main/java/com/gxwebsoft/ai/dto/export/DecisionTableExportEntity.java b/src/main/java/com/gxwebsoft/ai/dto/export/DecisionTableExportEntity.java new file mode 100644 index 0000000..57b440e --- /dev/null +++ b/src/main/java/com/gxwebsoft/ai/dto/export/DecisionTableExportEntity.java @@ -0,0 +1,40 @@ +package com.gxwebsoft.ai.dto.export; + +import cn.afterturn.easypoi.excel.annotation.Excel; +import cn.afterturn.easypoi.excel.annotation.ExcelTarget; +import lombok.Data; + +/** + * 重大经济决策调查表导出实体 + */ +@Data +@ExcelTarget("DecisionTableExportEntity") +public class DecisionTableExportEntity { + + @Excel(name = "序号", orderNum = "1", width = 8) + private String index; + + @Excel(name = "重大经济决策事项", orderNum = "2", width = 30) + private String decisionItem; + + @Excel(name = "会议时间", orderNum = "3", width = 15) + private String meetingTime; + + @Excel(name = "决策事项金额", orderNum = "4", width = 15) + private String decisionAmount; + + @Excel(name = "程序", orderNum = "5", width = 20) + private String procedure; + + @Excel(name = "执行情况(是/否)", orderNum = "6", width = 15) + private String executionStatus; + + @Excel(name = "好", orderNum = "7", width = 8) + private String good; + + @Excel(name = "一般", orderNum = "8", width = 8) + private String normal; + + @Excel(name = "差", orderNum = "9", width = 8) + private String bad; +} \ No newline at end of file diff --git a/src/main/java/com/gxwebsoft/ai/dto/export/TripleOneExportEntity.java b/src/main/java/com/gxwebsoft/ai/dto/export/TripleOneExportEntity.java new file mode 100644 index 0000000..27a998d --- /dev/null +++ b/src/main/java/com/gxwebsoft/ai/dto/export/TripleOneExportEntity.java @@ -0,0 +1,34 @@ +package com.gxwebsoft.ai.dto.export; + +import cn.afterturn.easypoi.excel.annotation.Excel; +import cn.afterturn.easypoi.excel.annotation.ExcelTarget; +import lombok.Data; + +/** + * 三重一大制度对比分析表导出实体 + */ +@Data +@ExcelTarget("TripleOneExportEntity") +public class TripleOneExportEntity { + + @Excel(name = "类别", orderNum = "1", width = 15) + private String category; + + @Excel(name = "政策内容", orderNum = "2", width = 40) + private String policyContent; + + @Excel(name = "集团制度", orderNum = "3", width = 30) + private String groupSystem; + + @Excel(name = "公司制度", orderNum = "4", width = 30) + private String companyFormulation; + + @Excel(name = "检查的证据及测试内容", orderNum = "5", width = 25) + private String checkEvidence; + + @Excel(name = "测试结果", orderNum = "6", width = 10) + private String testResult; + + @Excel(name = "工作底稿索引", orderNum = "7", width = 20) + private String workPaperIndex; +} \ No newline at end of file diff --git a/src/main/java/com/gxwebsoft/ai/utils/ExcelExportTool.java b/src/main/java/com/gxwebsoft/ai/utils/ExcelExportTool.java new file mode 100644 index 0000000..1185fd8 --- /dev/null +++ b/src/main/java/com/gxwebsoft/ai/utils/ExcelExportTool.java @@ -0,0 +1,139 @@ +package com.gxwebsoft.ai.utils; + +import cn.afterturn.easypoi.excel.ExcelExportUtil; +import cn.afterturn.easypoi.excel.entity.ExportParams; +import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; +import com.gxwebsoft.ai.config.AuditExcelStyle; +import lombok.extern.slf4j.Slf4j; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; + +import javax.servlet.http.HttpServletResponse; +import java.io.OutputStream; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.util.List; + +/** + * Excel导出工具类 + */ +@Slf4j +public class ExcelExportTool { + + /** + * 通用Excel导出方法 + * + * @param dataList 数据列表 + * @param entityClass 实体类Class + * @param fileName 文件名(不含扩展名) + * @param sheetName 工作表名 + * @param title 标题 + * @param response HttpServletResponse + */ + public static void exportExcel(List dataList, + Class entityClass, + String fileName, + String sheetName, + String title, + HttpServletResponse response) { + exportExcel(dataList, entityClass, fileName, sheetName, title, response, true); + } + + /** + * 通用Excel导出方法(完整参数) + * + * @param dataList 数据列表 + * @param entityClass 实体类Class + * @param fileName 文件名(不含扩展名) + * @param sheetName 工作表名 + * @param title 标题 + * @param response HttpServletResponse + * @param autoHeight 是否自适应行高 + */ + public static void exportExcel(List dataList, + Class entityClass, + String fileName, + String sheetName, + String title, + HttpServletResponse response, + boolean autoHeight) { + try { + if (dataList == null || dataList.isEmpty()) { + throw new RuntimeException("没有可导出的数据"); + } + + log.info("开始导出Excel - 文件: {}, 数据条数: {}", fileName, dataList.size()); + + // 设置响应头 + setupResponse(response, fileName); + + // 创建导出参数 + ExportParams exportParams = createExportParams(sheetName, title); + + // 创建Workbook并导出 + Workbook workbook = ExcelExportUtil.exportExcel(exportParams, entityClass, dataList); + + // 应用样式 + if (autoHeight) { + applyAutoStyle(workbook); + } + + // 写入响应流 + writeToResponse(workbook, response); + + log.info("成功导出Excel文件: {}, 共{}条数据", fileName, dataList.size()); + + } catch (Exception e) { + log.error("导出Excel失败: {}", fileName, e); + throw new RuntimeException("导出失败: " + e.getMessage()); + } + } + + /** + * 设置响应头 + */ + private static void setupResponse(HttpServletResponse response, String fileName) throws Exception { + response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); + String encodedFileName = URLEncoder.encode(fileName + ".xlsx", StandardCharsets.UTF_8.toString()); + response.setHeader("Content-Disposition", "attachment; filename=" + encodedFileName); + response.setCharacterEncoding(StandardCharsets.UTF_8.toString()); + } + + /** + * 创建导出参数 + */ + private static ExportParams createExportParams(String sheetName, String title) { + ExportParams exportParams = new ExportParams(); + exportParams.setSheetName(sheetName); + exportParams.setTitle(title); + exportParams.setType(ExcelType.XSSF); // 使用xlsx格式 + exportParams.setStyle(AuditExcelStyle.class); + return exportParams; + } + + /** + * 应用自动样式 + */ + private static void applyAutoStyle(Workbook workbook) { + Sheet sheet = workbook.getSheetAt(0); + + // 设置自适应行高 + for (int i = 1; i <= sheet.getLastRowNum(); i++) { + Row row = sheet.getRow(i); + if (row != null) { + row.setHeight((short) -1); // 设置为自动行高 + } + } + } + + /** + * 写入响应流 + */ + private static void writeToResponse(Workbook workbook, HttpServletResponse response) throws Exception { + try (OutputStream out = response.getOutputStream()) { + workbook.write(out); + out.flush(); + } + } +} \ No newline at end of file