From ba6eba202110af165366377168d0561d5d222d7c Mon Sep 17 00:00:00 2001
From: buhuazhen <hua100783@gmail.com>
Date: 星期五, 20 三月 2026 11:24:10 +0800
Subject: [PATCH] fix(staff): 解决导出数据列数不一致问题

---
 src/main/java/com/ruoyi/staff/controller/PersonalShiftController.java    |   42 +++++++++++++++++++++++++++---------------
 src/main/java/com/ruoyi/staff/service/impl/PersonalShiftServiceImpl.java |   13 +++++++++++++
 2 files changed, 40 insertions(+), 15 deletions(-)

diff --git a/src/main/java/com/ruoyi/staff/controller/PersonalShiftController.java b/src/main/java/com/ruoyi/staff/controller/PersonalShiftController.java
index 85ff013..130d588 100644
--- a/src/main/java/com/ruoyi/staff/controller/PersonalShiftController.java
+++ b/src/main/java/com/ruoyi/staff/controller/PersonalShiftController.java
@@ -17,6 +17,7 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
+import javax.servlet.ServletOutputStream;
 import javax.servlet.http.HttpServletResponse;
 import javax.validation.constraints.NotNull;
 import java.util.Collection;
@@ -67,31 +68,42 @@
     @ApiOperation(value = "瀵煎嚭")
     @GetMapping("export")
     public void exportToExcel(@NotNull(message = "鏃堕棿涓嶈兘涓虹┖锛�") String time, String userName, Integer sysDeptId, Boolean isMonth, HttpServletResponse response) throws Exception {
-        Map<Object, Object> data;
+        ServletOutputStream out = response.getOutputStream();
         response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
-        response.setHeader("requestType","excel");
-        response.setHeader("Access-Control-Expose-Headers", "requestType");
+        response.setCharacterEncoding("utf-8");
+        response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + time + ".xlsx");
+
         if (!isMonth) {
-            data = personalShiftService.exportToYearExcel(time, userName, sysDeptId);
-            // 璁剧疆鍗曞厓鏍兼牱寮�
-            HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(StyleYearUtils.getHeadStyle(), StyleYearUtils.getContentStyle());
-            // 淇濆瓨鍒扮涓�涓猻heet涓�
-            EasyExcel.write(response.getOutputStream())
+            Map<Object, Object> data = personalShiftService.exportToYearExcel(time, userName, sysDeptId);
+
+            HorizontalCellStyleStrategy style = new HorizontalCellStyleStrategy(
+                    StyleYearUtils.getHeadStyle(),
+                    StyleYearUtils.getContentStyle()
+            );
+
+            EasyExcel.write(out)
                     .head((List<List<String>>) data.get("header"))
-                    .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) // 鑷�傚簲鍒楀
-                    .registerWriteHandler(horizontalCellStyleStrategy)
+                    .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
+                    .registerWriteHandler(style)
                     .sheet("骞村害")
                     .doWrite((Collection<?>) data.get("data"));
+
         } else {
-            data = personalShiftService.exportToMonthExcel(time, userName, sysDeptId);
-            // 璁剧疆鍗曞厓鏍兼牱寮�
-            HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(StyleMonthUtils.getHeadStyle(), StyleMonthUtils.getContentStyle());
-            EasyExcel.write(response.getOutputStream())
+            Map<Object, Object> data = personalShiftService.exportToMonthExcel(time, userName, sysDeptId);
+
+            HorizontalCellStyleStrategy style = new HorizontalCellStyleStrategy(
+                    StyleMonthUtils.getHeadStyle(),
+                    StyleMonthUtils.getContentStyle()
+            );
+
+            EasyExcel.write(out)
                     .head((List<List<String>>) data.get("header"))
-                    .registerWriteHandler(horizontalCellStyleStrategy)
+                    .registerWriteHandler(style)
                     .sheet("鏈堝害")
                     .doWrite((Collection<?>) data.get("data"));
         }
+
+        out.flush();
     }
 
 
diff --git a/src/main/java/com/ruoyi/staff/service/impl/PersonalShiftServiceImpl.java b/src/main/java/com/ruoyi/staff/service/impl/PersonalShiftServiceImpl.java
index 420bb71..dc0f443 100644
--- a/src/main/java/com/ruoyi/staff/service/impl/PersonalShiftServiceImpl.java
+++ b/src/main/java/com/ruoyi/staff/service/impl/PersonalShiftServiceImpl.java
@@ -228,6 +228,9 @@
             for (String shiftTime : shiftTimes) {
                 Map<String, Object> hashMap = new HashMap<>();
                 String[] shiftTimeAndShift = shiftTime.split("锛�");
+                if(shiftTimeAndShift.length != 3){
+                    continue;
+                }
                 for (PersonalAttendanceLocationConfig personalAttendanceLocationConfig : personalAttendanceLocationConfigs) {
                     if (!i.getMonthlyAttendance().containsKey(personalAttendanceLocationConfig.getShift())) {
                         i.getMonthlyAttendance().put(personalAttendanceLocationConfig.getShift(), 0);
@@ -258,6 +261,16 @@
         LocalDateTime localDateTime = LocalDateTime.parse(time, formatters);
         map.put("header", getMonthHeader(localDateTime));
         List<List<Object>> lists = dataRequiredForProcessingIntoExcelMonth(mapIPage, personalAttendanceLocationConfigs);
+        int maxSize = lists.stream()
+                .mapToInt(List::size)
+                .max()
+                .orElse(0);
+
+        for (List<Object> row : lists) {
+            while (row.size() < maxSize) {
+                row.add("-");
+            }
+        }
         map.put("data", lists);
         return map;
     }

--
Gitblit v1.9.3