2015年1月13日

          JAVA使用POI讀取EXCEL文件的簡(jiǎn)單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             //根據(jù)文件格式(2003或者2007)來初始化
          28             if(isE2007)
          29                 wb = new XSSFWorkbook(input);
          30             else
          31                 wb = new HSSFWorkbook(input);
          32             Sheet sheet = wb.getSheetAt(0);        //獲得第一個(gè)表單
          33             Iterator<Row> rows = sheet.rowIterator();    //獲得第一個(gè)表單的迭代器
          34             while (rows.hasNext()) {
          35                 Row row = rows.next();    //獲得行數(shù)據(jù)
          36                 System.out.println("Row #" + row.getRowNum());    //獲得行號(hào)從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()) {    //根據(jù)cell中的類型來輸出數(shù)據(jù)
          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 @ 2015-01-13 09:57 魯勝迪 閱讀(343) | 評(píng)論 (0)編輯 收藏

          <2015年1月>
          28293031123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(4)

          隨筆分類

          隨筆檔案

          文章分類

          新聞分類

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 福海县| 来安县| 宝坻区| 望城县| 海阳市| 大港区| 蒲江县| 洛扎县| 方山县| 奉新县| 丽江市| 修文县| 皮山县| 沁源县| 建宁县| 溧阳市| 西丰县| 多伦县| 铁力市| 洛浦县| 宝兴县| 红安县| 巴彦县| 禄丰县| 长丰县| 曲松县| 古蔺县| 洮南市| 洛南县| 阳谷县| 汉沽区| 平舆县| 安西县| 长汀县| 沙坪坝区| 英吉沙县| 阳曲县| 泰兴市| 邵武市| 肇源县| 永康市|