import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class daochudaoexcel {
public static void main(String[] args) throws BiffException, IOException, ParseException {
String fileName = "D://Io//jisuan.xls"; // Excel文件所在路径
File file = new File(fileName); // 创建文件对象
Workbook wb = Workbook.getWorkbook(file); // 从文件流中获取Excel工作区对象(WorkBook)
Sheet sheet = wb.getSheet(0); // 从工作区中取得页(Sheet)
int n =0;
int j =0;
int f = 0;
Date max = null;
String Stringmax = null;
String Stringmin = null;
String zgs = "总公司";
DateFormat df = new SimpleDateFormat("HH:mm:ss");
//规定早上最晚打卡时间
Date time = df.parse("10:00:00");
while(n<sheet.getRows()){
//如果是总公司
if(sheet.getCell(0,n).getContents().equals(zgs)){
//如果姓名相等
if(sheet.getCell(1,n).getContents().equals(sheet.getCell(1,n+1).getContents())){
//如果日期相等
if(sheet.getCell(2,n).getContents().equals(sheet.getCell(2,n+1).getContents())){
//如果第n个时间小于n+1,那么将max等于n+1
if(df.parse(sheet.getCell(3,n).getContents()).getTime()<df.parse(sheet.getCell(3,n+1).getContents()).getTime() ){
max=df.parse(sheet.getCell(3,n+1).getContents());
//将max转成String
Stringmax = sheet.getCell(3,n+1).getContents();
}
}else {
//如果日期不相等那么下个日期的第一个打卡时间 就是min
Date min= df.parse(sheet.getCell(3,n+1).getContents());
//将min转为String类型
Stringmin = sheet.getCell(3,n+1).getContents();
//如果最早打卡时间大于规定早上最晚打卡时间 那么则判定为迟到
if(min.getTime()>time.getTime()){
System.err.print("迟到");
//如果上班时间大于小于八个小时 则说明早退
}else if((max.getTime()-min.getTime())/3600000<8){
System.err.print("早退");
}
//输出员工上班下班时间 和上班多少小时
System.out.println(sheet.getCell(0,n).getContents()+"员工:" +sheet.getCell(1,n).getContents()+sheet.getCell(2,n+1).getContents() + " "+ "上班时间:" + Stringmin + " " + "下班时间" + Stringmax + "上班了" + " " +(max.getTime()-min.getTime())/3600000 + "个小时" );
}
}
}
n++;
}
}
}
需求是输出这个人最早上班时间 最晚下班时间和是否迟到早退 我的逻辑是先判断第一列是不是相等 再判断第二列是否相等 直到判断到日期相等 然后遍历最大的时间替换为max 因为打卡时间是按照从早到晚排列的 到了下一天 那么下一天的第一个打卡时间是最早打卡时间 但是这样第一天的也就是表格中的第一天自动给略过了 因为第一天没有在else中判断 这个怎么解决 新手 请各位大牛帮帮忙 谢谢
//换了一个逻辑,由于打卡时间是从小到大排列的,那么只需定位一个人同部门 ,同一天的最早和最晚打卡时间即可,参照如下
public static void main(String[] args) throws BiffException, IOException, ParseException {
String fileName = "D://Io//jisuan.xls"; // Excel文件所在路径
File file = new File(fileName); // 创建文件对象
Workbook wb = Workbook.getWorkbook(file); // 从文件流中获取Excel工作区对象(WorkBook)
Sheet sheet = wb.getSheet(0); // 从工作区中取得页(Sheet)
int n =1;//数据行起始值
DateFormat df = new SimpleDateFormat("HH:mm:ss");
//规定早上最晚打卡时间
Date time = df.parse("10:00:00");
//记录一个周期(同一个地区、同一个人、同一天的上下班)数据
String firm="";//公司
String name="";//人员
String date="";//日期
String time_start="";//最早打卡时间
String time_end="";//最迟打卡时间
int hours=0;//上班时间
boolean isBegin=true;
while(n<sheet.getRows()){
//新起始行,记录最早打卡数据
if(isBegin){
firm=sheet.getCell(0,n).getContents();
name=sheet.getCell(1,n).getContents();
date=sheet.getCell(2,n).getContents();
time_start=sheet.getCell(3,n).getContents();
isBegin=false;
}
//找结束行,即最晚打卡数据
else{
String this0=sheet.getCell(0,n).getContents();
String this1=sheet.getCell(1,n).getContents();
String this2=sheet.getCell(2,n).getContents();
//判断和上一行是相同性质行
if(this0.equals(firm) && this1.equals(name) && this2.equals(date)){
//判断是最后一行
if(!sheet.getCell(0,n+1).getContents().equals(this0) || !sheet.getCell(1,n+1).getContents().equals(this1) && !sheet.getCell(2,n+1).getContents().equals(this2)){
time_end=sheet.getCell(3,n).getContents();
hours=(df.parse(time_end).getTime()-df.parse(time_start).getTime())/3600000;
//判断是否迟到
if(df.parse(time_start).getTime()>time.getTime()){
System.err.print("迟到");
}
//判断是否早退
else if(hours<8){
System.err.print("早退");
}
//输出员工上班下班时间 和上班多少小时
System.out.println(firm+"员工:" +name+date+ "上班时间:" + time_start + " " + "下班时间" + time_end + "上班了" + " " +hours.toString()+ "个小时" );
//当前数据结束,进行下一次统计
isBegin=true;
}
}
}
n++;
}
}
输出的下班时间不对。
我觉得可以在遍历的时候设置一个最小时间=24:00:00,一个最大时间:00:00:00
然后按天遍历,到下一天的时候输出最小时间就是上班,最大时间就是下班,再初始化遍历下一天。
public static void main(String[] args) throws ParseException {
//写的测试数据
List<String> date = new ArrayList<String>();
date.add("2017/10/26-10:43:41");
date.add("2017/10/26-14:07:48");
date.add("2017/10/26-18:07:48");
date.add("2017/10/27-10:45:41");
date.add("2017/10/27-14:07:48");
date.add("2017/10/27-15:07:48");
date.add("2017/10/27-17:07:48");
date.add("2017/10/27-18:10:48");
date.add("date-time");
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String day="";
String start = null,end = null;
for (int i = 0; i < date.size(); i++) {
String[] time = date.get(i).split("-");
//判断是否到了下一天
if (!day.equals(time[0])) {
if (start!=null&&end!=null) {
System.out.println("上班时间:"+start+"\t下班时间"+end);
}
//把每天第一条数据当做开始和结束时间
day = time[0];
start = time[1];
end = time[1];
}
//只对结束时间做更新
end = time[1];
}
}
package com.haxk.fangfa;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class zijixiedefangfa {
public static void main(String[] args) throws BiffException, IOException, ParseException {
String fileName = "D://Io//kaoqin.xls"; // Excel文件所在路径
File file = new File(fileName); // 创建文件对象
Workbook wb = Workbook.getWorkbook(file); // 从文件流中获取Excel工作区对象(WorkBook)
Sheet sheet = wb.getSheet(0); // 从工作区中取得页(Sheet)
int n =0;
Date min = null;
String firm = ""; //定义公司
String name = ""; //定义姓名
String day = ""; //定义日期
String time_start = ""; //定义最早时间
String time_end = ""; //定义最晚时间
List shuju = new ArrayList(); //定义总的传入集合
List bumen = new ArrayList(); //部门
List xingming = new ArrayList(); //姓名
List date = new ArrayList(); //日期
List earlyTime = new ArrayList(); //上班时间
List endTime = new ArrayList<Date>(); //下班时间
List workTime = new ArrayList(); //上班时间
List chidao = new ArrayList(); //迟到
List zaotui = new ArrayList(); //早退
//最晚上班时间
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
Date biaozhun = df.parse("10:00:00");
while(n<sheet.getRows()){
//这一天的最早上班时间
firm = sheet.getCell(0,shuju.size()).getContents();
name = sheet.getCell(1,shuju.size()).getContents();
day = sheet.getCell(2,shuju.size()).getContents();
time_start = sheet.getCell(3,shuju.size()).getContents();
bumen.add(sheet.getCell(0,shuju.size()).getContents());
xingming.add(sheet.getCell(1,shuju.size()).getContents());
date.add(sheet.getCell(2,shuju.size()).getContents());
earlyTime.add(sheet.getCell(3,shuju.size()).getContents());
//判断是否迟到
if(df.parse(sheet.getCell(3,shuju.size()).getContents()).getTime()>biaozhun.getTime()){
chidao.add("迟到");
}else{
chidao.add("");
}
while(n<sheet.getRows()){
//如果前三条数据一样的话 说明是同一天
if(sheet.getCell(0,n).getContents().equals(firm) && sheet.getCell(1,n).getContents().equals(name) && sheet.getCell(2,n).getContents().equals(day) ){
shuju.add(sheet.getCell(3,n).getContents());
}else{
break;
}
n++;
}
//当前三条数据不一样时 退出当前while循环 数组长度减1的下标就是下班时间
endTime.add(sheet.getCell(3,shuju.size()-1).getContents());
}
for (int i = 0; i < earlyTime.size(); i++) {
try {
workTime.add((df.parse((String) endTime.get(i)).getTime()-df.parse((String) earlyTime.get(i)).getTime())/3600000+"小时");
//判断是否早退
if((df.parse((String) endTime.get(i)).getTime()-df.parse((String) earlyTime.get(i)).getTime())/3600000>=8){
zaotui.add("");
}else{
zaotui.add("早退");
}
} catch (ParseException e) {
e.printStackTrace();
}
}
//关闭读取流
wb.close();
try {
//打开写入流
WritableWorkbook book = Workbook.createWorkbook(new File("D://Io//stu.xls"));
// 生成名为“sheet1”的工作表,参数0表示这是第一页
WritableSheet kaoqin = book.createSheet("kaoqin", 0);
//写入部门
for (int i = 0; i < bumen.size(); i++) {
Label label = new Label(0, i, (String) bumen.get(i));
// 将定义好的单元格添加到工作表中
kaoqin.addCell(label);
}
//写入姓名
for (int i = 0; i < xingming.size(); i++) {
Label label = new Label(1, i, (String) xingming.get(i));
kaoqin.addCell(label);
}
//写入日期
for (int i = 0; i < date.size(); i++) {
Label label = new Label(2, i, (String) date.get(i));
kaoqin.addCell(label);
}
//写入上班时间
for (int i = 0; i < earlyTime.size(); i++) {
Label label = new Label(3, i, (String) earlyTime.get(i));
kaoqin.addCell(label);
}
//写入下班时间
for (int i = 0; i < endTime.size(); i++) {
Label label = new Label(4, i, (String) endTime.get(i));
kaoqin.addCell(label);
}
//写入总共上班时间
for (int i = 0; i < workTime.size(); i++) {
Label label = new Label(5, i, (String) workTime.get(i)) ;
kaoqin.addCell(label);
}
//加入迟到
for (int i = 0; i < chidao.size(); i++) {
Label label = new Label(6, i, (String) chidao.get(i)) ;
kaoqin.addCell(label);
}
//写入早退
for (int i = 0; i < zaotui.size(); i++) {
Label label = new Label(7, i, (String) zaotui.get(i)) ;
kaoqin.addCell(label);
}
book.write(); //执行写入操作
book.close(); //关闭流
}catch (Exception e) {
System.out.println(e);
}
}
}