eagle

          學無止境,細節決定成敗.
          posts - 12, comments - 11, trackbacks - 0, articles - 2
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          poi解析excel列子

          Posted on 2016-03-18 09:51 月下孤城 閱讀(266) 評論(0)  編輯  收藏 所屬分類: java
          工作中有時會用java程序分析excel文件內容。本文介紹使用poi解析excel 2007及以上版本的方法,以備參考。

           1 /**
           2      * poi解析excel    sample
           3      * 針對2007及以上版本 使用XSSF解析
           4      * @throws EncryptedDocumentException
           5      * @throws InvalidFormatException
           6      * @throws IOException
           7      */
           8     public void parseExcel() throws EncryptedDocumentException, InvalidFormatException, IOException{
           9         InputStream is = new FileInputStream("e:\\excel.xlsx");
          10         Workbook workbook = WorkbookFactory.create(is);  
          11         Sheet sheet = null;
          12         for (int i = 0; i < workbook.getNumberOfSheets(); i++) {// 獲取每個Sheet表
          13             sheet = workbook.getSheetAt(i);
          14             String sheetName = sheet.getSheetName();
          15             if(workbook.isSheetHidden(i)){
          16                 //判斷sheet頁是否被隱藏
          17                 System.out.println("sheet="+sheetName+", is hidden.");
          18                 continue;
          19             }
          20             for (int j = 0; j < sheet.getPhysicalNumberOfRows(); j++) {// 獲取每行
          21                 if(j==0)    //第一行title,不處理
          22                     continue;
          23                 Row row = sheet.getRow(j);
          24                 if(row == null)
          25                     continue;
          26                 //處理每行數據
          27                 try {
          28                     if(row.getZeroHeight()){
          29                         //行是否被隱藏
          30                         System.out.println("---Sheet表["+sheetName+"],第" + j + "行被隱藏,不處理---");
          31                         continue;
          32                     }
          33                     int columns = row.getPhysicalNumberOfCells();
          34                     for(int c=0;c<columns;c++){
          35                         Cell cell = row.getCell(c);
          36                         //TODO: busyness process
          37                     }
          38                 } catch (Exception e) {
          39                     System.out.println("---Sheet表["+sheetName+"],第" + j + "行處理出錯.---");
          40                     e.printStackTrace();
          41                     throw new RuntimeException(e);
          42                 }
          43                 
          44                 System.out.println("---Sheet表["+sheetName+"],第" + j + "行處理完畢---");
          45             }    
          46         }    
          47     }



          ---------------------
          月下孤城
          mail:eagle_daiqiang@sina.com
          主站蜘蛛池模板: 黄浦区| 梁山县| 蒙阴县| 普兰县| 博客| 齐河县| 卢龙县| 定南县| 奉节县| 深州市| 剑川县| 西畴县| 墨玉县| 姜堰市| 拜泉县| 乌拉特前旗| 当雄县| 崇阳县| 岳阳县| 封丘县| 新沂市| 鄂尔多斯市| 陕西省| 宁南县| 麻城市| 洪洞县| 鹤庆县| 阳高县| 宣威市| 德清县| 雷波县| 永济市| 农安县| 嘉祥县| 丹江口市| 大英县| 北辰区| 六安市| 崇明县| 铁岭市| 牙克石市|