yanghb
2 天以前 7bd7ad452e6a7a3fc60acfd1d6898c70e933773e
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
package com.ruoyi.common.easyExcel;
 
import cn.hutool.core.util.NumberUtil;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
 
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
 
public class CommentWriteHandler<T> implements CellWriteHandler {
 
    private final List<T> dataList;
    private final Map<String, String> warnToTargetFieldMap;
    private final Map<String, String> warnMessages;
 
    public CommentWriteHandler(List<T> dataList,
                               Map<String, String> warnToTargetFieldMap,
                               Map<String, String> warnMessages) {
        this.dataList = dataList;
        this.warnToTargetFieldMap = warnToTargetFieldMap;
        this.warnMessages = warnMessages;
    }
 
    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder,
                                 WriteTableHolder writeTableHolder,
                                 List<WriteCellData<?>> cellDataList,
                                 Cell cell,
                                 Head head,
                                 Integer relativeRowIndex,
                                 Boolean isHead) {
        if (isHead) return;
 
        String fieldName = head.getFieldName();
        T rowData = dataList.get(relativeRowIndex);
 
        for (Map.Entry<String, String> entry : warnToTargetFieldMap.entrySet()) {
            String warnField = entry.getKey();
            String targetField = entry.getValue();
 
            if (!fieldName.equals(targetField)) continue;
 
            try {
                Field warn = rowData.getClass().getDeclaredField(warnField);
                warn.setAccessible(true);
                Object warnValue = warn.get(rowData);
                if (NumberUtil.parseInt(warnValue.toString()) == 1) {
 
                    // ✅ 设置背景色通过 WriteCellData 实现
                    if (cellDataList != null && !cellDataList.isEmpty()) {
                        WriteCellStyle style = new WriteCellStyle();
                        style.setFillForegroundColor(IndexedColors.RED.getIndex());
                        style.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
                        cellDataList.get(0).setWriteCellStyle(style);
                    }
 
                    // 获取现有的批注
                    Comment existingComment = cell.getCellComment();
                    String existingText = "";
                    if (existingComment != null) {
                        // 获取已有批注的内容
                        existingText = existingComment.getString().getString();
                    }
 
                    // 追加新的批注内容
                    String newCommentText = warnMessages.getOrDefault(warnField, "数据异常,请核查");
                    String updatedCommentText = existingText + "\n" + newCommentText;
 
                    // 如果已有批注,则更新批注内容;如果没有,则创建一个新的批注
                    if (existingComment != null) {
                        existingComment.setString(new XSSFRichTextString(updatedCommentText));
                    } else {
                        // 创建新的批注并设置内容
                        Drawing<?> drawing = cell.getSheet().createDrawingPatriarch();
                        Comment comment = drawing.createCellComment(new XSSFClientAnchor(
                                0, 0, 0, 0,
                                cell.getColumnIndex(), cell.getRowIndex(),
                                cell.getColumnIndex() + 3, cell.getRowIndex() + 2
                        ));
                        comment.setString(new XSSFRichTextString(updatedCommentText));
                        comment.setAuthor("系统提示");
                        cell.setCellComment(comment);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}