代码改变世界

Excel文件处理类

2017-02-15 11:44  安迪大傻妞  阅读(155)  评论(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;
	}