POI之考勤統(tǒng)計(jì)

需求分析

我們公司考勤使用指紋打卡琅束,出入公司也需要指紋打卡開門,這樣每個(gè)人每天打卡記錄全在考勤系統(tǒng)里面算谈,這個(gè)考勤系統(tǒng)竟然還不能自動(dòng)識(shí)別員工每天的出勤情況涩禀,只是把每次打卡記錄統(tǒng)計(jì)出來。然后月底時(shí)由人事然眼、財(cái)務(wù)篩選做績效考核和工資核算艾船,先來看看五月份考勤系統(tǒng)統(tǒng)計(jì)出來的原始數(shù)據(jù):

看到這個(gè)表,我也是醉了高每。公司幾十人員工統(tǒng)計(jì)出來4828條記錄屿岂,這樣的系統(tǒng)要它何用。現(xiàn)在我用POI寫個(gè)程序自動(dòng)篩選出來并導(dǎo)出Excel文件鲸匿。

設(shè)計(jì)思路

1.找到每個(gè)員工每天的第一條打開記錄和最后一條打卡記錄爷怀,去除中間重復(fù)數(shù)據(jù)。

2.統(tǒng)計(jì)到每天只有一次打卡的記錄為考勤異常带欢。

3.統(tǒng)計(jì)第一次打卡在早上九點(diǎn)半之后的記錄為遲到运授。

4.統(tǒng)計(jì)上班不滿8小時(shí)的記錄為早退烤惊。

5.統(tǒng)計(jì)周六、周日加班日期.

6.統(tǒng)計(jì)正常出勤天數(shù)吁朦。

效果預(yù)覽

代碼實(shí)現(xiàn)

1.把考勤系統(tǒng)導(dǎo)出的考勤表放到D:\liuwuchang文件夾里柒室。

2.新建OnePeople.java

package com.zhanyun.cc;

public class OnePeople {

String id;

String name;

String date;

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getDate() {

return date;

}

public void setDate(String date) {

this.date = date;

}

@Override

public String toString() {

return "OnePeople [id=" + id + ", name=" + name + ", date=" + date + "]";

}

}

3.新建ManyPeople.java

package com.zhanyun.cc;

import java.util.ArrayList;

public class ManyPeople {

String id;String name;

ArrayListonePeopleList;

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public ArrayListgetOnePeopleList() {

return onePeopleList;

}

public void setOnePeopleList(ArrayList onePeopleList) {

this.onePeopleList = onePeopleList;

}

@Override

public String toString() {

return "ManyPeople [id=" + id + ", name=" + name + ", onePeopleList=" + onePeopleList + "]";

}

}

4.新建Result.java

package com.zhanyun.cc;

public class Result {

String id;

String name;

String normalDay;

String lateDay;

String leaveEarly;

String weekDay;

String error;

public Result() {

// TODO Auto-generated constructor stub

}

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getNormalDay() {

return normalDay;

}

public void setNormalDay(String normalDay) {

this.normalDay = normalDay;

}

public String getLateDay() {

return lateDay;

}

public void setLateDay(String lateDay) {

this.lateDay = lateDay;

}

public String getLeaveEarly() {

return leaveEarly;

}

public void setLeaveEarly(String leaveEarly) {

this.leaveEarly = leaveEarly;

}

public String getWeekDay() {

return weekDay;

}

public void setWeekDay(String weekDay) {

this.weekDay = weekDay;

}

public String getError() {

return error;

}

public void setError(String error) {

this.error = error;

}

@Override

public String toString() {

return "Result [id=" + id + ", name=" + name + ", normalDay=" + normalDay + ", lateDay=" + lateDay

+ ", leaveEarly=" + leaveEarly + ", weekDay=" + weekDay + ", error=" + error + "]";

}

}

5.新建MainActivity.java

package com.zhanyun.cc;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Calendar;

