• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
守恪
恪守初心
博客园    首页    新随笔    联系   管理     
poi 读取 excel(.xlsx) 2007及以上版本

1.注意的一点是sh.getLastRowNum(),比实际的行数少一行

涉及到的包:

public List<Map> getData(InputStream ins) throws Exception {
        List<Map> list = new ArrayList<Map>(); //存放excel的data 每个list是sheet
        //打开XSSFWorkbook
        XSSFWorkbook wb = new XSSFWorkbook(ins);//获取HSSWorkbook
        int sheetsNum = wb.getNumberOfSheets();//excel sheet
         //解析sheet的数据      
        for(int i = 0 ; i < sheetsNum ; i++){
            Map map = new HashMap();
            int columnNum = 0;
            XSSFSheet sh = wb.getSheetAt(i);//得到sheet
            //解析行
            int rowNum = 0;
            rowNum = sh.getLastRowNum() + 1;
            for(int j = 0 ; j <= sh.getLastRowNum() ; j++){
                XSSFRow row = sh.getRow(j);
                if(row == null){
                    rowNum --;
                    continue;
                }
                //解析列
                for(int z = 0; z < row.getLastCellNum(); z++){
                    XSSFCell 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" ,rowNum);
                map.put("columnNum", columnNum);
                list.add(map);
            }
        }
        return list;
    }

 

posted on 2015-02-06 15:06  守恪  阅读(421)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3