本文目录导读:

- 方案一:Excel VBA 宏(适合非技术人员,直接内嵌在Excel中)
- 方案二:Python 脚本(适合处理大量文件或复杂逻辑,如多个Excel合并)
- 方案三:PQ(Power Query)—— 基于GUI,无需写代码
- 使用建议
要汇总考勤报表,通常有两种路径:通过Excel公式/函数(适用于手动或半自动汇总)或通过VBA/Python脚本(适用于自动化批量处理)。
由于你没有指定具体使用的工具(Excel、Python、还是企业系统内置脚本),我将针对最常用的Excel(VBA宏)和Python(Pandas库)给出两种主流脚本解决方案。
Excel VBA 宏(适合非技术人员,直接内嵌在Excel中)
如果你的考勤数据是每天一个sheet,或者所有数据在一个sheet的明细行里,这个脚本可以帮你汇总成:姓名、出勤天数、迟到次数、缺勤次数等。
前提假设:
- 原始数据在
Sheet1,格式为:A列(姓名),B列(日期),C列(状态)(如“出勤”、“迟到”、“缺勤”)。 - 汇总结果放到
Sheet2。
操作步骤:
- 按
Alt + F11打开VBA编辑器。 - 插入 -> 模块,粘贴以下代码。
- 按
F5运行。
Sub 汇总考勤()
Dim wsData As Worksheet, wsResult As Worksheet
Dim lastRow As Long, i As Long
Dim dict As Object
Dim key As String
Dim name As String, status As String
Set wsData = ThisWorkbook.Sheets("Sheet1")
Set wsResult = ThisWorkbook.Sheets("Sheet2")
' 使用字典去重并统计
Set dict = CreateObject("Scripting.Dictionary")
lastRow = wsData.Cells(wsData.Rows.Count, 1).End(xlUp).Row
' 清空结果区域
wsResult.Cells.Clear
wsResult.Range("A1:D1") = Array("姓名", "出勤天数", "迟到次数", "缺勤天数")
For i = 2 To lastRow ' 假设第一行是标题
name = wsData.Cells(i, 1).Value
status = wsData.Cells(i, 3).Value
If name <> "" Then
' 如果字典中没有该名字,初始化一个数组 [出勤, 迟到, 缺勤]
If Not dict.exists(name) Then
dict(name) = Array(0, 0, 0)
End If
' 根据状态累加
Select Case status
Case "出勤"
arr = dict(name)
arr(0) = arr(0) + 1
dict(name) = arr
Case "迟到"
arr = dict(name)
arr(1) = arr(1) + 1
dict(name) = arr
Case "缺勤"
arr = dict(name)
arr(2) = arr(2) + 1
dict(name) = arr
End Select
End If
Next i
' 将字典数据写入结果Sheet
Dim r As Long
r = 2
For Each key In dict.keys
wsResult.Cells(r, 1) = key
wsResult.Cells(r, 2) = dict(key)(0)
wsResult.Cells(r, 3) = dict(key)(1)
wsResult.Cells(r, 4) = dict(key)(2)
r = r + 1
Next key
MsgBox "考勤汇总完成!"
End Sub
Python 脚本(适合处理大量文件或复杂逻辑,如多个Excel合并)
适用场景: 你有多个考勤月的Excel文件,或者格式不规范,需要清洗后汇总。
环境准备: 安装 pandas 和 openpyxl。
pip install pandas openpyxl
脚本示例(summary_attendance.py):
假设你有多个Excel文件在 attendance_files/ 目录下,每个文件格式为:姓名、日期、状态。
import pandas as pd
import os
from pathlib import Path
def batch_summary(folder_path, output_file="考勤汇总.xlsx"):
"""
批量汇总文件夹内所有考勤Excel文件
"""
all_data = []
for file in Path(folder_path).glob("*.xlsx"):
df = pd.read_excel(file)
# 假设列名为:姓名、日期、状态(可根据实际情况修改)
df = df[['姓名', '日期', '状态']]
all_data.append(df)
if not all_data:
print("没有找到考勤文件")
return
# 合并所有数据
df_all = pd.concat(all_data, ignore_index=True)
# 使用数据透视表进行汇总
summary = df_all.pivot_table(
index='姓名',
columns='状态',
values='日期',
aggfunc='count',
fill_value=0
)
# 确保需要的列存在
required_cols = ['出勤', '迟到', '缺勤', '加班']
for col in required_cols:
if col not in summary.columns:
summary[col] = 0
# 按需要的顺序排列
summary = summary[['出勤', '迟到', '缺勤', '加班']]
# 添加合计行
summary.loc['合计'] = summary.sum()
# 输出结果
summary.to_excel(output_file)
print(f"汇总报表已保存到: {output_file}")
print(summary)
if __name__ == "__main__":
# 使用示例:运行前修改文件夹路径
batch_summary("attendance_files/")
PQ(Power Query)—— 基于GUI,无需写代码
如果你使用的是 Excel 2016 或更高版本(或WPS专业版),Power Query是最推荐的方式。
- 数据 -> 获取数据 -> 从文件 -> 从文件夹。
- 选择考勤文件存放的文件夹。
- 点 组合 -> 合并并载入。
- 在PQ编辑器中,选择“姓名”、“日期”、“状态”三列。
- 分组依据 -> 按“姓名”分组 -> 新增列(计数)。
- 关闭并上载,即自动生成动态汇总表。
- 以后新文件放入文件夹,只需 刷新 即可。
使用建议
| 你的需求 | 推荐方案 |
|---|---|
| 一次性、小数据量、手动操作 | Excel 公式(COUNTIFS) |
| 每月固定汇总,数据量中等 | Power Query(无需编程) |
| 数据量巨大(数万行以上) | Python,速度快,可定时运行 |
| 需要复杂的判断逻辑(如跨天加班、漏打卡) | VBA 或 Python 均可 |
如果你能提供样例数据截图或具体包含哪些列(比如是否有打卡时间、是否区分早退/迟到),我可以帮你把脚本修改成更匹配你实际需求的样子。