Przeglądaj źródła

```
refactor(AppOrderController, AppOrderServiceImpl, IAppOrderService):优化导出条件功能并返回字节数组

- 修改 `AppOrderController` 中的 `exportCondition` 方法,使其返回 `ResponseEntity<byte[]>` 而不是 `Result<File>`。
- 在 `AppOrderController` 中添加了 `HttpHeaders`、`HttpStatus` 和 `MediaType` 的导入,并在 `exportCondition` 方法中设置了响应头和内容类型。
- 修改 `AppOrderServiceImpl` 中的 `exportCondition` 方法,使其返回 `byte[]` 而不是 `File`。- 修改 `generateExcel` 方法,使其生成 Excel 文件并返回字节数组,而不是创建临时文件。- 更新 `IAppOrderService` 接口中的 `exportCondition` 方法签名,使其返回 `byte[]`。```

SheepHy 5 dni temu
rodzic
commit
cc286ab73d

+ 19 - 2
national-motion-module-system/national-motion-system-biz/src/main/java/org/jeecg/modules/system/app/controller/AppOrderController.java

@@ -19,12 +19,17 @@ import org.jeecg.modules.system.app.vo.AppOrderInfoVO;
 import org.jeecg.modules.system.app.vo.ExportConditionVO;
 import org.jeecg.modules.system.app.vo.OrderPageVO;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.servlet.ModelAndView;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.File;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.util.Arrays;
 import java.util.List;
@@ -230,7 +235,19 @@ public class AppOrderController extends JeecgController<AppOrder, IAppOrderServi
      **/
     @Operation(summary="赛事条件导出")
     @PostMapping(value = "/exportCondition")
-    public Result<File> exportCondition(@RequestBody ExportConditionDTO exportConditionDTO) throws IOException {
-        return Result.OK(appOrderService.exportCondition(exportConditionDTO));
+    public ResponseEntity<byte[]> exportCondition(@RequestBody ExportConditionDTO exportConditionDTO) throws IOException {
+        try {
+            byte[] excelBytes = appOrderService.exportCondition(exportConditionDTO);
+
+            HttpHeaders headers = new HttpHeaders();
+            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
+            headers.setContentDispositionFormData("attachment", "export_condition.xlsx");
+            headers.setContentLength(excelBytes.length);
+
+            return new ResponseEntity<>(excelBytes, headers, HttpStatus.OK);
+        } catch (Exception e) {
+            log.error("导出Excel文件失败", e);
+            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
+        }
     }
 }

+ 2 - 1
national-motion-module-system/national-motion-system-biz/src/main/java/org/jeecg/modules/system/app/service/IAppOrderService.java

@@ -15,6 +15,7 @@ import org.jeecg.modules.system.app.vo.ExportConditionVO;
 import org.jeecg.modules.system.app.vo.OrderPageVO;
 
 import java.io.File;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.time.LocalDate;
 import java.time.LocalTime;
@@ -94,5 +95,5 @@ public interface IAppOrderService extends IService<AppOrder> {
      * @date 2025/9/17
      * return   {@link }
      **/
-    File exportCondition(ExportConditionDTO exportConditionDTO) throws IOException;
+    byte[] exportCondition(ExportConditionDTO exportConditionDTO) throws IOException;
 }

+ 107 - 95
national-motion-module-system/national-motion-system-biz/src/main/java/org/jeecg/modules/system/app/service/impl/AppOrderServiceImpl.java

@@ -430,7 +430,7 @@ public class AppOrderServiceImpl extends ServiceImpl<AppOrderMapper, AppOrder> i
     }
 
     @Override
-    public File exportCondition(ExportConditionDTO exportConditionDTO) throws IOException {
+    public byte[] exportCondition(ExportConditionDTO exportConditionDTO) throws IOException {
         List<ExportConditionOutVO> exportConditionOutVO = appGameMapper.exportCondition(exportConditionDTO);
         List<Map<String, Object>> list = new ArrayList<>();
         int index = 1;
@@ -475,114 +475,126 @@ public class AppOrderServiceImpl extends ServiceImpl<AppOrderMapper, AppOrder> i
             index++;
         }
 
-        // 生成 Excel 文件
+        // 生成 Excel 文件并返回字节数组
         return generateExcel(list);
     }
 
