demo-1

my_xiajibagao@TOC

package caip;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Testpoi {

    public static void main(String[] args) {
        Workbook wb =null;
        Sheet sheet = null;
        Row row = null;
        List<Map<String,String>> list = null;
        String cellData = null;
        String filePath = "C:\\Users\\Administrator\\Desktop\\往期中奖号码.xls";
      //  String columns[] = {"a","b","c","d","e","f","g","h"};
        wb = readExcel(filePath);
   
        
        int [][] nums=new int[1400][8];
        
        
        if(wb != null){
            //用来存放表中数据
            list = new ArrayList<Map<String,String>>();
            //获取第一个sheet
            sheet = wb.getSheetAt(0);
            //获取最大行数
            int rownum = sheet.getPhysicalNumberOfRows();
            System.out.println("rownum="+rownum);
            //获取第一行
            row = sheet.getRow(0);
            //获取最大列数
          //  int colnum = row.getPhysicalNumberOfCells();
            int colnum = 8;
            System.out.println("colnum="+colnum);
            for (int i = 0; i<rownum; i++) {
                //Map<String,String> map = new LinkedHashMap<String,String>();
                row = sheet.getRow(i);
                if(row !=null){
                    for (int j=0;j<colnum;j++){
                        cellData = (String) getCellFormatValue(row.getCell(j));
                        nums[i][j]=Integer.parseInt(cellData);
                      //  System.out.println( "nums["+i+"]["+j+"]="+cellData);
                       // map.put(columns[j], cellData);
                    }
                }else{
                    break;
                }          
            }
        }
 
        for(int i=0;i<1384;i++) {
        	System.out.println();
        	for(int j=0;j<8;j++)
        		System.out.print(nums[i][j]+"    ");
        	System.out.println();
        	
        }
        
        
        Map<Integer,Integer> map1=new HashMap<Integer,Integer>();
        Map<Integer,Integer> map2=new HashMap<Integer,Integer>();
        Map<Integer,Integer> map3=new HashMap<Integer,Integer>();
        Map<Integer,Integer> map4=new HashMap<Integer,Integer>();
        Map<Integer,Integer> map5=new HashMap<Integer,Integer>();
        Map<Integer,Integer> map6=new HashMap<Integer,Integer>();
        Map<Integer,Integer> map7=new HashMap<Integer,Integer>();
        
        for(int i=1;i<=35;i++) {
        	map1.put(i, 0);
        	map2.put(i, 0);
        	map3.put(i, 0);
        	map4.put(i, 0);
        	map5.put(i, 0);
        	map6.put(i, 0);
        	map7.put(i, 0);   	    	
        }
        
        for(int i=0;i<1384;i++) {
        	
        	for(int j=1;j<8;j++)
        		switch(j) {
        			case 1:map1.put(nums[i][j], map1.get(nums[i][j])+1);continue;
        			case 2:map2.put(nums[i][j], map2.get(nums[i][j])+1);continue;
        			case 3:map3.put(nums[i][j], map3.get(nums[i][j])+1);continue;
        			case 4:map4.put(nums[i][j], map4.get(nums[i][j])+1);continue;
        			case 5:map5.put(nums[i][j], map5.get(nums[i][j])+1);continue;
        			case 6:map6.put(nums[i][j], map6.get(nums[i][j])+1);continue;
        			case 7:map7.put(nums[i][j], map7.get(nums[i][j])+1);continue;
        	
        			default: break;
        		      		
        		} 	
        }
        
        
  
        	maxKeyValuePrint("第1列的出现最高频率",map1);
        	maxKeyValuePrint("第2列的出现最高频率",map2);
        	maxKeyValuePrint("第3列的出现最高频率",map3);
        	maxKeyValuePrint("第4列的出现最高频率",map4);
        	maxKeyValuePrint("第5列的出现最高频率",map5);
        	maxKeyValuePrint("第6列的出现最高频率",map6);
        	maxKeyValuePrint("第7列的出现最高频率",map7);
        	
        	
    }
    
    

	public static void maxKeyValuePrint(String mesg,Map<Integer,Integer> map) {
    	
    	 Set<Entry<Integer, Integer>> set=	map.entrySet();
    	
    	 int max=-1;
    	 int key=-1;
    	 
    	for(Entry<Integer, Integer> en:set) {
    		if(en.getValue()>max) {
    			max=en.getValue();
    			key=en.getKey();
    			
    		}
    		
    	}
    	
    	System.out.println(mesg+"    : key="+key+";value="+max);
    	
    }
    
    
    
    //读取excel
    public static Workbook readExcel(String filePath){
        Workbook wb = null;
        if(filePath==null){
            return null;
        }
        String extString = filePath.substring(filePath.lastIndexOf("."));
        InputStream is = null;
        try {
            is = new FileInputStream(filePath);
            if(".xls".equals(extString)){
                return wb = new HSSFWorkbook(is);
            }else if(".xlsx".equals(extString)){
                return wb = new XSSFWorkbook(is);
            }else{
                return wb = null;
            }
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return wb;
    }
    public static Object getCellFormatValue(Cell cell){
        Object cellValue = null;
        if(cell!=null){
            //判断cell类型
            switch(cell.getCellType()){
            case Cell.CELL_TYPE_NUMERIC:{
                cellValue = String.valueOf(cell.getNumericCellValue());
                break;
            }
            case Cell.CELL_TYPE_FORMULA:{
                //判断cell是否为日期格式
                if(DateUtil.isCellDateFormatted(cell)){
                    //转换为日期格式YYYY-mm-dd
                    cellValue = cell.getDateCellValue();
                }else{
                    //数字
                    cellValue = String.valueOf(cell.getNumericCellValue());
                }
                break;
            }
            case Cell.CELL_TYPE_STRING:{
                cellValue = cell.getRichStringCellValue().getString();
                break;
            }
            default:
                cellValue = "";
            }
        }else{
            cellValue = "";
        }
        return cellValue;
    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值