lucene讀取word,excel,pdf

前面在寫lucene入門的時候,例子只能對txt文檔創建索引,不能對word,excel,pdf創建索引,要讀取這些文檔的內容,須要額外的jar包,好在apache這個開源組織好,提供了對這些文檔解析的開源jar包 html

 

索引和查詢,我就再也不寫出來了,前面文章有,下面只將這三種文檔的讀取方法貼在下面 java

 

1.首先來看WORD文檔: apache

這裏用的是poi,相關jar包(http://poi.apache.org/)能夠到apache官網上去下載,而後加到工程中(如下所要用的jar包也是,再也不重複說)。一個poi.jar還不行,還須要將poi-scratchpad.jar包導入才行 api

[java]  view plain copy
  1. public static String readWord(String path) {  
  2.     StringBuffer content = new StringBuffer("");// 文檔內容  
  3.     try {  
  4.   
  5.         HWPFDocument doc = new HWPFDocument(new FileInputStream(path));  
  6.         Range range = doc.getRange();  
  7.         int paragraphCount = range.numParagraphs();// 段落  
  8.         for (int i = 0; i < paragraphCount; i++) {// 遍歷段落讀取數據  
  9.             Paragraph pp = range.getParagraph(i);  
  10.             content.append(pp.text());  
  11.         }  
  12.   
  13.     } catch (Exception e) {  
  14.         e.printStackTrace();  
  15.     }  
  16.     return content.toString().trim();  
  17. }  


 

2.再來看EXCEL文檔: app

這裏用的是jxl包,但jxl包(http://www.andykhan.com/jexcelapi/ )目前還尚不支持2007或更高的版本,但poi能夠,如今相信開源的強大了,solr在今年3月份出的3.1版,5月份就出了3.2版,能夠看出更新的速度 ui

下面的例子,是用jxl包讀取excel2003的,有興趣的能夠去查一下,用poi去讀07版的excel,好像要加入不少關聯jar包才行 spa

 

[java]  view plain copy
  1. public static String readExcel(String path) throws Exception {  
  2.     FileInputStream fis = new FileInputStream(path);  
  3.     StringBuilder sb = new StringBuilder();  
  4.     jxl.Workbook rwb = Workbook.getWorkbook(fis);  
  5.     Sheet[] sheet = rwb.getSheets();  
  6.     for (int i = 0; i < sheet.length; i++) {  
  7.         Sheet rs = rwb.getSheet(i);  
  8.         for (int j = 0; j < rs.getRows(); j++) {  
  9.             Cell[] cells = rs.getRow(j);  
  10.             for (int k = 0; k < cells.length; k++)  
  11.                 sb.append(cells[k].getContents());  
  12.         }  
  13.     }  
  14.     fis.close();  
  15.     return sb.toString();  
  16. }  


3.最後來看PDF文檔: .net

這裏用的是PDFBox,相關jar包能夠到apache官網上去下載:http://pdfbox.apache.org/download.html excel

這裏要注意,若是隻單單導入pdfbox.jar包,還會報錯,還須要導入commons-logging.jar和fontbox.jar包才行 htm

[java]  view plain copy
  1.  public static String readPdf(String path) throws Exception {   
  2.            StringBuffer content = new StringBuffer("");// 文檔內容 FileInputStream fis = new  
  3.       FileInputStream(path); PDFParser p = new PDFParser(fis); p.parse();  
  4.       PDFTextStripper ts = new PDFTextStripper();  
  5.       content.append(ts.getText(p.getPDDocument())); fis.close(); return  
  6.       content.toString().trim();   
  7. }  


若是提取pdf文檔的時候都會拋出異常:java.lang.Throwable: Warning: You did not close the PDF Document,請參考下面資料:

1.http://lqw.iteye.com/blog/721568

2.http://blog.csdn.net/rxr1st/article/details/2204460

 

在solr官網上看到:

Rich Document Parsing and Indexing (PDF, Word, HTML, etc) using Apache Tika

Tika好像是把poi,pdfbox等一些解析jar包容到一塊兒了,下面看看如何在solr中實現對pdf的解析,估計要看配置文件才行

參考資料:

1.http://blog.163.com/lewutian@126/blog/static/163824796201041131910140/

2.http://blog.csdn.net/iamwangbao/archive/2009/11/04/4767387.aspx

相關文章
相關標籤/搜索