import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class MainActivity {

String filePath = "D://liuwuchang/考勤表.xls";

InputStream stream;

HSSFWorkbook workbook;

HSSFSheet sheet;

ArrayList resultList;

public MainActivity() {

try {

stream = new FileInputStream(filePath);

workbook = new HSSFWorkbook(stream);// 讀取現(xiàn)有的Excel

sheet = workbook.getSheet("Sheet1");// 得到指定名稱的Sheet

resultList = new ArrayList<>();

} catch (Exception e) {

e.printStackTrace();

}

}

??/*

?? * 讀取所有人的考勤記錄

?? */

??public ArrayList getManyPeopleList() {

????ArrayList manyPeopleList = new ArrayList<>();

????ArrayList onePeopleList = null;

????ManyPeople manyPeople = null;

????String id = null;

????int firstRowNum = sheet.getFirstRowNum();

????int lastRowNum = sheet.getLastRowNum();

????for (int i = firstRowNum; i <= lastRowNum; i++) {

??????HSSFRow row = sheet.getRow(i);

??????if (id != row.getCell(0).getStringCellValue()) {

????????id = row.getCell(0).getStringCellValue();

????????onePeopleList = new ArrayList<>();

????????manyPeople = new ManyPeople();

????????manyPeople.setId(row.getCell(0).getStringCellValue());

????????manyPeople.setName(row.getCell(1).getStringCellValue());

????????manyPeople.setOnePeopleList(onePeopleList);

????????manyPeopleList.add(manyPeople);

??????}

??????OnePeople onePeople = new OnePeople();

??????onePeople.setId(row.getCell(0).getStringCellValue());

??????onePeople.setName(row.getCell(1).getStringCellValue());

??????onePeople.setDate(row.getCell(2).getStringCellValue());

??????onePeopleList.add(onePeople);

????}

????return manyPeopleList;

??}

??/*

?? * 讀取所有人有效的考勤記錄

?? */

??public void getValidManyPeopleList(String id,String name,ArrayList list) {

????Result result = new Result();

????int normalDay=0;

????String lateDay="";

????String leaveEarly="";

????String weekDay="";

????String error="";

????ArrayList firstOnePeopleList = new ArrayList<>();

????ArrayList lastOnePeopleList = new ArrayList<>();

????SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm");

????Calendar calendar = Calendar.getInstance();

????int firstDay = 0;

????int lastDay = 0;

????for (int i = 0; i < list.size(); i++) {

??????try {

????????Date date = formatter.parse(list.get(i).getDate());

????????calendar.setTime(date);

????????if (firstDay != calendar.get(Calendar.DAY_OF_MONTH)) {

??????????firstDay = calendar.get(Calendar.DAY_OF_MONTH);

??????????firstOnePeopleList.add(date);

????????}

????????// System.out.println(calendar.get(Calendar.DAY_OF_MONTH));

??????} catch (ParseException e) {

????????// TODO Auto-generated catch block

????????e.printStackTrace();

??????}

????}

????for (int i = list.size() - 1; i >= 0; i--) {

??????try {

????????Date date = formatter.parse(list.get(i).getDate());

????????calendar.setTime(date);

????????if (lastDay != calendar.get(Calendar.DAY_OF_MONTH)) {

??????????lastDay = calendar.get(Calendar.DAY_OF_MONTH);

??????????lastOnePeopleList.add(date);

????????}

??????} catch (ParseException e) {

????????// TODO Auto-generated catch block

????????e.printStackTrace();

??????}

????}

????ArrayList arrayList = new ArrayList<>();

????for (int i = lastOnePeopleList.size() - 1; i >= 0; i--) {

??????arrayList.add(lastOnePeopleList.get(i));

????}

????lastOnePeopleList = arrayList;

????int len = firstOnePeopleList.size();

????for (int i = 0; i < len; i++) {

??????calendar.setTime(firstOnePeopleList.get(i));

??????int month=calendar.get(Calendar.MONTH)+1;

??????int day=calendar.get(Calendar.DAY_OF_MONTH);

??????String week = getWeekOfDate(firstOnePeopleList.get(i));

??????long firstTime = firstOnePeopleList.get(i).getTime();

??????long lastTime = lastOnePeopleList.get(i).getTime();

??????int firstHours = firstOnePeopleList.get(i).getHours();

??????int firstMinutes = firstOnePeopleList.get(i).getMinutes();

??????int lastHours = lastOnePeopleList.get(i).getHours();

??????int lastMinutes = lastOnePeopleList.get(i).getMinutes();

??????if (firstTime != lastTime) {

????????if (week != "星期日" && week != "星期六") {

??????????if (firstHours < 9 || (firstHours == 9 && firstMinutes <= 30)) {

????????????long diff = (lastTime - firstTime) / (1000 * 60);

????????????if (diff >= (9 * 60) && (lastHours > 17 || (lastHours == 17 && lastMinutes >= 30))) {

//??????????????System.out.println("正常上班");

??????????????normalDay++;

????????????} else {

//??????????????System.out.println("早退");

??????????????leaveEarly=leaveEarly+month+"月"+day+"日"+lastHours+":"+lastMinutes+"? ";

????????????}

??????????} else {

//????????????System.out.println("遲到");

????????????lateDay=lateDay+month+"月"+day+"日"+firstHours+":"+firstMinutes+"? ";

??????????}

????????} else {

//??????????System.out.println("周末加班");

??????????weekDay=weekDay+month+"月"+day+"日? ";

????????}

??????} else {

????????// System.out.println(calendar.get(Calendar.DAY_OF_MONTH));

//????????System.out.println("考勤異常");

????????error=error+month+"月"+day+"日? "+firstHours+":"+firstMinutes+"? ";

??????}

????}


????result.setId(id);

????result.setName(name);

????result.setNormalDay(normalDay+"");

????result.setLateDay(lateDay);

????result.setLeaveEarly(leaveEarly);

????result.setWeekDay(weekDay);

????result.setError(error);

????resultList.add(result);

??}

??/**

?? * * 獲取指定日期是星期幾 參數(shù)為null時(shí)表示獲取當(dāng)前日期是星期幾

?? *

?? * @param date

?? * @return

?? */

??public String getWeekOfDate(Date date) {

????String[] weekOfDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };

????Calendar calendar = Calendar.getInstance();

????if (date != null) {

??????calendar.setTime(date);

????}

????int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;

????if (w < 0) {

??????w = 0;

????}

????return weekOfDays[w];

??}

