EXCEL读取

public Map getExcelContentByInputStream(InputStream is){
  try{
   HSSFWorkbook   workbook  =  new   HSSFWorkbook(is);//创建对Excel工作簿文件的引用
      for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) {
       if (null != workbook.getSheetAt(numSheets)) {
        HSSFSheet aSheet = workbook.getSheetAt(numSheets);//获得一个sheet
        for (int rowNumOfSheet = 0; rowNumOfSheet <= aSheet.getLastRowNum(); rowNumOfSheet++){
         System.out.println("行数=/t"+rowNumOfSheet);
         if (null != aSheet.getRow(rowNumOfSheet)){
          HSSFRow aRow = aSheet.getRow(rowNumOfSheet); //获得一个行
          StringBuffer content = new StringBuffer();
          for (short cellNumOfRow = 0; cellNumOfRow <= aRow.getLastCellNum(); cellNumOfRow++){
           if (null != aRow.getCell(cellNumOfRow)){
            HSSFCell aCell = aRow.getCell(cellNumOfRow);//获得列值
            if (aCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
             System.out.print("/t"+cellNumOfRow+"/t"+aCell.getStringCellValue());
             content.append(aCell.getStringCellValue());
            } else if (aCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
             System.out.print("/t"+cellNumOfRow+"/t"+aCell.getNumericCellValue());
             content.append(""+aCell.getNumericCellValue());
            }
           }
          }
          System.out.println("!!"+content);
         }
        }
       }
      }
  }catch(Exception e){
   e.printStackTrace();
  }
  return null;
 }

当excel某些栏有下拉列表时可能会报错.解决方法是将POI换成3.0以上的.或者在保存EXCEL时焦点不要停放在有下拉列表的栏中.

你可能感兴趣的:(EXCEL读取)