-    // 优化后的 generateExcel 方法
-    private File generateExcel(List<Map<String, Object>> dataList) throws IOException {
-        // 创建工作簿和工作表
-        Workbook workbook = new XSSFWorkbook();
-        Sheet sheet = workbook.createSheet("报名信息");
-
-        // 设置列宽
-        sheet.setColumnWidth(0, 3000);  // 序号
-        sheet.setColumnWidth(1, 8000);  // 赛事名称
-        sheet.setColumnWidth(2, 6000);  // 个人/团队赛
-        sheet.setColumnWidth(3, 6000);  // 报名项目
-        sheet.setColumnWidth(4, 6000);  // 队名
-        sheet.setColumnWidth(5, 6000);  // 队徽
-        sheet.setColumnWidth(6, 10000); // 社保证明
-        sheet.setColumnWidth(7, 4000);  // 报名人员
-        sheet.setColumnWidth(8, 4000);  // 联系电话
-
-        // 创建标题样式
-        CellStyle headerStyle = createHeaderStyle(workbook);
-
-        // 创建内容样式
-        CellStyle contentStyle = createContentStyle(workbook);
-
-        // 写入表头
-        Row headerRow = sheet.createRow(0);
-        String[] headers = {"序号", "赛事名称", "个人/团队赛", "报名项目", "队名", "队徽", "社保证明", "报名人员", "联系电话"};
-        for (int i = 0; i < headers.length; i++) {
-            Cell cell = headerRow.createCell(i);
-            cell.setCellValue(headers[i]);
-            cell.setCellStyle(headerStyle);
-        }
+    // 修改 generateExcel 方法返回字节数组
+    private byte[] generateExcel(List<Map<String, Object>> dataList) throws IOException {
+        Workbook workbook = null;
 
-        // 写入数据
-        for (int i = 0; i < dataList.size(); i++) {
-            Map<String, Object> rowMap = dataList.get(i);
-            Row dataRow = sheet.createRow(i + 1);
-            // 设置行高,为图片留出空间
-            dataRow.setHeightInPoints(80);
-
-            // 序号
-            Cell cell0 = dataRow.createCell(0);
-            cell0.setCellValue((Integer) rowMap.get("序号"));
-            cell0.setCellStyle(contentStyle);
-
-            // 赛事名称
-            Cell cell1 = dataRow.createCell(1);
-            cell1.setCellValue((String) rowMap.get("赛事名称"));
-            cell1.setCellStyle(contentStyle);
-
-            // 个人/团队赛
-            Cell cell2 = dataRow.createCell(2);
-            cell2.setCellValue((int) rowMap.get("个人/团队赛") == 3 ? "个人" : "团队");
-            cell2.setCellStyle(contentStyle);
-
-            // 报名项目
-            Cell cell3 = dataRow.createCell(3);
-            cell3.setCellValue((String) rowMap.get("报名项目"));
-            cell3.setCellStyle(contentStyle);
-
-            // 队名
-            Cell cell4 = dataRow.createCell(4);
-            cell4.setCellValue((String) rowMap.get("队名"));
-            cell4.setCellStyle(contentStyle);
-
-            // 队徽:插入图片(嵌入单元格)
-            String teamEmblemImg = (String) rowMap.get("队徽");
-            Cell cell5 = dataRow.createCell(5);
-            cell5.setCellStyle(contentStyle);
-            if (teamEmblemImg != null && !teamEmblemImg.isEmpty()) {
-                insertImageToCell(workbook, sheet, teamEmblemImg, 5, i + 1);
+        try {
+            // 创建工作簿和工作表
+            workbook = new XSSFWorkbook();
+            Sheet sheet = workbook.createSheet("报名信息");
+
+            // 设置列宽
+            sheet.setColumnWidth(0, 3000);  // 序号
+            sheet.setColumnWidth(1, 8000);  // 赛事名称
+            sheet.setColumnWidth(2, 6000);  // 个人/团队赛
+            sheet.setColumnWidth(3, 6000);  // 报名项目
+            sheet.setColumnWidth(4, 6000);  // 队名
+            sheet.setColumnWidth(5, 6000);  // 队徽
+            sheet.setColumnWidth(6, 10000); // 社保证明
+            sheet.setColumnWidth(7, 4000);  // 报名人员
+            sheet.setColumnWidth(8, 4000);  // 联系电话
+
+            // 创建标题样式
+            CellStyle headerStyle = createHeaderStyle(workbook);
+
+            // 创建内容样式
+            CellStyle contentStyle = createContentStyle(workbook);
+
+            // 写入表头
+            Row headerRow = sheet.createRow(0);
+            String[] headers = {"序号", "赛事名称", "个人/团队赛", "报名项目", "队名", "队徽", "社保证明", "报名人员", "联系电话"};
+            for (int i = 0; i < headers.length; i++) {
+                Cell cell = headerRow.createCell(i);
+                cell.setCellValue(headers[i]);
+                cell.setCellStyle(headerStyle);
             }
 
-            // 社保证明:多个图片,放在同一单元格内,按行排列
-            String certificationStr = (String) rowMap.get("社保证明");
-            Cell cell6 = dataRow.createCell(6);
-            cell6.setCellStyle(contentStyle);
+            // 写入数据
+            for (int i = 0; i < dataList.size(); i++) {
+                Map<String, Object> rowMap = dataList.get(i);
+                Row dataRow = sheet.createRow(i + 1);
+                // 设置行高,为图片留出空间
+                dataRow.setHeightInPoints(80);
+
+                // 序号
+                Cell cell0 = dataRow.createCell(0);
+                cell0.setCellValue((Integer) rowMap.get("序号"));
+                cell0.setCellStyle(contentStyle);
+
+                // 赛事名称
+                Cell cell1 = dataRow.createCell(1);
+                cell1.setCellValue((String) rowMap.get("赛事名称"));
+                cell1.setCellStyle(contentStyle);
+
+                // 个人/团队赛
+                Cell cell2 = dataRow.createCell(2);
+                cell2.setCellValue((int) rowMap.get("个人/团队赛") == 3 ? "个人" : "团队");
+                cell2.setCellStyle(contentStyle);
+
+                // 报名项目
+                Cell cell3 = dataRow.createCell(3);
+                cell3.setCellValue((String) rowMap.get("报名项目"));
+                cell3.setCellStyle(contentStyle);
+
+                // 队名
+                Cell cell4 = dataRow.createCell(4);
+                cell4.setCellValue((String) rowMap.get("队名"));
+                cell4.setCellStyle(contentStyle);
+
+                // 队徽:插入图片(嵌入单元格)
+                String teamEmblemImg = (String) rowMap.get("队徽");
+                Cell cell5 = dataRow.createCell(5);
+                cell5.setCellStyle(contentStyle);
+                if (teamEmblemImg != null && !teamEmblemImg.isEmpty()) {
+                    insertImageToCell(workbook, sheet, teamEmblemImg, 5, i + 1);
+                }
+
+                // 社保证明:多个图片,放在同一单元格内,按行排列
+                String certificationStr = (String) rowMap.get("社保证明");
+                Cell cell6 = dataRow.createCell(6);
+                cell6.setCellStyle(contentStyle);
 
-            if (certificationStr != null && !certificationStr.isEmpty()) {
-                List<String> allImageUrls = extractImageUrls(certificationStr);
-                if (!allImageUrls.isEmpty()) {
-                    insertMultipleImagesToCell(workbook, sheet, allImageUrls, 6, i + 1);
+                if (certificationStr != null && !certificationStr.isEmpty()) {
+                    List<String> allImageUrls = extractImageUrls(certificationStr);
+                    if (!allImageUrls.isEmpty()) {
+                        insertMultipleImagesToCell(workbook, sheet, allImageUrls, 6, i + 1);
+                    }
                 }
-            }
 
-            // 报名人员
-            Cell cell7 = dataRow.createCell(7);
-            cell7.setCellValue((String) rowMap.get("报名人员"));
-            cell7.setCellStyle(contentStyle);
+                // 报名人员
+                Cell cell7 = dataRow.createCell(7);
+                cell7.setCellValue((String) rowMap.get("报名人员"));
+                cell7.setCellStyle(contentStyle);
 
-            // 联系电话
-            Cell cell8 = dataRow.createCell(8);
-            cell8.setCellValue((String) rowMap.get("联系电话"));
-            cell8.setCellStyle(contentStyle);
-        }
+                // 联系电话
+                Cell cell8 = dataRow.createCell(8);
+                cell8.setCellValue((String) rowMap.get("联系电话"));
+                cell8.setCellStyle(contentStyle);
+            }
 
-        // 输出文件
-        File file = File.createTempFile("export_condition_", ".xlsx");
-        try (FileOutputStream fos = new FileOutputStream(file)) {
-            workbook.write(fos);
+            // 将工作簿写入字节数组
+            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+            workbook.write(outputStream);
+            return outputStream.toByteArray();
+
+        } finally {
+            // 关闭工作簿
+            if (workbook != null) {
+                try {
+                    workbook.close();
+                } catch (IOException e) {
+                    System.err.println("关闭工作簿时出错: " + e.getMessage());
+                }
+            }
         }
-        workbook.close();
-        return file;
     }
 
+
     // 创建表头样式
     private CellStyle createHeaderStyle(Workbook workbook) {
         CellStyle headerStyle = workbook.createCellStyle();