??public static void main(String[] args) {

????MainActivity mainActivity = new MainActivity();

????ArrayList manyPeopleList = mainActivity.getManyPeopleList();

????HSSFWorkbook workbook = new HSSFWorkbook();// 創(chuàng)建Excel文件(Workbook)

????HSSFSheet sheet = workbook.createSheet("Sheet1");// 創(chuàng)建工作表(Sheet)

???? for (int i = 0; i < manyPeopleList.size(); i++) {

?????? mainActivity.getValidManyPeopleList(manyPeopleList.get(i).getId(),

????????????manyPeopleList.get(i).getName(),

????????????manyPeopleList.get(i).getOnePeopleList());

???? }

????for (int i = 0; i < mainActivity.resultList.size(); i++) {

??????HSSFRow row= sheet.createRow(i);

??????HSSFCell cell_0 = row.createCell(0);

??????HSSFCell cell_1 = row.createCell(1);

??????HSSFCell cell_2 = row.createCell(2);

??????HSSFCell cell_3 = row.createCell(3);

??????HSSFCell cell_4 = row.createCell(4);

??????HSSFCell cell_5 = row.createCell(5);

??????HSSFCell cell_6 = row.createCell(6);

??????cell_0.setCellValue(mainActivity.resultList.get(i).getId());

??????cell_1.setCellValue(mainActivity.resultList.get(i).getName());

??????cell_2.setCellValue(mainActivity.resultList.get(i).getNormalDay());

??????cell_3.setCellValue(mainActivity.resultList.get(i).getLateDay());

??????cell_4.setCellValue(mainActivity.resultList.get(i).getLeaveEarly());

??????cell_5.setCellValue(mainActivity.resultList.get(i).getWeekDay());

??????cell_6.setCellValue(mainActivity.resultList.get(i).getError());



??????System.out.println(mainActivity.resultList.get(i).toString());

????}


????String filePath = "D:\\統(tǒng)計(jì)考勤表.xls";// 文件路徑

????FileOutputStream out;

????try {

??????out = new FileOutputStream(filePath);

??????workbook.write(out);// 保存Excel文件

??????out.close();// 關(guān)閉文件流

??????System.out.println("OK!");

????} catch (Exception e) {

??????// TODO Auto-generated catch block

??????e.printStackTrace();

????}

??}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市逗宜,隨后出現(xiàn)的幾起案子雄右,更是在濱河造成了極大的恐慌,老刑警劉巖锦溪,帶你破解...
    沈念sama閱讀 212,718評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件不脯,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡刻诊,警方通過查閱死者的電腦和手機(jī)防楷,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,683評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來则涯,“玉大人复局,你說我怎么就攤上這事∷谂校” “怎么了亿昏?”我有些...
    開封第一講書人閱讀 158,207評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長档礁。 經(jīng)常有香客問我角钩,道長,這世上最難降的妖魔是什么呻澜? 我笑而不...
    開封第一講書人閱讀 56,755評(píng)論 1 284
  • 正文 為了忘掉前任递礼,我火速辦了婚禮,結(jié)果婚禮上羹幸,老公的妹妹穿的比我還像新娘脊髓。我一直安慰自己,他們只是感情好栅受,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,862評(píng)論 6 386
  • 文/花漫 我一把揭開白布将硝。 她就那樣靜靜地躺著,像睡著了一般屏镊。 火紅的嫁衣襯著肌膚如雪依疼。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 50,050評(píng)論 1 291
  • 那天而芥,我揣著相機(jī)與錄音涛贯,去河邊找鬼。 笑死蔚出,一個(gè)胖子當(dāng)著我的面吹牛弟翘,可吹牛的內(nèi)容都是我干的虫腋。 我是一名探鬼主播,決...
    沈念sama閱讀 39,136評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼稀余,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼悦冀!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起睛琳,我...
    開封第一講書人閱讀 37,882評(píng)論 0 268
  • 序言:老撾萬榮一對(duì)情侶失蹤盒蟆,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后师骗,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體历等,經(jīng)...
    沈念sama閱讀 44,330評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,651評(píng)論 2 327
  • 正文 我和宋清朗相戀三年辟癌,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了寒屯。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,789評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡黍少,死狀恐怖寡夹,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情厂置,我是刑警寧澤菩掏,帶...
    沈念sama閱讀 34,477評(píng)論 4 333
  • 正文 年R本政府宣布,位于F島的核電站昵济,受9級(jí)特大地震影響智绸,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜访忿,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,135評(píng)論 3 317
  • 文/蒙蒙 一传于、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧醉顽,春花似錦、人聲如沸平挑。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,864評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽通熄。三九已至唆涝,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間唇辨,已是汗流浹背廊酣。 一陣腳步聲響...
    開封第一講書人閱讀 32,099評(píng)論 1 267
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留赏枚,地道東北人亡驰。 一個(gè)月前我還...
    沈念sama閱讀 46,598評(píng)論 2 362
  • 正文 我出身青樓晓猛,卻偏偏與公主長得像,于是被迫代替她去往敵國和親凡辱。 傳聞我的和親對(duì)象是個(gè)殘疾皇子戒职,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,697評(píng)論 2 351

推薦閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法透乾,內(nèi)部類的語法洪燥,繼承相關(guān)的語法,異常的語法乳乌,線程的語...
    子非魚_t_閱讀 31,602評(píng)論 18 399
  • 先Activity的抽象類 BaseActivity [java]view plaincopy /** *Acti...
    Zaker2Magic閱讀 972評(píng)論 0 0
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理捧韵,服務(wù)發(fā)現(xiàn),斷路器汉操,智...
    卡卡羅2017閱讀 134,638評(píng)論 18 139
  • 初入簡書,對(duì)無戒老師的365訓(xùn)練營也有了一些了解膀斋。訓(xùn)練營堅(jiān)持每日一更千字以上梭伐,并且還會(huì)有各種老師的精品課程和文評(píng)。...
    細(xì)雨斜風(fēng)冷畫橋閱讀 514評(píng)論 26 11
  • 蘋果公司規(guī)定6.1號(hào)之后仰担,所有上線的應(yīng)用需要支持IPv6,于是公司就讓我來適配以前的一個(gè)老項(xiàng)目糊识,這個(gè)項(xiàng)目最新版本是...
    小霍同學(xué)閱讀 2,213評(píng)論 7 4