使用POI讀取Word207和Excel2007實例

  1. /**
  2. * 需要的jar包:
  3. * poi-3.0.2-FINAL-20080204.jar
  4. * poi-contrib-3.0.2-FINAL-20080204.jar
  5. * poi-scratchpad-3.0.2-FINAL-20080204.jar
  6. * poi-3.5-beta6-20090622.jar
  7. * geronimo-stax-api_1.0_spec-1.0.jar
  8. * ooxml-schemas-1.0.jar
  9. * openxml4j-bin-beta.jar
  10. * poi-ooxml-3.5-beta6-20090622.jar
  11. * xmlbeans-2.3.0.jar
  12. * dom4j-1.6.1.jar
  13. */  
  14.   
  15. import java.io.FileInputStream;   
  16. import java.io.IOException;   
  17. import java.io.InputStream;   
  18.   
  19. import org.apache.poi.POIXMLDocument;   
  20. import org.apache.poi.POIXMLTextExtractor;   
  21. import org.apache.poi.hssf.usermodel.HSSFCell;   
  22. import org.apache.poi.hssf.usermodel.HSSFRow;   
  23. import org.apache.poi.hssf.usermodel.HSSFSheet;   
  24. import org.apache.poi.hssf.usermodel.HSSFWorkbook;   
  25. import org.apache.poi.hwpf.extractor.WordExtractor;   
  26. import org.apache.poi.openxml4j.exceptions.OpenXML4JException;   
  27. import org.apache.poi.openxml4j.opc.OPCPackage;   
  28. import org.apache.poi.xssf.usermodel.XSSFCell;   
  29. import org.apache.poi.xssf.usermodel.XSSFRow;   
  30. import org.apache.poi.xssf.usermodel.XSSFSheet;   
  31. import org.apache.poi.xssf.usermodel.XSSFWorkbook;   
  32. import org.apache.poi.xwpf.extractor.XWPFWordExtractor;   
  33. import org.apache.xmlbeans.XmlException;   
  34.   
  35. public class WordAndExcelExtractor {   
  36. public static void main(String[] args){   
  37.   try{   
  38.     String wordFile = "D:/松山血戰.docx";   
  39.     String wordText2007 = WordAndExcelExtractor.extractTextFromDOC2007(wordFile);   
  40.     System.out.println("wordText2007======="+wordText2007);   
  41.       
  42.     InputStream is = new FileInputStream("D:/XXX研發中心技術崗位職位需求.xls");      
  43.     String excelText = WordAndExcelExtractor.extractTextFromXLS(is);      
  44.     System.out.println("text2003==========" + excelText);   
  45.       
  46.     String excelFile = "D:/Hello2007.xlsx";      
  47.     String excelText2007 = WordAndExcelExtractor.extractTextFromXLS2007(excelFile);   
  48.     System.out.println("excelText2007==========" + excelText2007);   
  49.   
  50.       
  51.    }catch(Exception e ){   
  52.     e.printStackTrace();   
  53.    }   
  54. }   
  55.   
  56. /**
  57.    * @Method: extractTextFromDOCX
  58.    * @Description: 從word 2003文檔中提取純文本
  59.    *
  60.    * @param
  61.    * @return String
  62.    * @throws
  63.    */  
  64.     public static String extractTextFromDOC(InputStream is) throws IOException {   
  65.          WordExtractor ex = new WordExtractor(is); //is是WORD文件的InputStream   
  66.   
  67.         return ex.getText();   
  68.      }   
  69.   
  70. /**
  71.    * @Method: extractTextFromDOCX
  72.    * @Description: 從word 2007文檔中提取純文本
  73.    *
  74.    * @param
  75.    * @return String
  76.    * @throws
  77.    */  
  78.     public static String extractTextFromDOC2007(String fileName) throws IOException, OpenXML4JException, XmlException {   
  79.       OPCPackage opcPackage = POIXMLDocument.openPackage(fileName);   
  80.       POIXMLTextExtractor ex = new XWPFWordExtractor(opcPackage);      
  81.   
  82.         return ex.getText();   
  83.      }   
  84.   
  85. /**
  86.    * @Method: extractTextFromXLS
  87.    * @Description: 從excel 2003文檔中提取純文本
  88.    *
  89.    * @param
  90.    * @return String
  91.    * @throws
  92.    */  
  93.     @SuppressWarnings("deprecation")   
  94. private static String extractTextFromXLS(InputStream is)   
  95.         throws IOException {   
  96.          StringBuffer content   = new StringBuffer();   
  97.          HSSFWorkbook workbook = new HSSFWorkbook(is); //創建對Excel工作簿文件的引用   
  98.   
  99.         for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) {   
  100.             if (null != workbook.getSheetAt(numSheets)) {   
  101.                  HSSFSheet aSheet = workbook.getSheetAt(numSheets); //獲得一個sheet   
  102.   
  103.                 for (int rowNumOfSheet = 0; rowNumOfSheet <= aSheet.getLastRowNum(); rowNumOfSheet++) {   
  104.                     if (null != aSheet.getRow(rowNumOfSheet)) {   
  105.                          HSSFRow aRow = aSheet.getRow(rowNumOfSheet); //獲得一行   
  106.   
  107.                         for (short cellNumOfRow = 0; cellNumOfRow <= aRow.getLastCellNum(); cellNumOfRow++) {   
  108.                             if (null != aRow.getCell(cellNumOfRow)) {   
  109.                                  HSSFCell aCell = aRow.getCell(cellNumOfRow); //獲得列值   
  110.                                                                    
  111.                                 if(aCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){   
  112.                                   content.append(aCell.getNumericCellValue());   
  113.                                  }else if(aCell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN){   
  114.                                   content.append(aCell.getBooleanCellValue());   
  115.                                  }else {   
  116.                                   content.append(aCell.getStringCellValue());   
  117.                                  }   
  118.                              }   
  119.                          }   
  120.                      }   
  121.                  }   
  122.              }   
  123.          }   
  124.   
  125.         return content.toString();   
  126.      }   
  127.        
  128.     /**
  129.       * @Method: extractTextFromXLS2007
  130.       * @Description: 從excel 2007文檔中提取純文本
  131.       *
  132.       * @param
  133.       * @return String
  134.       * @throws
  135.       */  
  136.     private static String extractTextFromXLS2007(String fileName) throws Exception{   
  137.       StringBuffer content = new StringBuffer();   
  138.         
  139.      //構造 XSSFWorkbook 對象,strPath 傳入文件路徑       
  140.    XSSFWorkbook xwb = new XSSFWorkbook(fileName);   
  141.      
  142.   //循環工作表Sheet   
  143.   for(int numSheet = 0; numSheet < xwb.getNumberOfSheets(); numSheet++){   
  144.     XSSFSheet xSheet = xwb.getSheetAt(numSheet);   
  145.    if(xSheet == null){   
  146.     continue;   
  147.     }   
  148.       
  149.    //循環行Row   
  150.    for(int rowNum = 0; rowNum <= xSheet.getLastRowNum(); rowNum++){   
  151.      XSSFRow xRow = xSheet.getRow(rowNum);   
  152.     if(xRow == null){   
  153.      continue;   
  154.      }   
  155.        
  156.     //循環列Cell   
  157.     for(int cellNum = 0; cellNum <= xRow.getLastCellNum(); cellNum++){   
  158.       XSSFCell xCell = xRow.getCell(cellNum);   
  159.      if(xCell == null){   
  160.       continue;   
  161.       }   
  162.         
  163.      if(xCell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN){   
  164.        content.append(xCell.getBooleanCellValue());   
  165.       }else if(xCell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC){   
  166.        content.append(xCell.getNumericCellValue());   
  167.       }else{   
  168.        content.append(xCell.getStringCellValue());   
  169.       }   
  170.      }   
  171.     }   
  172.    }   
  173.      
  174.   return content.toString();   
  175.      }   
  176.        

  1. }   

轉載自  http://blog.csdn.net/soundfly/article/details/5969567

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章