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; }