飞书考勤Excel导入到自己系统

devtools/2025/3/4 13:27:33/

此篇主要用于记录Excel一行中,单条数据的日期拿取,并判断上下班打卡情况。代码可能满足不了大部分需求,目前只够本公司用,如果需要,可以参考。
在这里插入图片描述
需要把飞书月度汇总的考勤表导入系统中可以参考下。

下图为需要获取的年月日以及对应表格的数据。

在这里插入图片描述
实现代码如下


//导入Excel接口并调用下方的readAttendanceRecords()方法	/*** 导入飞书考勤列表*//** 示例:@ApiOperation("导入飞书考勤表")@Log(title = "导入飞书考勤表", businessType = BusinessType.IMPORT)@PostMapping("/importFeiShuDataFile")public AjaxResult importFeiShuDataFile(MultipartFile file, boolean updateSupport) throws Exception {File convertFile = convert(file);List<Attendance> attendanceFeiShus = readAttendanceRecords(convertFile);String message = attendanceService.importAttendance(attendanceFeiShus, false, "1");return AjaxResult.success(message);}
**//*** 将MultipartFile转换为File** @param multipartFile 上传的文件* @return 转换后的File对象* @throws IOException 如果文件操作失败*/public static File convert(MultipartFile multipartFile) throws IOException {// 创建一个临时文件Path tempFile = Files.createTempFile("upload-", ".tmp");try (InputStream inputStream = multipartFile.getInputStream()) {// 将MultipartFile的内容复制到临时文件Files.copy(inputStream, tempFile, StandardCopyOption.REPLACE_EXISTING);}// 返回File对象return tempFile.toFile();}public List<Attendance> readAttendanceRecords(File file) throws IOException, InvalidFormatException, ParseException {List<Attendance> records = new ArrayList<>();// 创建工作簿对象Workbook workbook = new XSSFWorkbook(file);Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表// 遍历第一行(跳过表头)Row row = sheet.getRow(1);String date = getCellValue(row.getCell(58)); // 日期
//        截取字符串日期String[] dataArray = date.split("-");String year = dataArray[0];String month = dataArray[1];LocalDate localDate = LocalDate.of(Integer.parseInt(year), Integer.parseInt(month), 1);LocalDate origin = LocalDate.of(Integer.parseInt(year), Integer.parseInt(month), 1);// 获取这个月的总天数int monthLength = localDate.lengthOfMonth();// 遍历每一行(跳过表头)for (int i = 2; i <= sheet.getLastRowNum(); i++) {Row row2 = sheet.getRow(i);if (row2 == null) continue;// 解析每一列的数据String employeeName = getCellValue(row2.getCell(0)); // 员工姓名String employeeNo = getCellValue(row2.getCell(1));   // 员工ID
//            需要查询员工id的员工对象Employee employee = new Employee();if (employeeNo.equals("-")) {employee.setEmployeeName(employeeName);// 根据姓名查询员工idList<Employee> employees = employeeService.selectEmployeeList(employee);if (CollUtil.isNotEmpty(employees)) {employee = employees.get(0); // 如果出现多个员工,请在excel里面把员工编号填写上,否则如果姓名相同根据姓名来获取默认第一个}}String day = getCellValue(row2.getCell(58));       // 日期
//           一个月的数据for (int j = 1; j < monthLength; j++) {Attendance record = new Attendance();record.setEmployeeName(employeeName);record.setEmployeeNo(employee.getEmployeeNo() == null ? employeeNo : employee.getEmployeeNo()); // 如果excel的员工id不为空的话,用excel,否则用查询的if (day.equals("-")) { // 如果为-,代表没来day = getCellValue(row2.getCell(58 + j));localDate = localDate.plusDays(1);// 加一天//                    continue;} else {//                 解析day 的上下班时间if (!day.contains(",")) { // 说明只上班,没下班,默认下午18:00;Date startTime = DateUtils.dateTime("HH:mm:ss", day + ":00");record.setStartTime(startTime);record.setEndTime(DateUtils.dateTime("HH:mm:ss", "18:00:00"));} else {String[] split = day.split(",");record.setStartTime(DateUtils.dateTime("HH:mm:ss", split[0] + ":00")); // 拼接成HH:mm:ss格式if (split.length == 3) {record.setEndTime(DateUtils.dateTime("HH:mm:ss", "18:00:00")); // 一天只打了三次卡,设置默认下班时间为18点}else {record.setEndTime(DateUtils.dateTime("HH:mm:ss", split[split.length-1] + ":00" )); // 一天只打了三次卡,设置默认下班时间为18点}}// 将LocalDate转换为ZonedDateTime(默认时区)ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());// 将ZonedDateTime转换为InstantInstant instant = zonedDateTime.toInstant();// 将Instant转换为DateDate Day = Date.from(instant);record.setDay(Day); // 从该月第一天开始records.add(record);localDate = localDate.plusDays(1);// 加一天day = getCellValue(row2.getCell(58 + j));}}localDate = origin;}logger.info("考勤信息:{{}}", records);workbook.close();return records;}// 获取单元格的值private static String getCellValue(Cell cell) {if (cell == null) {return "";}switch (cell.getCellType()) {case STRING:return cell.getStringCellValue();case NUMERIC:if (DateUtil.isCellDateFormatted(cell)) {return cell.getLocalDateTimeCellValue().toString(); // 如果是日期类型,返回日期字符串} else {return String.valueOf(cell.getNumericCellValue());}case BOOLEAN:return String.valueOf(cell.getBooleanCellValue());case FORMULA:return cell.getCellFormula();default:return "";}}/*** 判断某一年是否是闰年** @param year 年份* @return true表示是闰年,false表示是平年*/public static boolean isLeapYear(int year) {// 规则1:能被4整除但不能被100整除// 规则2:能被400整除return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);}

