eagle

          學(xué)無止境,細(xì)節(jié)決定成敗.
          posts - 12, comments - 11, trackbacks - 0, articles - 2
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          poi解析excel列子

          Posted on 2016-03-18 09:51 月下孤城 閱讀(265) 評論(0)  編輯  收藏 所屬分類: java
          工作中有時會用java程序分析excel文件內(nèi)容。本文介紹使用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                 //處理每行數(shù)據(jù)
          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
          主站蜘蛛池模板: 高雄县| 奉贤区| 精河县| 康平县| 苏尼特左旗| 海盐县| 桓台县| 宜章县| 玉龙| 承德县| 武强县| 平定县| 城步| 沙湾县| 云龙县| 崇州市| 滕州市| 依兰县| 鸡泽县| 麻栗坡县| 百色市| 宿州市| 哈密市| 龙州县| 勃利县| 蒲江县| 西城区| 海丰县| 民乐县| 华安县| 宜川县| 化德县| 新郑市| 蓬安县| 景泰县| 西盟| 镇沅| 双柏县| 两当县| 巩留县| 夏河县|