package com.ruoyi.performance.excel.handler.attendance; import com.alibaba.excel.util.BooleanUtils; import com.alibaba.excel.write.handler.RowWriteHandler; import com.alibaba.excel.write.handler.context.RowWriteHandlerContext; import com.ruoyi.performance.excel.StaffAttendanceAnnotationTextExcelData; import com.ruoyi.performance.excel.StaffAttendanceExcelData; import org.apache.commons.lang3.StringUtils; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Comment; import org.apache.poi.ss.usermodel.Drawing; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.usermodel.XSSFClientAnchor; import org.apache.poi.xssf.usermodel.XSSFRichTextString; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 人员考勤导出写入处理器 * 1. 补齐模板中不存在的班次明细单元格 * 2. 在对应单元格上写入批注 */ public class CommentWriteHandler implements RowWriteHandler { /** * 模板数据起始行为第 5 行 */ private static final int DATA_START_ROW_INDEX = 4; /** * 模板班次起始列为 C 列 */ private static final int DATA_START_COLUMN_INDEX = 2; private final List excelDataList; private final Map annotationTextMap = new HashMap<>(); public CommentWriteHandler(List excelDataList, List annotationTextList) { this.excelDataList = excelDataList == null ? Collections.emptyList() : excelDataList; if (annotationTextList == null) { return; } annotationTextList.stream() .filter(item -> item != null && StringUtils.isNotBlank(item.getAnnotationText())) .forEach(item -> annotationTextMap.put(buildKey(item.getRowIndex(), item.getCellIndex()), item.getAnnotationText())); } @Override public void afterRowDispose(RowWriteHandlerContext context) { if (BooleanUtils.isTrue(context.getHead())) { return; } Row row = context.getRow(); if (row == null || row.getRowNum() < DATA_START_ROW_INDEX) { return; } int dataIndex = row.getRowNum() - DATA_START_ROW_INDEX; if (dataIndex < 0 || dataIndex >= excelDataList.size()) { return; } StaffAttendanceExcelData excelData = excelDataList.get(dataIndex); if (excelData == null || excelData.getShiftList() == null) { return; } Sheet sheet = row.getSheet(); CellStyle cellStyle = sheet.getColumnStyle(DATA_START_COLUMN_INDEX); Drawing drawingPatriarch = null; List shiftList = excelData.getShiftList(); for (int i = 0; i < shiftList.size(); i++) { int cellIndex = DATA_START_COLUMN_INDEX + i; Cell cell = row.getCell(cellIndex); if (cell == null) { cell = row.createCell(cellIndex); } if (cellStyle != null) { cell.setCellStyle(cellStyle); } String cellValue = shiftList.get(i); if (StringUtils.isNotBlank(cellValue)) { cell.setCellValue(cellValue); } String annotationText = annotationTextMap.get(buildKey(dataIndex, i)); if (StringUtils.isBlank(annotationText) || cell.getCellComment() != null) { continue; } if (drawingPatriarch == null) { drawingPatriarch = sheet.createDrawingPatriarch(); } Comment comment = drawingPatriarch.createCellComment(new XSSFClientAnchor( 0, 0, 0, 0, cellIndex, row.getRowNum(), cellIndex + 2, row.getRowNum() + 3)); comment.setString(new XSSFRichTextString(annotationText)); comment.setAuthor("NS-LIMS"); cell.setCellComment(comment); } } private String buildKey(int rowIndex, int cellIndex) { return rowIndex + "_" + cellIndex; } }