JAVA读取WORD,EXCEL,POWERPOINT,PDF文件的方法

来源:互联网 发布:网络数据 编辑:程序博客网 时间:2024/04/30 12:42

OFFICE文档使用POI控件,PDF可以使用PDFBOX0.7.3控件,完全支持中文,用XPDF也行,不过感觉PDFBOX比较好,而且作者也在更新。

 

  1. WORD:
  2. import org.apache.lucene.document.Document;
  3. import org.apache.lucene.document.Field;
  4. import org.apache.poi.hwpf.extractor.WordExtractor;
  5. import java.io.File;
  6. import java.io.InputStream;
  7. import java.io.FileInputStream;
  8. import com.search.code.Index;
  9. public Document getDocument(Index index, String url, String title, InputStream is) throws DocCenterException {
  10.   String bodyText = null;
  11.   try {
  12.    WordExtractor ex = new WordExtractor(is);//is是WORD文件的InputStream 
  13.    bodyText = ex.getText();
  14.    if(!bodyText.equals("")){
  15.     index.AddIndex(url, title, bodyText);
  16.    }
  17.   }catch (DocCenterException e) {
  18.    throw new DocCenterException("无法从该Mocriosoft Word文档中提取内容", e);
  19.   }catch(Exception e){
  20.    e.printStackTrace();
  21.   }
  22. }
  23.   return null;
  24. }
  25. Excel:
  26. import org.apache.lucene.document.Document;
  27. import org.apache.lucene.document.Field;
  28. import org.apache.poi.hwpf.extractor.WordExtractor;
  29. import  org.apache.poi.hssf.usermodel.HSSFWorkbook; 
  30. import  org.apache.poi.hssf.usermodel.HSSFSheet; 
  31. import  org.apache.poi.hssf.usermodel.HSSFRow; 
  32. import  org.apache.poi.hssf.usermodel.HSSFCell; 
  33. import java.io.File;
  34. import java.io.InputStream;
  35. import java.io.FileInputStream;
  36. import com.search.code.Index;
  37. public Document getDocument(Index index, String url, String title, InputStream is) throws DocCenterException {
  38.   StringBuffer content = new StringBuffer();
  39.   try{
  40.    HSSFWorkbook  workbook  =  new  HSSFWorkbook(is);//创建对Excel工作簿文件的引用 
  41.    for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) {
  42.     if (null != workbook.getSheetAt(numSheets)) {
  43.      HSSFSheet aSheet = workbook.getSheetAt(numSheets);//获得一个sheet
  44.         for (int rowNumOfSheet = 0; rowNumOfSheet <= aSheet.getLastRowNum(); rowNumOfSheet++) {
  45.          if (null != aSheet.getRow(rowNumOfSheet)) {
  46.           HSSFRow aRow = aSheet.getRow(rowNumOfSheet); //获得一个行
  47.           for (short cellNumOfRow = 0; cellNumOfRow <= aRow.getLastCellNum(); cellNumOfRow++) {
  48.            if (null != aRow.getCell(cellNumOfRow)) {
  49.             HSSFCell aCell = aRow.getCell(cellNumOfRow);//获得列值
  50.             content.append(aCell.getStringCellValue());
  51.            }
  52.           }
  53.          }
  54.         }
  55.     }
  56.    }
  57.    if(!content.equals("")){
  58.     index.AddIndex(url, title, content.toString());
  59.    }
  60.   }catch (DocCenterException e) {
  61.    throw new DocCenterException("无法从该Mocriosoft Word文档中提取内容", e);
  62.   }catch(Exception  e)  { 
  63.    System.out.println("已运行xlRead()  :  "  +  e  ); 
  64.   }
  65.   return null;
  66. }
  67. PowerPoint:
  68. import java.io.InputStream;
  69. import org.apache.lucene.document.Document;
  70. import org.apache.poi.hslf.HSLFSlideShow;
  71. import org.apache.poi.hslf.model.TextRun;
  72. import org.apache.poi.hslf.model.Slide;
  73. import org.apache.poi.hslf.usermodel.SlideShow;
  74. public Document getDocument(Index index, String url, String title, InputStream is)
  75. throws DocCenterException {
  76.   StringBuffer content = new StringBuffer("");
  77.   try{
  78.    SlideShow ss = new SlideShow(new HSLFSlideShow(is));//is 为文件的InputStream,建立SlideShow
  79.    Slide[] slides = ss.getSlides();//获得每一张幻灯片
  80.    for(int i=0;i<slides.length;i++){
  81.     TextRun[] t = slides[i].getTextRuns();//为了取得幻灯片的文字内容,建立TextRun
  82.     for(int j=0;j<t.length;j++){
  83.      content.append(t[j].getText());//这里会将文字内容加到content中去
  84.     }
  85.     content.append(slides[i].getTitle());
  86.    }
  87.    index.AddIndex(url, title, content.toString());
  88.   }catch(Exception ex){
  89.    System.out.println(ex.toString());
  90.   }
  91.   return null;
  92. }
  93. PDF:
  94. import java.io.InputStream;
  95. import java.io.IOException;
  96. import org.apache.lucene.document.Document;
  97. import org.pdfbox.cos.COSDocument;
  98. import org.pdfbox.pdfparser.PDFParser;
  99. import org.pdfbox.pdmodel.PDDocument;
  100. import org.pdfbox.pdmodel.PDDocumentInformation;
  101. import org.pdfbox.util.PDFTextStripper;
  102. import com.search.code.Index;
  103. public Document getDocument(Index index, String url, String title, InputStream is)throws DocCenterException {
  104.   
  105.   COSDocument cosDoc = null;
  106.   try {
  107.    cosDoc = parseDocument(is);
  108.   } catch (IOException e) {
  109.    closeCOSDocument(cosDoc);
  110.    throw new DocCenterException("无法处理该PDF文档", e);
  111.   }
  112.   if (cosDoc.isEncrypted()) {
  113.    if (cosDoc != null)
  114.     closeCOSDocument(cosDoc);
  115.    throw new DocCenterException("该PDF文档是加密文档,无法处理");
  116.   }
  117.   String docText = null;
  118.   try {
  119.    PDFTextStripper stripper = new PDFTextStripper();
  120.    docText = stripper.getText(new PDDocument(cosDoc));
  121.   } catch (IOException e) {
  122.    closeCOSDocument(cosDoc);
  123.    throw new DocCenterException("无法处理该PDF文档", e);
  124.   }
  125.   PDDocument pdDoc = null;
  126.   try {
  127.    pdDoc = new PDDocument(cosDoc);
  128.    PDDocumentInformation docInfo = pdDoc.getDocumentInformation();
  129.    if(docInfo.getTitle()!=null && !docInfo.getTitle().equals("")){
  130.     title = docInfo.getTitle();
  131.    }
  132.   } catch (Exception e) {
  133.    closeCOSDocument(cosDoc);
  134.    closePDDocument(pdDoc);
  135.    System.err.println("无法取得该PDF文档的元数据" + e.getMessage());
  136.   } finally {
  137.    closeCOSDocument(cosDoc);
  138.    closePDDocument(pdDoc);
  139.   }
  140.   
  141.   return null;
  142. }
  143. private static COSDocument parseDocument(InputStream is) throws IOException {
  144.   PDFParser parser = new PDFParser(is);
  145.   parser.parse();
  146.   return parser.getDocument();
  147. }
  148. private void closeCOSDocument(COSDocument cosDoc) {
  149.   if (cosDoc != null) {
  150.    try {
  151.     cosDoc.close();
  152.    } catch (IOException e) {
  153.    }
  154.   }
  155. }
  156. private void closePDDocument(PDDocument pdDoc) {
  157.   if (pdDoc != null) {
  158.    try {
  159.     pdDoc.close();
  160.    } catch (IOException e) {
  161.    }
  162.   }
  163. }