用的POI解析xls; 代码很短,正则表达式不熟练,不然能短三分之一... 而且坑爹的移动服务密码搞了我半个小时。 需要POI包的可以去网上找找,找不到私聊我,QQ:613258200,备注牛客。 Excel文件: 代码: import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; public class POI_test { public static void main(String[] args) throws FileNotFoundException, IOException { getSumTimes(); } public static void getSumTimes() throws FileNotFoundException, IOException { // 得到Excel常用对象 POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream( "f:/yidong.xls")); // 得到Excel工作簿对象 HSSFWorkbook wb = new HSSFWorkbook(fs); // 得到Excel工作表对象 HSSFSheet sheet = wb.getSheetAt(0); int maxRowNums = sheet.getLastRowNum(); int second = 0; int minute = 0; int hour = 0; for (int i = 1; i < maxRowNums; i++) { // 得到Excel工作表的行 HSSFRow row = sheet.getRow(i); // 得到Excel工作表指定行的单元格 HSSFCell cell = row.getCell(4); String time = cell.getStringCellValue(); for (int j = time.length() - 1; j >= 0; j--) { if (time.charAt(j) == '秒') { StringBuffer se = new StringBuffer(); for (int k = j - 1; k >= 0 && time.charAt(k) >= 48 && time.charAt(k) <= 57; k--) { se.append(time.charAt(k)); } se.reverse(); int se_int = Integer.parseInt(se.toString()); second += se_int; } if (time.charAt(j) == '分') { StringBuffer min = new StringBuffer(); for (int k = j - 1; k >= 0 && time.charAt(k) >= 48 && time.charAt(k) <= 57; k--) { min.append(time.charAt(k)); } min.reverse(); int min_int = Integer.parseInt(min.toString()); minute += min_int; } if (time.charAt(j) == '时') { StringBuffer hou = new StringBuffer(); for (int k = j - 1; k >= 0 && time.charAt(k) >= 48 && time.charAt(k) <= 57; k--) { hou.append(time.charAt(k)); } hou.reverse(); int hour_int = Integer.parseInt(hou.toString()); hour += hour_int; } } } minute = minute + second / 60; second = second % 60; hour = hour + minute / 60; minute = minute % 60; System.out.println("总通话时间为:" + hour + " 小时 " + minute + " 分钟 " + second + " 秒 "); } } 运行效果:
点赞 2

相关推荐

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