JAVA使用POI讀取EXCEL文件的簡單model

           1 package poi;
           2 import java.io.FileInputStream;
           3 import java.io.IOException;
           4 import java.io.InputStream;
           5 import java.util.Iterator;
           6 import org.apache.poi.hssf.usermodel.HSSFCell;
           7 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
           8 import org.apache.poi.ss.usermodel.Cell;
           9 import org.apache.poi.ss.usermodel.Row;
          10 import org.apache.poi.ss.usermodel.Sheet;
          11 import org.apache.poi.ss.usermodel.Workbook;
          12 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
          13  
          14 public class ReadExcel001 {
          15     public static void main(String[] args) {
          16         readXml("D:/test.xlsx");
          17         System.out.println("-------------");
          18         readXml("d:/test2.xls");
          19      }
          20     public static void readXml(String fileName){
          21         boolean isE2007 = false;    //判斷是否是excel2007格式
          22         if(fileName.endsWith("xlsx"))
          23             isE2007 = true;
          24         try {
          25             InputStream input = new FileInputStream(fileName);    //建立輸入流
          26             Workbook wb  = null;
          27             //根據文件格式(2003或者2007)來初始化
          28             if(isE2007)
          29                 wb = new XSSFWorkbook(input);
          30             else
          31                 wb = new HSSFWorkbook(input);
          32             Sheet sheet = wb.getSheetAt(0);        //獲得第一個表單
          33             Iterator<Row> rows = sheet.rowIterator();    //獲得第一個表單的迭代器
          34             while (rows.hasNext()) {
          35                 Row row = rows.next();    //獲得行數據
          36                 System.out.println("Row #" + row.getRowNum());    //獲得行號從0開始
          37                 Iterator<Cell> cells = row.cellIterator();    //獲得第一行的迭代器
          38                 while (cells.hasNext()) {
          39                     Cell cell = cells.next();
          40                     System.out.println("Cell #" + cell.getColumnIndex());
          41                     switch (cell.getCellType()) {    //根據cell中的類型來輸出數據
          42                     case HSSFCell.CELL_TYPE_NUMERIC:
          43                         System.out.println(cell.getNumericCellValue());
          44                         break;
          45                     case HSSFCell.CELL_TYPE_STRING:
          46                         System.out.println(cell.getStringCellValue());
          47                         break;
          48                     case HSSFCell.CELL_TYPE_BOOLEAN:
          49                         System.out.println(cell.getBooleanCellValue());
          50                         break;
          51                     case HSSFCell.CELL_TYPE_FORMULA:
          52                         System.out.println(cell.getCellFormula());
          53                         break;
          54                     default:
          55                         System.out.println("unsuported sell type");
          56                     break;
          57                     }
          58                 }
          59             }
          60         } catch (IOException ex) {
          61             ex.printStackTrace();
          62         }
          63     }
          64 }


          posted on 2015-01-13 09:57 魯勝迪 閱讀(341) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2015年1月>
          28293031123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導航

          統計

          常用鏈接

          留言簿(4)

          隨筆分類

          隨筆檔案

          文章分類

          新聞分類

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 镇雄县| 元江| 平湖市| 兴城市| 长白| 普定县| 北安市| 蓬莱市| 阿瓦提县| 寿光市| 万盛区| 定西市| 二连浩特市| 应用必备| 兴国县| 尤溪县| 大丰市| 蓬溪县| 沈丘县| 铁岭县| 中宁县| 巴南区| 临江市| 哈密市| 霍山县| 修文县| 池州市| 桐梓县| 安达市| 防城港市| 黔西| 北安市| 德州市| 亚东县| 出国| 衡水市| 阜南县| 龙南县| 潜山县| 林州市| 衢州市|