使用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; } } 表格样式:   运行结果:
点赞 评论

相关推荐

03-25 16:22
南华大学 Java
不敢追175女神:你是打了上千个招呼吧?😂
点赞 评论 收藏
分享
牛客网
牛客企业服务