首页 > 其他 > 详细

Excel文件处理类

时间:2017-02-15 12:14:59      阅读:200      评论:0      收藏:0      [点我收藏+]
/**
	 * 解析系统黑名单
	 * @param excelFile
	 * 		导入的Excel文件
	 * @param list
	 * 		待添加的数据列表
	 * @return
	 */
	public static List<String> readSysBlackPhones(File excelFile,Integer excelVersion){ 
		
		List<String> datalist = null;
		
		try {   	       
	        
			InputStream is = new FileInputStream(excelFile);   	 
			
			if(excelVersion==2003){
				
				//2003版本
				HSSFWorkbook work = new HSSFWorkbook(is); 	             
		        
				//获取第一个工作表		            
		        Sheet sheet = work.getSheetAt(0);  	        
		        
		        //获取行数
		        int rowNum = sheet.getLastRowNum();	        
		        Row row = null;
		        int colIndex=0;
		        if(rowNum>1){
		        	datalist = new ArrayList<String>();
			        for (int i=1; i<=rowNum; i++) { 		            	 
			        	row = sheet.getRow(i);	
			        	Cell cell = row.getCell(colIndex);
	 		        	if(cell==null){
	 		        		continue;
	 		        	}
	 		        	String phone = getStringCellValue(cell);
	 		        	if(StringUtils.isBlank(phone)){
	 		        		continue;
	 		        	}
	 		        	if(phone.length()<11){
	 		        		continue;
	 		        	} 		        	
	 		        	datalist.add(phone);
			        }
		        }
  
		        is.close();
		        
	        }else if (excelVersion==2007){
	        	//2007版本
				XSSFWorkbook work = new XSSFWorkbook(is); 	             
		        
				//获取第一个工作表		            
		        Sheet sheet = work.getSheetAt(0);  	        
		        
		        //获取行数
		        int rowNum = sheet.getLastRowNum();	        
		        Row row = null;
		        int colIndex=0;
		        if(rowNum>1){
		        	datalist = new ArrayList<String>();
			        for (int i = 1; i <= rowNum; i++) { 		            	 
			        	row = sheet.getRow(i);	
			        	Cell cell = row.getCell(colIndex);
	 		        	if(cell==null){
	 		        		colIndex=0;
	 		        		continue;
	 		        	}
	 		        	String phone = getStringCellValue(cell);
	 		        	if(StringUtils.isBlank(phone)){
	 		        		colIndex=0;
	 		        		continue;
	 		        	}
	 		        	if(phone.length()<11){
	 		        		colIndex=0;
	 		        		continue;
	 		        	} 		        	
	 		        	datalist.add(phone);
			        }
		        }
  
		        is.close();
	        }
        } catch (Exception e) {   
            log.info("解析会员Excel数据文件,出现异常");   
            e.printStackTrace();             
        }   
	   	return datalist;
	}
/**
	 * 解析顾客群号码导入名单
	 * @param excelFile
	 * 		导入的Excel文件
	 * @param excelVersion
	 * 		excel表格版本
	 * @return
	 */
	public static Set<String> readGroupTels(File excelFile,Integer excelVersion){ 
		
		Set<String> datalist = null;
		
		try {   	       
	        
			InputStream is = new FileInputStream(excelFile);   	 
			
			if(excelVersion==2003){
				
				//2003版本
				HSSFWorkbook work = new HSSFWorkbook(is); 	             
		        
				//获取第一个工作表		            
		        Sheet sheet = work.getSheetAt(0);  	        
		        
		        //获取行数
		        int rowNum = sheet.getLastRowNum();	        
		        Row row = null;
		        int colIndex=0;
		        if(rowNum>1){
		        	datalist = new HashSet<String>();
			        for (int i=1; i <=rowNum; i++) { 		            	 
			        	row = sheet.getRow(i);	
			        	Cell cell = row.getCell(colIndex);
	 		        	if(cell==null){
	 		        		continue;
	 		        	}
	 		        	String phone = null;
	 		        	if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC){
	 		        		DecimalFormat df = new DecimalFormat("###########");
	 		        		phone = df.format(cell.getNumericCellValue());
			        	}else if(cell.getCellType()==Cell.CELL_TYPE_STRING){
			        		phone = cell.getStringCellValue();
			        	}
	 		        	System.out.println(phone+"jkj");
	 		        	if(StringUtils.isBlank(phone)){
	 		        		continue;
	 		        	}
	 		        	if(phone.length()<11){
	 		        		continue;
	 		        	} 		        	
	 		        	datalist.add(phone);
			        }
		        }
  
		        is.close();
		        
	        }else if (excelVersion==2007){
	        	//2007版本
				XSSFWorkbook work = new XSSFWorkbook(is); 	             
		        
				//获取第一个工作表		            
		        Sheet sheet = work.getSheetAt(0);  	        
		        
		        //获取行数
		        int rowNum = sheet.getLastRowNum();	        
		        Row row = null;
		        int colIndex=0;
		        if(rowNum>1){
		        	datalist = new HashSet<String>();
			        for (int i =1; i <= rowNum; i++) { 		            	 
			        	row = sheet.getRow(i);	
			        	Cell cell = row.getCell(colIndex);
	 		        	if(cell==null){
	 		        		colIndex=0;
	 		        		continue;
	 		        	}
	 		        	String phone = "";
	 		        	if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC){
	 		        		DecimalFormat df = new DecimalFormat("###########");
	 		        		phone = df.format(cell.getNumericCellValue());
			        	}else if(cell.getCellType()==Cell.CELL_TYPE_STRING){
			        		phone = cell.getStringCellValue();
			        	}	 		        	
	 		        	System.out.println(phone+"jkjko");
	 		        	if(StringUtils.isBlank(phone)){
	 		        		colIndex=0;
	 		        		continue;
	 		        	}
	 		        	if(phone.length()<11){
	 		        		colIndex=0;
	 		        		continue;
	 		        	} 
	 		        	
	 		        	datalist.add(phone);
			        }
		        }
  
		        is.close();
	        }
        } catch (Exception e) {   
            log.info("解析Excel数据文件,出现异常");   
            e.printStackTrace();             
        }   
	   	return datalist;
	}

  

  

Excel文件处理类

原文:http://www.cnblogs.com/lqlzh/p/6400756.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!