java读取word-excel-ppt文件代码

所属分类: 软件编程 / java 阅读数: 85
收藏 0 赞 0 分享
WORD:
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.poi.hwpf.extractor.WordExtractor;
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import com.search.code.Index;
public Document getDocument(Index index, String url, String title, InputStream is) throws DocCenterException {
String bodyText = null;
try {
WordExtractor ex = new WordExtractor(is);//is是WORD文件的InputStream
bodyText = ex.getText();
if(!bodyText.equals("")){
index.AddIndex(url, title, bodyText);
}
}catch (DocCenterException e) {
throw new DocCenterException("无法从该Mocriosoft Word文档中提取内容", e);
}catch(Exception e){
e.printStackTrace();
}
}
return null;
}
Excel:
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import com.search.code.Index;
public Document getDocument(Index index, String url, String title, InputStream is) throws DocCenterException {
StringBuffer content = new StringBuffer();
try{
HSSFWorkbook workbook = new HSSFWorkbook(is);//创建对Excel工作簿文件的引用
for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) {
if (null != workbook.getSheetAt(numSheets)) {
HSSFSheet aSheet = workbook.getSheetAt(numSheets);//获得一个sheet
for (int rowNumOfSheet = 0; rowNumOfSheet <= aSheet.getLastRowNum(); rowNumOfSheet++) {
if (null != aSheet.getRow(rowNumOfSheet)) {
HSSFRow aRow = aSheet.getRow(rowNumOfSheet); //获得一个行
for (short cellNumOfRow = 0; cellNumOfRow <= aRow.getLastCellNum(); cellNumOfRow++) {
if (null != aRow.getCell(cellNumOfRow)) {
HSSFCell aCell = aRow.getCell(cellNumOfRow);//获得列值
content.append(aCell.getStringCellValue());
}
}
}
}
}
}
if(!content.equals("")){
index.AddIndex(url, title, content.toString());
}
}catch (DocCenterException e) {
throw new DocCenterException("无法从该Mocriosoft Word文档中提取内容", e);
}catch(Exception e) {
System.out.println("已运行xlRead() : " + e );
}
return null;
}
PowerPoint:
import java.io.InputStream;
import org.apache.lucene.document.Document;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;
public Document getDocument(Index index, String url, String title, InputStream is)
throws DocCenterException {
StringBuffer content = new StringBuffer("");
try{
SlideShow ss = new SlideShow(new HSLFSlideShow(is));//is 为文件的InputStream,建立SlideShow
Slide[] slides = ss.getSlides();//获得每一张幻灯片
for(int i=0;i<slides.length;i++){
TextRun[] t = slides[i].getTextRuns();//为了取得幻灯片的文字内容,建立TextRun
for(int j=0;j<t.length;j++){
content.append(t[j].getText());//这里会将文字内容加到content中去
}
content.append(slides[i].getTitle());
}
index.AddIndex(url, title, content.toString());
}catch(Exception ex){
System.out.println(ex.toString());
}
return null;
}
PDF:
import java.io.InputStream;
import java.io.IOException;
import org.apache.lucene.document.Document;
import org.pdfbox.cos.COSDocument;
import org.pdfbox.pdfparser.PDFParser;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.pdmodel.PDDocumentInformation;
import org.pdfbox.util.PDFTextStripper;
import com.search.code.Index;
public Document getDocument(Index index, String url, String title, InputStream is)throws DocCenterException {
COSDocument cosDoc = null;
try {
cosDoc = parseDocument(is);
} catch (IOException e) {
closeCOSDocument(cosDoc);
throw new DocCenterException("无法处理该PDF文档", e);
}
if (cosDoc.isEncrypted()) {
if (cosDoc != null)
closeCOSDocument(cosDoc);
throw new DocCenterException("该PDF文档是加密文档,无法处理");
}
String docText = null;
try {
PDFTextStripper stripper = new PDFTextStripper();
docText = stripper.getText(new PDDocument(cosDoc));
} catch (IOException e) {
closeCOSDocument(cosDoc);
throw new DocCenterException("无法处理该PDF文档", e);
}
PDDocument pdDoc = null;
try {
pdDoc = new PDDocument(cosDoc);
PDDocumentInformation docInfo = pdDoc.getDocumentInformation();
if(docInfo.getTitle()!=null && !docInfo.getTitle().equals("")){
title = docInfo.getTitle();
}
} catch (Exception e) {
closeCOSDocument(cosDoc);
closePDDocument(pdDoc);
System.err.println("无法取得该PDF文档的元数据" + e.getMessage());
} finally {
closeCOSDocument(cosDoc);
closePDDocument(pdDoc);
}
return null;
}
private static COSDocument parseDocument(InputStream is) throws IOException {
PDFParser parser = new PDFParser(is);
parser.parse();
return parser.getDocument();
}
private void closeCOSDocument(COSDocument cosDoc) {
if (cosDoc != null) {
try {
cosDoc.close();
} catch (IOException e) {
}
}
}
private void closePDDocument(PDDocument pdDoc) {
if (pdDoc != null) {
try {
pdDoc.close();
} catch (IOException e) {
}
}
}
代码复制可能出错,不过代码经过测试,绝对能用,POI为3.0-rc4,PDFBOX为0.7.3

POI: http://jakarta.apache.org/poi/index.html
PDFBOX: http://www.pdfbox.org/

更多精彩内容其他人还在看

利用MultipartFile实现文件上传功能

这篇文章主要为大家详细介绍了利用MultipartFile实现文件上传功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java编程实现NBA赛事接口调用实例代码

这篇文章主要介绍了Java编程实现NBA赛事接口调用实例代码,具有一定参考价值,需要的朋友可以了解下。
收藏 0 赞 0 分享

Java编程之双重循环打印图形

这篇文章主要介绍了Java编程之双重循环打印图形,属于Java编程基础练习部分,具有一定参考价值,需要的朋友可以了解下。
收藏 0 赞 0 分享

java基础学习JVM中GC的算法

这篇文章主要介绍了java基础学习JVM中GC的算法,通过图文加深对GC算法思路的理解。
收藏 0 赞 0 分享

Java编程Post数据请求和接收代码详解

这篇文章主要介绍了Java编程Post数据请求和接收代码详解,涉及enctype的三种编码,post与get等相关内容,具有一定参考价值,需要的朋友可以了解下。
收藏 0 赞 0 分享

Retrofit+Rxjava实现文件上传和下载功能

这篇文章主要介绍了Retrofit+Rxjava实现文件上传和下载功能,文中提到了单文件上传和多文件上传及相关参数的请求,需要的朋友参考下吧
收藏 0 赞 0 分享

Retrofit+Rxjava下载文件进度的实现

这篇文章主要介绍了Retrofit+Rxjava下载文件进度的实现,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

java检查服务器的连通两种方法代码分享

这篇文章主要介绍了java检查服务器的连通两种方法代码分享,涉及ping的介绍以及检查服务器连通的两种方法代码示例,具有一定参考价值,需要的朋友可以了解下。
收藏 0 赞 0 分享

Java/Android 获取网络重定向文件的真实URL的示例代码

本篇文章主要介绍了Java/Android 获取网络重定向文件的真实URL的示例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java并发编程之同步器代码示例

这篇文章主要介绍了java并发编程之同步器代码示例,分享了相关代码,具有一定参考价值,需要的朋友可以了解下。
收藏 0 赞 0 分享
查看更多