zoukankan      html  css  js  c++  java
  • poi 读取 excel (.xls) 97-2003

    1.sh.getLastRowNum() 比行数少1

    private List<Map>  getData(File file) throws Exception{
            List<Map> list = new ArrayList<Map>(); //存放excel的data 每个list是sheet
            
            //获取IO流
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
            //打开HSSFWorkbook
            POIFSFileSystem fs = new POIFSFileSystem(in);
            HSSFWorkbook wb = new HSSFWorkbook(fs);//获取HSSWorkbook
            int sheetsNum = wb.getNumberOfSheets();//excel sheet
            //解析sheet的数据
            for(int i = 0 ; i < sheetsNum ; i++){
                Map map = new HashMap();
                HSSFSheet sh = wb.getSheetAt(i);//得到sheet
                //解析行
                int columnNum = 0;
                for(int j = 0 ; j <= sh.getLastRowNum() ; j++){
                    HSSFRow row = sh.getRow(j);
                    //解析列
                    for(int z = 0; z < row.getLastCellNum(); z++){
                        HSSFCell cell = row.getCell(z);
                        
                        if(cell != null){
                            if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                                 map.put(j + "" + z, cell.getNumericCellValue());
                            }
                            if(cell.getCellType() == HSSFCell.CELL_TYPE_STRING){
                                 map.put(j + "" + z, cell.getStringCellValue());
                            }
                        }
                    }
                    columnNum = row.getLastCellNum();
                }
                if(sh.getLastRowNum() != 0 && columnNum != 0){
                    map.put("rowNum" ,sh.getLastRowNum() + 1);
                    map.put("columnNum", columnNum);
                    list.add(map);
                }
            }
            return list;
        }
  • 相关阅读:
    洛谷 P1037 产生数
    【bzoj】 1070: [SCOI2007]修车
    【bzoj】 1066: [SCOI2007]蜥蜴 (网络流)
    开发环境搭建
    数据库设计
    sql优化实例(用左连接)
    泛型T和通配符?的区别
    Nginx配置优化
    Tomcat优化
    Nginx反向代理配置
  • 原文地址:https://www.cnblogs.com/jinTaylor/p/4277259.html
Copyright © 2011-2022 走看看