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

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

更新时间:2009年04月27日 17:56:54   作者:

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

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

TextRun[] t = slides[i].getTextRuns();//为了取得幻灯片的文字内容,建立TextRun

for(int j=0;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

相关文章

1a1b05c64693fbf380aa1344a7812747.png

这篇文章主要介绍了详解Spring中接口的bean是如何注入的的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Spring具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧2020-06-06

4f55910a645b073bc4fc65dc10dc14bd.png

本文的主要内容就是学习Spring的开端,模拟一下Spring的实现,感兴趣的小伙伴可以参考一下2015-10-10

0ea3c7666119d5615e582f823fb3fad6.png

个人从来没有研究过图像学,也没看过什么论文或者相关文档,写这个完全是靠google和百度,自己写了个实验了下,测试用例也少,估计有大BUG的存在,所以看的人权当学习交流,切勿生产使用。2014-09-09

4f96a78db829b1556ff16de21e013c7a.png

这篇文章主要介绍了浅谈Tomcat三种运行模式,小编冒昧将两篇文章略微整合了一下,有不足的地方,欢迎指出。需要的朋友可以参考。2017-10-10

8cc1031babc6aff2319f1c6af8544aa0.png

GridBagLayout是java里面最重要的布局管理器之一,可以做出很复杂的布局,可以说GridBagLayout是必须要学好的的,需要的朋友可以了解下2012-12-12

0c932a99bb7b6f23c937db507070cc7b.png

这篇文章主要介绍了java基于servlet编写上传下载功能,类似文件服务器,感兴趣的小伙伴们可以参考一下2016-07-07

cca732bf65a93ed2ec0ac80c638460fe.png

LinkedBlockingDeque是双向链表实现的双向并发阻塞队列。该阻塞队列同时支持FIFO和FILO两种操作方式,即可以从队列的头和尾同时操作(插入/删除);并且,该阻塞队列是支持线程安全。2017-06-06

2d9f31f2af7b675a3d153d2b7f1035a7.png

JFinal 是基于 Java 语言的极速 WEB + ORM 框架,其核心设计目标是开发迅速、代码量少、学习简单、功能强大、轻量级、易扩展、Restful。这篇文章主要介绍了JFinal实现伪静态,需要的朋友可以参考下2018-04-04

b452cee8ec5cd9e58ab98eba17281e59.png

这篇文章主要为大家详细介绍了java语言图形用户登录界面代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2016-06-06

f4838ec7e2d4da28e0b57d4e852dadd4.png

这篇文章主要介绍了JFrame中添加和设置JPanel的方法实例解析,具有一定借鉴价值2018-01-01

最新评论

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值