From 2c02ee738df0ebae3a38c9dd259ced94b72d243c Mon Sep 17 00:00:00 2001 From: aikai Date: Wed, 26 Jun 2024 19:19:51 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9F=BF=E5=B7=A5=E7=BB=9F=E8=AE=A1=E8=B0=83?= =?UTF-8?q?=E6=95=B4/=E8=80=83=E5=8B=A4excel=E5=AF=BC=E5=87=BA=20=E4=B8=8D?= =?UTF-8?q?=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../attendance/AttendanceServiceImpl.java | 108 +++++++++++++++++- .../attendance/CustomCellStyleHandler.java | 57 +++++++++ 2 files changed, 162 insertions(+), 3 deletions(-) create mode 100644 yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/attendance/CustomCellStyleHandler.java diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/attendance/AttendanceServiceImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/attendance/AttendanceServiceImpl.java index 3a51dc61..23f81eea 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/attendance/AttendanceServiceImpl.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/attendance/AttendanceServiceImpl.java @@ -33,6 +33,7 @@ import cn.iocoder.yudao.module.system.service.attendance.punch.PunchService; import cn.iocoder.yudao.module.system.service.attendance.punch.dto.AttendanceOnTheDayDTO; import cn.iocoder.yudao.module.system.service.attendance.punchrecord.AttendancePunchRecordService; import cn.iocoder.yudao.module.system.service.user.AdminUserService; +import com.alibaba.excel.EasyExcel; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import lombok.extern.slf4j.Slf4j; @@ -682,7 +683,7 @@ public class AttendanceServiceImpl implements AttendanceService { // -- 缺卡 this.calculateMissingCardsList(workMap, calculateNum); // -- 旷工 - this.calculateMiner(entry.getValue(), calculateNum); + this.calculateMiner(dayEntry.getValue(), calculateNum); // -- 外勤 this.calculateFieldService(workMap, calculateNum); } @@ -810,10 +811,111 @@ public class AttendanceServiceImpl implements AttendanceService { .in(AttendancePunchRecordDO::getUserId, userList) .in(AttendancePunchRecordDO::getDayTime, dateList)); // -- 根据部门分组 - 根据考勤组分组 - - Map>> map = list.stream().collect(Collectors.groupingBy(AttendancePunchRecordDO::getDeptId, Collectors.groupingBy(AttendancePunchRecordDO::getAttendanceGroupId))); + + + } + + public static void main(String[] args) { + String fileName = "/Users/aikai/Downloads/" + System.currentTimeMillis() + ".xlsx"; + // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 + EasyExcel.write(fileName) + .head(generateHead()) + .registerWriteHandler(new CustomCellStyleHandler()) +// .registerWriteHandler(new LoopMergeStrategy()) +// .registerWriteHandler(loopMergeStrategy) + .sheet("模板") + .doWrite(generateData()); + } + + public static List> generateHead() { + List> head = new ArrayList<>(); + String headTitle = "月度汇总 统计日期:2024-06-01 至 2024-06-13"; + String detailedHead = "月度汇总 统计日期:2024-06-01 至 2024-06-13 10:48"; + head.add(Arrays.asList(headTitle, detailedHead, "姓名", "姓名")); + head.add(Arrays.asList(headTitle, detailedHead, "考勤组", "考勤组")); + head.add(Arrays.asList(headTitle, detailedHead, "部门", "部门")); + head.add(Arrays.asList(headTitle, detailedHead, "工号", "工号")); + head.add(Arrays.asList(headTitle, detailedHead, "职位", "职位")); + head.add(Arrays.asList(headTitle, detailedHead, "出勤天数", "出勤天数")); + head.add(Arrays.asList(headTitle, detailedHead, "休息天数", "休息天数")); + head.add(Arrays.asList(headTitle, detailedHead, "工作时长", "工作时长")); + head.add(Arrays.asList(headTitle, detailedHead, "迟到次数", "迟到次数")); + head.add(Arrays.asList(headTitle, detailedHead, "迟到时长", "迟到时长")); + head.add(Arrays.asList(headTitle, detailedHead, "早退次数", "早退次数")); + head.add(Arrays.asList(headTitle, detailedHead, "早退时长", "早退时长")); + head.add(Arrays.asList(headTitle, detailedHead, "上班缺卡次数", "上班缺卡次数")); + head.add(Arrays.asList(headTitle, detailedHead, "下班缺卡次数", "下班缺卡次数")); + head.add(Arrays.asList(headTitle, detailedHead, "旷工天数", "旷工天数")); +// head.add(Arrays.asList(headTitle, detailedHead, "出差时长", "出差时长")); +// head.add(Arrays.asList(headTitle, detailedHead, "外出时长", "外出时长")); +// head.add(Arrays.asList(headTitle, detailedHead, "加班-审批单统计", "加班-审批单统计")); +// head.add(Arrays.asList(headTitle, detailedHead, "加班时长-按加班规则计算", "工作日加班")); +// head.add(Arrays.asList(headTitle, detailedHead, "加班时长-按加班规则计算", "休息日加班")); +// head.add(Arrays.asList(headTitle, detailedHead, "加班时长-按加班规则计算", "节假日加班")); + head.add(Arrays.asList(headTitle, detailedHead, "考勤结果", "六")); + head.add(Arrays.asList(headTitle, detailedHead, "考勤结果", "日")); + head.add(Arrays.asList(headTitle, detailedHead, "考勤结果", "1")); + head.add(Arrays.asList(headTitle, detailedHead, "考勤结果", "2")); + head.add(Arrays.asList(headTitle, detailedHead, "考勤结果", "3")); + head.add(Arrays.asList(headTitle, detailedHead, "考勤结果", "4")); + head.add(Arrays.asList(headTitle, detailedHead, "考勤结果", "5")); + head.add(Arrays.asList(headTitle, detailedHead, "考勤结果", "六")); + head.add(Arrays.asList(headTitle, detailedHead, "考勤结果", "日")); + // 添加更多表头 + return head; + } + + public static List> generateData() { + List> data = new ArrayList<>(); + + List row1 = new ArrayList<>(); + row1.add("艾楷"); + row1.add("测试考勤"); + row1.add(""); + row1.add(""); + row1.add(""); + row1.add("22026829221062585"); + row1.add(1); + row1.add(2); + row1.add(3); + row1.add(4); + row1.add(5); + row1.add(6); + row1.add(7); + row1.add(8); + row1.add(9); + row1.add(10); + row1.add(10); + row1.add(12); + // 添加更多数据 + + List row2 = new ArrayList<>(); + row2.add("谢鸿飞"); + row2.add("测试考勤"); + row2.add(""); + row2.add(""); + row2.add(""); + row2.add("066557433135769889"); + row2.add(1); + row2.add(2); + row2.add(3); + row2.add(4); + row2.add(4); + row2.add(4); + row2.add(4); + row2.add(4); + row2.add(4); + row2.add(4); + row2.add(4); + row2.add(4); + // 添加更多数据 + + data.add(row1); + data.add(row2); + + return data; } /** diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/attendance/CustomCellStyleHandler.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/attendance/CustomCellStyleHandler.java new file mode 100644 index 00000000..8f543678 --- /dev/null +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/attendance/CustomCellStyleHandler.java @@ -0,0 +1,57 @@ +package cn.iocoder.yudao.module.system.service.attendance; + +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 com.alibaba.excel.write.metadata.style.WriteFont; +import org.apache.poi.ss.usermodel.*; + +import java.util.List; + +public class CustomCellStyleHandler implements CellWriteHandler { + + @Override + public void afterCellDispose( + WriteSheetHolder writeSheetHolder, + WriteTableHolder writeTableHolder, + List> cellDataList, + Cell cell, + Head head, + Integer relativeRowIndex, + Boolean isHead) { + + Workbook workbook = writeSheetHolder.getSheet().getWorkbook(); + CellStyle cellStyle = workbook.createCellStyle(); + writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), 5120); + // 设置水平和垂直居中对齐 + cellStyle.setAlignment(HorizontalAlignment.CENTER); + cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); + // 设置不同行的样式 + if (isHead) { + if (relativeRowIndex == 0 || relativeRowIndex == 1) { + // 头的策略 + WriteCellStyle headWriteCellStyle = new WriteCellStyle(); + // 背景设置为红色 + headWriteCellStyle.setFillForegroundColor(IndexedColors.TURQUOISE.getIndex()); + WriteFont headWriteFont = new WriteFont(); + headWriteFont.setFontHeightInPoints((short) 20); + headWriteCellStyle.setWriteFont(headWriteFont); + WriteCellStyle.merge(headWriteCellStyle, cellDataList.get(0).getOrCreateStyle()); + } else if (relativeRowIndex == 2 || relativeRowIndex == 3) { + // 头的策略 + WriteCellStyle headWriteCellStyle = new WriteCellStyle(); + // 背景设置为红色 + headWriteCellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex()); + WriteFont headWriteFont = new WriteFont(); + headWriteFont.setFontHeightInPoints((short) 20); + headWriteCellStyle.setWriteFont(headWriteFont); + WriteCellStyle.merge(headWriteCellStyle, cellDataList.get(0).getOrCreateStyle()); + } + } + cell.setCellStyle(cellStyle); + } +} +