poi 解析excel (test 版)

    
    /**
     * 解析excel  
     */
    @Test
    public void testPassExcel() throws Exception {
           // 从 template.xls 文件中读取数据,并保存到 ArrayList<Area> 中后打印输出。
            // 1、获取文件输入流
            InputStream inputStream = new FileInputStream("/home/sea/Downloads/no Mawb.xls");
            // 2、获取Excel工作簿对象
             Workbook workbook = filePathAndName.endsWith("xls")==true?new HSSFWorkbook(inputStream):new XSSFWorkbook(inputStream);//创建返回对象,把每行中的值作为一个数组,所有行作为一个集合返回
           TreeSet<String> listNOs = new TreeSet<String>();
            if(workbook != null)
            {
                //遍历,每一个sheet
                for(int sheetNum = 0;sheetNum < workbook.getNumberOfSheets();sheetNum++)
                {
                        //获得当前sheet工作表
                        Sheet sheet = workbook.getSheetAt(sheetNum);
                        if(sheet == null){
                            continue;
                        }
                        //获得当前sheet的开始行
                        int firstRowNum  = sheet.getFirstRowNum();
                        //获得当前sheet的结束行
                        int lastRowNum = sheet.getLastRowNum();
                        
                        //###########循环除了第一行的 每一行的数据 然后用list收集############
                        ArrayList<String> perlineData = new ArrayList<>();//
                        for(int rowNum = firstRowNum+1;rowNum <= lastRowNum;rowNum++)
                        {
                            //获得当前行
                            Row row = sheet.getRow(rowNum);
                            if(row == null){
                                continue;
                            }
                            //获得当前行的开始
                            int firstCellNum = row.getFirstCellNum();
                            //获得当前行的列数
                            int lastCellNum = row.getPhysicalNumberOfCells();
                            //循环当前行
                            for(int cellNum = firstCellNum; cellNum < lastCellNum;cellNum++)
                            {   
                                //获取每一列的数据
                                Cell cell = row.getCell(cellNum);
                                String cellValue = getCellValue(cell);
                                perlineData.add(cellValue);
                            }
                            //收集需要的数据
                            listNOs.add(perlineData.get(0));
                            perlineData.clear();
                            System.err.println("##### end line"+(rowNum+1)+" ########");
                        }
                }
                workbook.close();
            }
//            return list;
         System.err.println(listNOs.size());
         System.err.println(listNOs.size());
         System.err.println(listNOs.size());
         System.err.println(listNOs.size());
        
    }
    
    
    
    
       /**
     * 
     * @param cell
     * @return
     */
    public static String getCellValue(Cell cell){
        String cellValue = "";
        if(cell == null){
            return cellValue;
        }
        //把数字当成String来读,避免出现1读成1.0的情况
        if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
            cell.setCellType(Cell.CELL_TYPE_STRING);
        }
        //判断数据的类型
        switch (cell.getCellType()){
            case Cell.CELL_TYPE_NUMERIC: //数字
                cellValue = String.valueOf(cell.getNumericCellValue());
                break;
            case Cell.CELL_TYPE_STRING: //字符串
                cellValue = String.valueOf(cell.getStringCellValue());
                break;
            case Cell.CELL_TYPE_BOOLEAN: //Boolean
                cellValue = String.valueOf(cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA: //公式
                cellValue = String.valueOf(cell.getCellFormula());
                break;
            case Cell.CELL_TYPE_BLANK: //空值 
                cellValue = "";
                break;
            case Cell.CELL_TYPE_ERROR: //故障
                cellValue = "非法字符";
                break;
            default:
                cellValue = "未知类型";
                break;
        }
        return cellValue;
    }
    
 
原文地址:https://www.cnblogs.com/lshan/p/11996001.html