专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »Java教程 » java读取excel:JAVA读取WORD,EXCEL,POWERPOINT,PDF文件的方法及代码 »正文

java读取excel:JAVA读取WORD,EXCEL,POWERPOINT,PDF文件的方法及代码

来源: 发布时间:星期四, 2009年2月12日 浏览:318次 评论:0


OFFICE文档使用POIControl控件PDF可以使用PDFBOX0.7.3Control控件完全支持中文用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 = WordExtractor(is);//is是WORD文件InputStream
bodyText = ex.getText;
(!bodyText.equals("")){
index.AddIndex(url, title, bodyText);
}
}catch (DocCenterException e) {
throw DocCenterException("无法从该Mocriosoft Word文档中提取内容", e);
}catch(Exception e){
e.prStackTrace;
}
}
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 = StringBuffer;
try{
HSSFWorkbook workbook = HSSFWorkbook(is);//创建对Excel工作簿文件引用
for ( numSheets = 0; numSheets < workbook.getNumberOfSheets; numSheets) {
(null != workbook.getSheetAt(numSheets)) {
HSSFSheet aSheet = workbook.getSheetAt(numSheets);//获得个sheet
for ( rowNumOfSheet = 0; rowNumOfSheet <= aSheet.getLastRowNum; rowNumOfSheet) {
(null != aSheet.getRow(rowNumOfSheet)) {
HSSFRow aRow = aSheet.getRow(rowNumOfSheet); //获得个行
for ( cellNumOfRow = 0; cellNumOfRow <= aRow.getLastCellNum; cellNumOfRow) {
(null != aRow.getCell(cellNumOfRow)) {
HSSFCell aCell = aRow.getCell(cellNumOfRow);//获得列值
content.append(aCell.getStringCellValue);
}
}
}
}
}
}
(!content.equals("")){
index.AddIndex(url, title, content.toString);
}
}catch (DocCenterException e) {

throw DocCenterException("无法从该Mocriosoft Word文档中提取内容", e);
}catch(Exception e) {
.out.prln("已运行xlRead : " + e );
}
null;
}



PowerPo:

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 = StringBuffer("");
try{
SlideShow ss = SlideShow( HSLFSlideShow(is));//is 为文件InputStream建立SlideShow
Slide slides = ss.getSlides;//获得每张幻灯片
for( i=0;i<slides.length;i){
TextRun t = slides[i].getTextRuns;//为了取得幻灯片文字内容建立TextRun
for( 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){
.out.prln(ex.toString);
}
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 DocCenterException("无法处理该PDF文档", e);
}
(cosDoc.isEncrypted) {
(cosDoc != null)
closeCOSDocument(cosDoc);
throw DocCenterException("该PDF文档是加密文档无法处理");
}
String docText = null;


try {
PDFTextStripper stripper = PDFTextStripper;
docText = stripper.getText( PDDocument(cosDoc));
} catch (IOException e) {
closeCOSDocument(cosDoc);
throw DocCenterException("无法处理该PDF文档", e);
}

PDDocument pdDoc = null;
try {
pdDoc = PDDocument(cosDoc);
PDDocumentInformation docInfo = pdDoc.getDocumentInformation;
(docInfo.getTitle!=null && !docInfo.getTitle.equals("")){
title = docInfo.getTitle;
}

} catch (Exception e) {
closeCOSDocument(cosDoc);
closePDDocument(pdDoc);
.err.prln("无法取得该PDF文档元数据" + e.getMessage);
} finally {
closeCOSDocument(cosDoc);
closePDDocument(pdDoc);
}

null;
}

private COSDocument parseDocument(InputStream is) throws IOException {
PDFParser parser = PDFParser(is);
parser.parse;
parser.getDocument;
}

private void closeCOSDocument(COSDocument cosDoc) {
(cosDoc != null) {
try {
cosDoc.close;
} catch (IOException e) {
}
}
}

private void closePDDocument(PDDocument pdDoc) {
(pdDoc != null) {
try {
pdDoc.close;
} catch (IOException e) {
}
}
}
0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: