ExcelReader类 import java.io.File; import java.io.IOException; import java.util.ArrayList; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; /** * *jxl操作excel包括对象Workbook,Sheet,Cell *一个excel就对应一个Workbook对象, *一个Workbook可以有多个Sheet对象 *一个Sheet对象可以有多个Cell对象 * @author Matrix42 * */ public class ExcelReader { private File excelFile; private Workbook workbook; private Sheet sheet; private int sheetNumber; //可以用String或File构造一个ExcelReader对象 public ExcelReader(String filePath,int sheetNumber){ this.excelFile = new File(filePath); this.sheetNumber = sheetNumber; } public ExcelReader(File excelFile,int sheetNumber){ this.excelFile = excelFile; this.sheetNumber = sheetNumber; } //初始化 public void init() throws BiffException, IOException{ this.workbook = Workbook.getWorkbook(excelFile); this.sheet = workbook.getSheet(sheetNumber); } //获取columnNumber列的所有行,两个重载 //取一个区间内的 public ArrayList<String> getColumns(int columnNumber,int start,int end){ int rows = sheet.getRows(); int columns = sheet.getColumns(); if(columns<0||columnNumber>columns-1){ throw new IllegalArgumentException("column not in range!"); } if(start<0||end>rows-1){ throw new IllegalArgumentException("row not in range!"); } ArrayList<String> list = new ArrayList<String>(); for(int i=start;i<=end;i++){ Cell cell = sheet.getCell(columnNumber,i); list.add(cell.getContents()); } return list; } //从start到最后 public ArrayList<String> getColumns(int columnNumber,int start){ int rows = sheet.getRows(); int columns = sheet.getColumns(); int end = rows - 1; if(columns<0||columnNumber>columns-1){ throw new IllegalArgumentException("column not in range!"); } if(start<0){ throw new IllegalArgumentException("row not in range!"); } ArrayList<String> list = new ArrayList<String>(); for(int i=start;i<=end;i++){ Cell cell = sheet.getCell(columnNumber,i); list.add(cell.getContents()); } return list; } } 时间转换类 public class Time { private int d; private int h; private int m; private int s; //解析出天,时,分,秒,与之前的结果相加 public void add(String time) { int indexOfDay = time.indexOf("天"); int indexOfHour = time.indexOf("时"); int indexOfMinute = time.indexOf("分"); int indexOfSecond = time.indexOf("秒"); if(indexOfDay>0){ d += Integer.parseInt(time.substring(0,indexOfDay)); } if(indexOfHour>0){ if(indexOfDay>0){ h += Integer.parseInt(time.substring(indexOfDay+1,indexOfHour)); }else { h += Integer.parseInt(time.substring(0,indexOfHour)); } } if(indexOfMinute>0){ if(indexOfHour>0){ m += Integer.parseInt(time.substring(indexOfHour+1,indexOfMinute)); }else { m += Integer.parseInt(time.substring(0,indexOfMinute)); } } if(indexOfSecond>0){ if(indexOfMinute>0){ s += Integer.parseInt(time.substring(indexOfMinute+1,indexOfSecond)); }else { s += Integer.parseInt(time.substring(indexOfMinute+1,indexOfSecond)); } } } //转换时间 public String format(){ int tmp; tmp = s/60; s %= 60; m = m + tmp; tmp = m/60; m %= 60; h = h + tmp; tmp = h/24; h %= 24; d = d + tmp; String t = ""; if(d!=0){ t += d+"天"; } if(h!=0){ t += h+"时"; } if(m!=0){ t += m+"分"; } if(s!=0){ t += s+"秒"; } return t; } public int getD() { return d; } public int getH() { return h; } public int getM() { return m; } public int getS() { return s; } } import java.io.IOException; import java.util.ArrayList; import jxl.read.biff.BiffException; public class Client { public static void main(String[] args) throws BiffException, IOException { //读取第1个Sheet ExcelReader reader = new ExcelReader("D:/01.xls", 0); reader.init(); //读取第4列 ArrayList<String> result = reader.getColumns(3, 1); Time time = new Time(); //把时间加一起 for (String string : result) { time.add(string); } System.out.println("您的通话总时长为:"+time.format()+"!"); } } 使用jxl库解析的excel excel: 运行结果:
点赞 5

相关推荐

牛客网
牛客网在线编程
牛客网题解
牛客企业服务