Java世界

          學習筆記

          常用鏈接

          統計

          積分與排名

          天籟村

          新華網

          雅虎

          最新評論

          Java POI讀取Office excel (2003,2007)及相關jar包

          poi jar包下載 : http://poi.apache.org/ 

          讀取excel 文件的 java 代碼:
          1. import java.io.File;  
          2. import java.io.FileInputStream;  
          3. import java.io.FileNotFoundException;  
          4. import java.io.IOException;  
          5. import java.text.DecimalFormat;  
          6. import java.text.SimpleDateFormat;  
          7. import java.util.LinkedList;  
          8. import java.util.List;  
          9. import org.apache.poi.hssf.usermodel.HSSFCell;  
          10. import org.apache.poi.hssf.usermodel.HSSFDateUtil;  
          11. import org.apache.poi.hssf.usermodel.HSSFRow;  
          12. import org.apache.poi.hssf.usermodel.HSSFSheet;  
          13. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
          14. import org.apache.poi.xssf.usermodel.XSSFCell;  
          15. import org.apache.poi.xssf.usermodel.XSSFRow;  
          16. import org.apache.poi.xssf.usermodel.XSSFSheet;  
          17. import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
          18. public class ReadExcel {  
          19.      /** 
          20.      * 對外提供讀取excel 的方法 
          21.      * */  
          22. public static List<List<Object>> readExcel(File file) throws IOException{  
          23.    String fileName = file.getName();  
          24.    String extension = fileName.lastIndexOf(".")==-1?"":fileName.substring(fileName.lastIndexOf(".")+1);  
          25.    if("xls".equals(extension)){  
          26.     return read2003Excel(file);  
          27.    }else if("xlsx".equals(extension)){  
          28.     return read2007Excel(file);  
          29.    }else{  
          30.     throw new IOException("不支持的文件類型");  
          31.    }  
          32. }  
          33. /** 
          34. * 讀取 office 2003 excel 
          35. * @throws IOException  
          36. * @throws FileNotFoundException */  
          37. private static List<List<Object>> read2003Excel(File file) throws IOException{  
          38.    List<List<Object>> list = new LinkedList<List<Object>>();  
          39.    HSSFWorkbook hwb = new HSSFWorkbook(new FileInputStream(file));  
          40.    HSSFSheet sheet = hwb.getSheetAt(0);  
          41.    Object value = null;  
          42.    HSSFRow row = null;  
          43.    HSSFCell cell = null;   
          44.    for(int i = sheet.getFirstRowNum();i<= sheet.getPhysicalNumberOfRows();i++){  
          45.     row = sheet.getRow(i);  
          46.     if (row == null) {  
          47.      continue;  
          48.     }  
          49.     List<Object> linked = new LinkedList<Object>();  
          50.     for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {  
          51.      cell = row.getCell(j);  
          52.      if (cell == null) {  
          53.       continue;  
          54.      }  
          55.      DecimalFormat df = new DecimalFormat("0");// 格式化 number String 字符  
          56.      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 格式化日期字符串  
          57.      DecimalFormat nf = new DecimalFormat("0.00");// 格式化數字  
          58.      switch (cell.getCellType()) {  
          59.      case XSSFCell.CELL_TYPE_STRING:  
          60.       System.out.println(i+"行"+j+" 列 is String type");  
          61.       value = cell.getStringCellValue();  
          62.       break;  
          63.      case XSSFCell.CELL_TYPE_NUMERIC:  
          64.       System.out.println(i+"行"+j+" 列 is Number type ; DateFormt:"+cell.getCellStyle().getDataFormatString());  
          65.       if("@".equals(cell.getCellStyle().getDataFormatString())){  
          66.          value = df.format(cell.getNumericCellValue());  
          67.       } else if("General".equals(cell.getCellStyle().getDataFormatString())){  
          68.          value = nf.format(cell.getNumericCellValue());  
          69.       }else{  
          70.         value = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));  
          71.       }  
          72.       break;  
          73.      case XSSFCell.CELL_TYPE_BOOLEAN:  
          74.       System.out.println(i+"行"+j+" 列 is Boolean type");  
          75.       value = cell.getBooleanCellValue();  
          76.       break;  
          77.      case XSSFCell.CELL_TYPE_BLANK:  
          78.       System.out.println(i+"行"+j+" 列 is Blank type");  
          79.       value = "";  
          80.       break;  
          81.      default:  
          82.       System.out.println(i+"行"+j+" 列 is default type");  
          83.       value = cell.toString();  
          84.      }  
          85.      if (value == null || "".equals(value)) {  
          86.       continue;  
          87.      }  
          88.      linked.add(value);    
          89.    }  
          90.     list.add(linked);  
          91.    }  
          92.    return list;  
          93. }  
          94. /** 
          95. * 讀取Office 2007 excel 
          96. * */  
          97. private static List<List<Object>> read2007Excel(File file) throws IOException {  
          98.    List<List<Object>> list = new LinkedList<List<Object>>();  
          99.    // 構造 XSSFWorkbook 對象,strPath 傳入文件路徑  
          100.    XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(file));  
          101.    // 讀取第一章表格內容  
          102.    XSSFSheet sheet = xwb.getSheetAt(0);  
          103.    Object value = null;  
          104.    XSSFRow row = null;  
          105.    XSSFCell cell = null;  
          106.    for (int i = sheet.getFirstRowNum(); i <= sheet  
          107.      .getPhysicalNumberOfRows(); i++) {  
          108.     row = sheet.getRow(i);  
          109.     if (row == null) {  
          110.      continue;  
          111.     }  
          112.     List<Object> linked = new LinkedList<Object>();  
          113.     for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {  
          114.      cell = row.getCell(j);  
          115.      if (cell == null) {  
          116.       continue;  
          117.      }  
          118.      DecimalFormat df = new DecimalFormat("0");// 格式化 number String 字符  
          119.      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 格式化日期字符串  
          120.      DecimalFormat nf = new DecimalFormat("0.00");// 格式化數字  
          121.      switch (cell.getCellType()) {  
          122.      case XSSFCell.CELL_TYPE_STRING:  
          123.       System.out.println(i+"行"+j+" 列 is String type");  
          124.       value = cell.getStringCellValue();  
          125.       break;  
          126.      case XSSFCell.CELL_TYPE_NUMERIC:  
          127.       System.out.println(i+"行"+j+" 列 is Number type ; DateFormt:"+cell.getCellStyle().getDataFormatString());  
          128.       if("@".equals(cell.getCellStyle().getDataFormatString())){  
          129.         value = df.format(cell.getNumericCellValue());  
          130.       } else if("General".equals(cell.getCellStyle().getDataFormatString())){  
          131.         value = nf.format(cell.getNumericCellValue());  
          132.       }else{  
          133.        value = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));  
          134.       }  
          135.       break;  
          136.      case XSSFCell.CELL_TYPE_BOOLEAN:  
          137.       System.out.println(i+"行"+j+" 列 is Boolean type");  
          138.       value = cell.getBooleanCellValue();  
          139.       break;  
          140.      case XSSFCell.CELL_TYPE_BLANK:  
          141.       System.out.println(i+"行"+j+" 列 is Blank type");  
          142.       value = "";  
          143.       break;  
          144.      default:  
          145.       System.out.println(i+"行"+j+" 列 is default type");  
          146.       value = cell.toString();  
          147.      }  
          148.      if (value == null || "".equals(value)) {  
          149.       continue;  
          150.      }  
          151.      linked.add(value);  
          152.     }  
          153.     list.add(linked);  
          154.    }  
          155.    return list;  
          156. }  
          157. }  
          說明:該類中共封裝了三個方法,對外提供的讀取excel文件的方法,兩個私有的分別讀取excel2003和excel2007的方法。外部使用,只需調用readExcel 方法,傳入一個File 參數,程序根據文件擴展名來判斷選取那個方法來讀取Excel文件。 

          posted on 2012-06-12 11:23 Rabbit 閱讀(5944) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 梁河县| 呼伦贝尔市| 西城区| 河西区| 海安县| 永嘉县| 嘉善县| 绿春县| 寿光市| 彰化市| 平阳县| 益阳市| 宁安市| 淄博市| 花垣县| 汶川县| 天等县| 林周县| 鲁甸县| 崇文区| 裕民县| 汕头市| 滨州市| 郸城县| 沂源县| 隆尧县| 隆昌县| 亳州市| 甘孜| 阿荣旗| 定边县| 咸宁市| 廊坊市| 镇康县| 永吉县| 从江县| 晋城| 清新县| 德惠市| 监利县| 加查县|