Java 操作 Excel (读取Excel2003 2007,Poi实现)

来源:互联网 发布:淘宝买家订单数据诈骗 编辑:程序博客网 时间:2024/06/07 04:05
一. Apache POI 简介( http://poi.apache.org/) 
    使用Java程序读写Microsoft Office,提供了下面这几种类型: 
    HSSF-提供读写Microsoft Excel XLS格式档案的功能。 
    XSSF-提供读写Microsoft Excel OOXML XLSX格式档案的功能。 
    HWPF-提供读写Microsoft Word DOC格式档案的功能。 
    HSLF- 供读写Microsoft PowerPoint格式档案的功能。 
    HDGF-提供读Microsoft Visio格式档案的功能。 
    HPBF-提供读Microsoft Publisher格式档案的功能。 

二、POI操作Excel 
    1. 官方快速帮助:http://poi.apache.org/spreadsheet/quick-guide.html 
    2. 导入包:
               dom4j-1.6.1.jar
               junit-3.8.1.jar
               poi-3.8-20120326.jar
               poi-ooxml-3.8-20120326.jar
               poi-ooxml-schemas-3.8-20120326.jar
               xmlbeans-2.3.0.jar

参考: 
    1. http://www.blogjava.net/vwpolo/archive/2009/09/16/295243.html 
    2. http://hacker-zxf.javaeye.com/blog/746546 
    3. http://zmx.javaeye.com/blog/622536 
    4. http://canfly2010.javaeye.com/blog/701726
Java代码 收藏代码
  1. package excel.poi.input;  
  2. import java.io.File;  
  3. import java.io.FileInputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.util.Iterator;  
  7. import org.apache.poi.POITextExtractor;  
  8. import org.apache.poi.extractor.ExtractorFactory;  
  9. import org.apache.poi.hssf.usermodel.HSSFCell;  
  10. import org.apache.poi.hssf.usermodel.HSSFRow;  
  11. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  12. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  13. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;  
  14. import org.apache.poi.openxml4j.exceptions.OpenXML4JException;  
  15. import org.apache.poi.poifs.filesystem.POIFSFileSystem;  
  16. import org.apache.poi.xssf.usermodel.XSSFRow;  
  17. import org.apache.poi.xssf.usermodel.XSSFSheet;  
  18. import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
  19. import org.apache.xmlbeans.XmlException;  
  20. publicclass ReadExcel {  
  21. /**
  22.      * 读取office 2003 xls
  23.      * @param filePath
  24.      */
  25. @SuppressWarnings({ "unchecked""deprecation" })  
  26. publicvoid loadXls(String filePath){  
  27. try {  
  28.            InputStream input = new FileInputStream("D://test.xls");  
  29.            POIFSFileSystem fs = new POIFSFileSystem(input);  
  30.            HSSFWorkbook wb = new HSSFWorkbook(fs);  
  31.            HSSFSheet sheet = wb.getSheetAt(0);  
  32. // Iterate over each row in the sheet
  33.            Iterator rows = sheet.rowIterator();  
  34. while (rows.hasNext()) {  
  35.             HSSFRow row = (HSSFRow) rows.next();  
  36.             System.out.println("Row #" + row.getRowNum());  
  37. // Iterate over each cell in the row and print out the cell"s
  38. // content
  39.             Iterator cells = row.cellIterator();  
  40. while (cells.hasNext()) {  
  41.              HSSFCell cell = (HSSFCell) cells.next();  
  42.              System.out.println("Cell #" + cell.getCellNum());  
  43. switch (cell.getCellType()) {  
  44. case HSSFCell.CELL_TYPE_NUMERIC:  
  45.               System.out.println(cell.getNumericCellValue());  
  46. break;  
  47. case HSSFCell.CELL_TYPE_STRING:  
  48.               System.out.println(cell.getStringCellValue());  
  49. break;  
  50. case HSSFCell.CELL_TYPE_BOOLEAN:  
  51.               System.out.println(cell.getBooleanCellValue());  
  52. break;  
  53. case HSSFCell.CELL_TYPE_FORMULA:  
  54.               System.out.println(cell.getCellFormula());  
  55. break;  
  56. default:  
  57.               System.out.println("unsuported sell type");  
  58. break;  
  59.              }  
  60.             }  
  61.            }  
  62.           } catch (IOException ex) {  
  63.            ex.printStackTrace();  
  64.           }  
  65.  }  
  66. /**
  67.   * 读取xlsx文本
  68.   * @param filePath
  69.   */
  70. publicvoid loadXlsxText(String filePath){  
  71.      File inputFile = new File("D://test.xlsx");     
  72. try {  
  73.         POITextExtractor extractor = ExtractorFactory.createExtractor(inputFile);  
  74.         System.out.println(extractor.getText());  
  75.     } catch (InvalidFormatException e) {  
  76.         e.printStackTrace();  
  77.     } catch (IOException e) {  
  78.         e.printStackTrace();  
  79.     } catch (OpenXML4JException e) {  
  80.         e.printStackTrace();  
  81.     } catch (XmlException e) {  
  82.         e.printStackTrace();  
  83.     }     
  84.  }  
  85. /**
  86.   * 读取office 2007 xlsx
  87.   * @param filePath
  88.   */
  89. publicvoid loadXlsx(String filePath){  
  90. // 构造 XSSFWorkbook 对象,strPath 传入文件路径   
  91.     XSSFWorkbook xwb = null;  
  92. try {  
  93.         xwb = new XSSFWorkbook("D://test.xlsx");  
  94.     } catch (IOException e) {  
  95.         System.out.println("读取文件出错");  
  96.         e.printStackTrace();  
  97.     }     
  98. // 读取第一章表格内容   
  99.      XSSFSheet sheet = xwb.getSheetAt(0);     
  100.      xwb.getSheetAt(1);  
  101. // 定义 row、cell   
  102.      XSSFRow row;     
  103.      String cell;     
  104. // 循环输出表格中的内容   
  105. for (int i = sheet.getFirstRowNum(); i < sheetgetPhysicalNumberOfRowsispan>
  106.          row = sheet.getRow(i);     
  107. for (int j = row.getFirstCellNum(); j < rowgetPhysicalNumberOfCellsjspan>
  108. // 通过 row.getCell(j).toString() 获取单元格内容,   
  109.             cell = row.getCell(j).toString();    
  110.             System.out.print(cell + "/t");     
  111.         }     
  112.          System.out.println("");     
  113.      }     
  114.  }  
  115. publicstaticvoid main(String[] args) {  
  116.      ReadExcel readExcel =new ReadExcel();  
  117.      readExcel.loadXlsx("");  
  118.  }  
  119. }  


Workbook wb = WorkbookFactory.create(new FileInputStream(FILE_URL));
可以自动判断2003还是2007版的文档格式。

转自:http://blog.csdn.net/yuanfen860913/article/details/5940387
0 0
原创粉丝点击