---
title: "JAVA读取WORD,EXCEL,POWERPOINT,PDF文件的方法"
author: "fuyangchang"
date: "2007-06-11T14:12:00Z"
tags: []
slug: "java-word-excel-powerpoint-pdf"
draft: false
lastmod: "2007-06-11T14:12:00Z"
id: 104082
url: "https://csdn.iteye.com/blog/104082"
---

JAVA读取WORD,EXCEL,POWERPOINT,PDF文件的方法  
http://blog.sina.com.cn/u/54c1567b010008vh

OFFICE文档使用POI控件，PDF可以使用PDFBOX0.7.3控件，完全支持中文，用XPDF也行，不过感觉PDFBOX比较好，而且作者也在更新。水平有限，万望各位指正

WORD:

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.apache.lucene.document.Document;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.apache.lucene.document.Field;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.apache.poi.hwpf.extractor.WordExtractor;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.File;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.InputStream;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.FileInputStream;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import com.search.code.Index;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public Document getDocument(Index index, String url, String title, InputStream is) throws DocCenterException ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  String bodyText = null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)  try ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)   WordExtractor ex = new WordExtractor(is);//is是WORD文件的InputStream  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)   bodyText = ex.getText();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)   if(!bodyText.equals(""))...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    index.AddIndex(url, title, bodyText);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)   }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)  }catch (DocCenterException e) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)   throw new DocCenterException("无法从该Mocriosoft Word文档中提取内容", e);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)  }catch(Exception e)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)   e.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)  }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  return null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)}

Excel:

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.apache.lucene.document.Document;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.apache.lucene.document.Field;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.apache.poi.hwpf.extractor.WordExtractor;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import  org.apache.poi.hssf.usermodel.HSSFWorkbook;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import  org.apache.poi.hssf.usermodel.HSSFSheet;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import  org.apache.poi.hssf.usermodel.HSSFRow;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import  org.apache.poi.hssf.usermodel.HSSFCell;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.File;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.InputStream;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.FileInputStream;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import com.search.code.Index;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public Document getDocument(Index index, String url, String title, InputStream is) throws DocCenterException ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  StringBuffer content = new StringBuffer();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)  try...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)   HSSFWorkbook  workbook  =  new  HSSFWorkbook(is);//创建对Excel工作簿文件的引用  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)   for (int numSheets = 0; numSheets &lt; workbook.getNumberOfSheets(); numSheets++) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    if (null != workbook.getSheetAt(numSheets)) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     HSSFSheet aSheet = workbook.getSheetAt(numSheets);//获得一个sheet  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        for (int rowNumOfSheet = 0; rowNumOfSheet &lt;= aSheet.getLastRowNum(); rowNumOfSheet++) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)         if (null != aSheet.getRow(rowNumOfSheet)) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          HSSFRow aRow = aSheet.getRow(rowNumOfSheet); //获得一个行  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)          for (short cellNumOfRow = 0; cellNumOfRow &lt;= aRow.getLastCellNum(); cellNumOfRow++) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)           if (null != aRow.getCell(cellNumOfRow)) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            HSSFCell aCell = aRow.getCell(cellNumOfRow);//获得列值  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            content.append(aCell.getStringCellValue());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)           }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)          }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)         }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)   }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)   if(!content.equals(""))...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    index.AddIndex(url, title, content.toString());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)   }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)  }catch (DocCenterException e) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)   throw new DocCenterException("无法从该Mocriosoft Word文档中提取内容", e);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)  }catch(Exception  e)  ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)   System.out.println("已运行xlRead()  :  "  +  e  );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)  }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  return null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}

PowerPoint:

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.InputStream;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.apache.lucene.document.Document;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.apache.poi.hslf.HSLFSlideShow;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.apache.poi.hslf.model.TextRun;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.apache.poi.hslf.model.Slide;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.apache.poi.hslf.usermodel.SlideShow;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)public Document getDocument(Index index, String url, String title, InputStream is)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)throws DocCenterException ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  StringBuffer content = new StringBuffer("");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)  try...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)   SlideShow ss = new SlideShow(new HSLFSlideShow(is));//is 为文件的InputStream，建立SlideShow  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)   Slide\[] slides = ss.getSlides();//获得每一张幻灯片  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)   for(int i=0;i&lt;slides.length;i++)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    TextRun\[] t = slides\[i].getTextRuns();//为了取得幻灯片的文字内容，建立TextRun  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    for(int j=0;j&lt;t.length;j++)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     content.append(t\[j].getText());//这里会将文字内容加到content中去  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    content.append(slides\[i].getTitle());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)   }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)   index.AddIndex(url, title, content.toString());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)  }catch(Exception ex)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)   System.out.println(ex.toString());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)  }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  return null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

PDF:

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.InputStream;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.IOException;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.apache.lucene.document.Document;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.pdfbox.cos.COSDocument;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.pdfbox.pdfparser.PDFParser;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.pdfbox.pdmodel.PDDocument;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.pdfbox.pdmodel.PDDocumentInformation;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.pdfbox.util.PDFTextStripper;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import com.search.code.Index;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public Document getDocument(Index index, String url, String title, InputStream is)throws DocCenterException ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  COSDocument cosDoc = null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)  try ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)   cosDoc = parseDocument(is);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)  } catch (IOException e) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)   closeCOSDocument(cosDoc);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)   throw new DocCenterException("无法处理该PDF文档", e);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)  }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)  if (cosDoc.isEncrypted()) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)   if (cosDoc != null)