关注
使用jxl.jar包读取excel数据
CallTimeCount类
public class CallTimeCount {
public static void main(String[] args){
ReadExcel readExcel = new ReadExcel("C:\\Users\\CAD\\Desktop\\telephonefee.xls");
System.out.print("主叫通话时长:");
readExcel.countTime(readExcel.getCallingTime());
System.out.println(" 通话次数:" + readExcel.getCallingInt());
System.out.print("被叫通话时长:");
readExcel.countTime(readExcel.getCalledTime());
System.out.println(" 通话次数:" + readExcel.getCalledInt());
System.out.print("总通知时长: " + readExcel.outputTimeCnt());
}
}
ReadExcel类
import jxl.*;
import java.io.*;
import java.util.*;
public class ReadExcel {
private int callingInt = 0; //主叫次数
private int calledInt = 0; // 被叫次数
private List<String> callingTime; // 主叫时长
private List<String> calledTime; // 被叫时长
private int day = 0;
private int hour = 0;
private int minute = 0;
private int second = 0;
// 读取表格中内容
public ReadExcel(String filePath){
callingTime = new ArrayList<>();
calledTime = new ArrayList<>();
Workbook readwb = null;
try{
// 构建Workbook对象,只读Workbook对象
// 直接从本地文件创建Workbook
InputStream inStream = new FileInputStream(filePath);
readwb = Workbook.getWorkbook(inStream);
// Sheet下标从0开始,获取第一张sheet表
Sheet readSheet = readwb.getSheet(0);
// 获取Sheet表中的总列数,总行数
int cntColumns = readSheet.getColumns();
int cntRows = readSheet.getRows();
// 获取单元格的对象引用
for(int i = 1;i < cntRows; i++){ //跳过第一行表格内容头
for(int j = 0; j < cntColumns; j++){
Cell cell = readSheet.getCell(j,i);
if(j == 2){
if(cell.getContents().equals("主叫")){ // 主叫统计
callingInt++;
callingTime.add(readSheet.getCell(j + 2,i).getContents());
}
else { // 被叫统计
calledInt++;
calledTime.add(readSheet.getCell(j + 2,i).getContents());
}
}
}
}
}
catch (Exception e){
e.printStackTrace();
}
finally{
readwb.close();
}
}
// 输出呼叫次数以及各自的时长
public void countTime(List<String> time){
int ad = 0,ah = 0,am = 0,as = 0;
String res = "";
for(String s : time){
int indexOfHour = s.indexOf("时");
int indexOfMinute = s.indexOf("分");
int indexOfSecond = s.indexOf("秒");
if(indexOfHour > 0){
ah += Integer.parseInt(s.substring(0,indexOfHour));
if(ah > 24){
ah %= 24;
ad++;
day++;
}
}
if(indexOfMinute > 0){
am += Integer.parseInt(s.substring(indexOfHour + 1,indexOfMinute));
if(am > 59){
am %= 60;
ah++;
}
}
if(indexOfSecond > 0){
as += Integer.parseInt(s.substring(indexOfMinute + 1,indexOfSecond));
if(as > 59){
as %= 60;
am++;
}
}
}
if(ad > 0)
res += ad + "天";
if(ah > 0)
res += ah + "时";
res += am + "分" + as + "秒";
System.out.print(res);
hour += ah;minute += am;second += as;
}
public String outputTimeCnt(){
String res = "";
if(day > 0)
res += day + "天";
res += hour + "时" + minute + "分" + second + "秒";
return res;
}
public int getCallingInt() {
return callingInt;
}
public int getCalledInt() {
return calledInt;
}
public List<String> getCallingTime() {
return callingTime;
}
public List<String> getCalledTime() {
return calledTime;
}
public int getDay() {
return day;
}
public int getHour() {
return hour;
}
public int getMinute() {
return minute;
}
public int getSeoncd() {
return second;
}
}
表格样式:
运行结果:
查看原帖
点赞 评论
相关推荐
点赞 评论 收藏
分享
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 面试问题记录 #
19363次浏览 330人参与
# 硬件人你反向读研了吗 #
39799次浏览 608人参与
# 京东TGT #
27301次浏览 151人参与
# 硬件人秋招的第一个offer #
65585次浏览 1081人参与
# 滴滴工作体验 #
23261次浏览 123人参与
# 非技术岗投递进展 #
137540次浏览 1222人参与
# 材料进Fab厂真的劝退吗? #
36078次浏览 158人参与
# 不考虑转正,实习多久合适 #
24096次浏览 118人参与
# 机械求职避坑tips #
41054次浏览 355人参与
# 互联网回暖,腾讯要招5000+人! #
263521次浏览 4889人参与
# 面试经验谈 #
12502次浏览 190人参与
# 机械只有转码才有出路吗? #
125876次浏览 1590人参与
# 职场新人生存指南 #
332198次浏览 7133人参与
# 面试吐槽bot #
2502次浏览 31人参与
# 异地恋该为对方跳槽吗 #
23338次浏览 119人参与
# 硬件人更看重稳定还是高薪 #
38512次浏览 203人参与
# vivo求职进展汇总 #
208605次浏览 1341人参与
# 25届如何提前做秋招准备? #
163914次浏览 2451人参与
# 你遇到过哪些神仙同事 #
69359次浏览 623人参与
# 租房找室友 #
27515次浏览 144人参与
# 深信服求职进展汇总 #
188730次浏览 1694人参与