http://www.ppmy.cn/devtools/164461.html

相关文章

BiliBili视频下载-原理与实现Python+FFmpeg

脚本地址: 项目地址: Gazer BiliGrab.py 提要 适用于: 登录状态下, 非大会员视频下载. 自动解析任意 B 站非大会员 / 付费视频的视频 & 音频请求链接并下载, 需要添加 Cookie 保证视频清晰度. 使用 FFmpeg 命令无损合并视频和音频. 使用方法 克隆或下载项目代码.安装…

Java面试第八山!《Spring框架》

一、Spring框架概述 Spring是Java企业级应用开发的核心框架&#xff0c;通过控制反转&#xff08;IoC&#xff09;和 面向切面编程&#xff08;AOP&#xff09;实现模块解耦&#xff0c;简化开发流程。其核心优势包括依赖注入、声明式事务管理、集成主流ORM框架&#xff08;如…

Day31 第八章 贪心算法 part04

一. 学习文章及资料 860.柠檬水找零 406.根据身高重建队列 452.用最少数量的箭引爆气球 二. 学习内容 1. 柠檬水找零 (1) 解题步骤&#xff1a; 有三种情况&#xff1a; 情况一&#xff1a;账单是5&#xff0c;直接收下。 情况二&#xff1a;账单是10&#xff0c;消耗一个5…

NO.19十六届蓝桥杯模拟赛第三期上

1 如果一个数 p 是个质数&#xff0c;同时又是整数 a 的约数&#xff0c;则 p 称为 a 的一个质因数。 请问&#xff0c; 2024 的最大的质因数是多少&#xff1f; 答&#xff1a;23 #include <bits/stdc.h> using namespace std;int main() {ios::sync_with_stdio(false)…

DevOps原理和实现面试题及参考答案

解释 DevOps 的核心目标与文化价值观,如何理解 “CAMS” 模型? DevOps 的核心目标是打破开发(Development)和运维(Operations)之间的壁垒,通过自动化、协作和持续反馈,实现软件的快速、可靠交付,以更好地满足业务需求和客户期望。具体来说,DevOps 旨在缩短软件的交付…

记录一次跨库连表的坑

一、背景 1. 业务背景 一个微服务项目&#xff0c;本次业务主要涉及两个板块&#xff0c;分别是 文章管理 和 系统管理。具有开发环境、测试环境、生产环境三个环境。其中&#xff0c;开发环境和测试环境用的是同一个服务器&#xff08;nacos和MySQL都是用的同一个服务器中的…

用Python+Flask打造可视化武侠人物关系图生成器:从零到一的实战全记录

用PythonFlask打造可视化武侠人物关系图生成器&#xff1a;从零到一的实战全记录 一、缘起&#xff1a;一个程序小白的奇妙探索之旅 作为一个接触Python仅13天的编程萌新&#xff0c;我曾以为开发一个完整的应用是遥不可及的事情。但在DeepSeek的帮助下&#xff0c;我竟用短短…

Linux总结

1 用户与用户组管理 1.1 用户与用户组 //linux用户和用户组 Linux系统是一个多用户多任务的分时操作系统 使用系统资源的用户需要账号进入系统 账号是用户在系统上的标识&#xff0c;系统根据该标识分配不同的权限和资源 一个账号包含用户和用户组 //用户分类 超级管理员 UID…