package com.baorant; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.List; public class GetExcelData { public static void main(String[] args) { File csv = new File("csv//1205.csv"); // CSV文件路径 BufferedReader br = null; try { br = new BufferedReader(new FileReader(csv)); } catch (FileNotFoundException e) { e.printStackTrace(); } String line = ""; String everyLine = ""; try { List<String> allString = new ArrayList<>(); List<Integer> allInt = new ArrayList<>(); int[] records = new int[24 * 60 / 5];//存取每个时间段的通过数量,可以获得最大分钟后得到长度; while ((line = br.readLine()) != null) // 读取到的内容给line变量 { // System.out.println(line); everyLine = line; if(!everyLine.contains(" ")){ continue;//有些文件中时间模式没有空格 } String tem = everyLine.substring(everyLine.lastIndexOf(" ") + 1);// 获取每一行空格后面的部分; String tem2 = tem.substring(0, tem.indexOf(",", tem.indexOf(",") - 1));// 获得每一行逗号前面的部分; // s.substring(0, s.indexOf(".",s.indexOf(".")-1 )) tem2 = tem2.replace(""","");//去掉字符串中可能多出的引号 System.out.println(tem2); System.out.println(tem2.length()); String hourStr;// 获得字符串小时部分 String minuteStr;// 获得字符串分钟部分 if(tem2.length() == 5){ hourStr = tem2.substring(0, 2); minuteStr = tem2.substring(3, 5); }else if(tem2.length() == 4){ hourStr = tem2.substring(0, 1); minuteStr = tem2.substring(2, 4); }else if(tem2.length() == 7){//时间为7位 hourStr = tem2.substring(0, 1); minuteStr = tem2.substring(2, 4); }else{//时间为8位 hourStr = tem2.substring(0, 2); minuteStr = tem2.substring(3, 5); } if (!tem2.contains("SSID")) { String subHourStr; String subMinuteStr; if(hourStr.length() == 2) { if(hourStr.charAt(0) == '0'){ subHourStr = hourStr.substring(1, 2); }else{ subHourStr = hourStr.substring(0, 2); } }else{ subHourStr = hourStr.substring(0, 1); } if(minuteStr.length() == 2) { if(minuteStr.charAt(0) == '0') { subMinuteStr = minuteStr.substring(1, 2); }else{ subMinuteStr = minuteStr.substring(0, 2); } }else { subMinuteStr = minuteStr.substring(0, 1); } Integer hours = Integer.valueOf(subHourStr);//小时数 // System.out.println(hours); int minutes = Integer.valueOf(subMinuteStr);//分钟数 // System.out.println(minutes); int minutestotal = 60 * hours + minutes;//当前时间总的分钟数 System.out.println(minutestotal); allInt.add(minutes); int n = minutestotal/5; records[n] = records[n] + 1; System.out.println("n:" + n + "records[minutestotal/5]:" + records[n]); } allString.add(tem2); } System.out.println("csv表格中所有行数:" + allString.size()); System.out.println(allInt.size()); for(int i = 0; i < records.length; i++){ System.out.print(records[i] + " "); } } catch (Exception e) { e.printStackTrace(); } } }
该程序主要获得了excel表格中的每一行数据后获得其中的时间,然后按自己需要的格式进行处理得到想要的数据。