zouyu
2026-04-20 1702e168020f36627c7de0b145e7e4f67a0fb2e1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.ruoyi.performance.excel.handler.performance;
 
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.handler.context.SheetWriteHandlerContext;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
 
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.List;
 
/**
 * 班次导出动态表头处理器
 */
public class PerformanceShiftHeaderWriteHandler implements SheetWriteHandler {
 
    private static final int TITLE_ROW_INDEX = 1;
 
    private static final int WEEK_ROW_INDEX = 2;
 
    private static final int DAY_ROW_INDEX = 3;
 
    private static final int START_COLUMN_INDEX = 2;
 
    private static final int MAX_DATE_COLUMN_COUNT = 31;
 
    private static final DateTimeFormatter TITLE_FORMATTER = DateTimeFormatter.ofPattern("yyyy年M月");
 
    private final List<LocalDate> attendanceDateList;
 
    public PerformanceShiftHeaderWriteHandler(List<LocalDate> attendanceDateList) {
        this.attendanceDateList = attendanceDateList == null ? Collections.emptyList() : attendanceDateList;
    }
 
    @Override
    public void afterSheetCreate(SheetWriteHandlerContext context) {
        Sheet sheet = context.getWriteSheetHolder().getSheet();
        if (sheet == null || attendanceDateList.isEmpty()) {
            return;
        }
 
        Row titleRow = sheet.getRow(TITLE_ROW_INDEX);
        if (titleRow != null) {
            Cell titleCell = titleRow.getCell(0);
            if (titleCell != null) {
                titleCell.setCellValue(attendanceDateList.get(attendanceDateList.size() - 1).format(TITLE_FORMATTER));
            }
        }
 
        Row weekRow = sheet.getRow(WEEK_ROW_INDEX);
        Row dayRow = sheet.getRow(DAY_ROW_INDEX);
        if (weekRow == null || dayRow == null) {
            return;
        }
 
        for (int i = 0; i < MAX_DATE_COLUMN_COUNT; i++) {
            Cell weekCell = getOrCreateCell(weekRow, START_COLUMN_INDEX + i);
            Cell dayCell = getOrCreateCell(dayRow, START_COLUMN_INDEX + i);
            if (i < attendanceDateList.size()) {
                LocalDate currentDate = attendanceDateList.get(i);
                weekCell.setCellValue(resolveWeekOfYear(currentDate));
                dayCell.setCellValue(currentDate.getDayOfMonth());
            } else {
                weekCell.setBlank();
                dayCell.setBlank();
            }
        }
    }
 
    private Cell getOrCreateCell(Row row, int cellIndex) {
        Cell cell = row.getCell(cellIndex);
        if (cell != null) {
            return cell;
        }
        Cell templateCell = row.getCell(START_COLUMN_INDEX);
        cell = row.createCell(cellIndex);
        if (templateCell != null && templateCell.getCellStyle() != null) {
            cell.setCellStyle(templateCell.getCellStyle());
        }
        return cell;
    }
 
    private String resolveWeekOfYear(LocalDate date) {
        switch (date.getDayOfWeek()) {
            case MONDAY:
                return "一";
            case TUESDAY:
                return "二";
            case WEDNESDAY:
                return "三";
            case THURSDAY:
                return "四";
            case FRIDAY:
                return "五";
            case SATURDAY:
                return "六";
            case SUNDAY:
                return "日";
            default:
                return "";
        }
    }
}