亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java讀取word-excel-ppt文件代碼

 更新時(shí)間: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文檔中提取內(nèi)容", 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);//創(chuàng)建對(duì)Excel工作簿文件的引用
for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) {
if (null != workbook.getSheetAt(numSheets)) {
HSSFSheet aSheet = workbook.getSheetAt(numSheets);//獲得一個(gè)sheet
for (int rowNumOfSheet = 0; rowNumOfSheet <= aSheet.getLastRowNum(); rowNumOfSheet++) {
if (null != aSheet.getRow(rowNumOfSheet)) {
HSSFRow aRow = aSheet.getRow(rowNumOfSheet); //獲得一個(gè)行
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文檔中提取內(nèi)容", e);
}catch(Exception e) {
System.out.println("已運(yùn)行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();//為了取得幻燈片的文字內(nèi)容,建立TextRun
for(int j=0;j<t.length;j++){
content.append(t[j].getText());//這里會(huì)將文字內(nèi)容加到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("無法處理該P(yáng)DF文檔", e);
}
if (cosDoc.isEncrypted()) {
if (cosDoc != null)
closeCOSDocument(cosDoc);
throw new DocCenterException("該P(yáng)DF文檔是加密文檔,無法處理");
}
String docText = null;
try {
PDFTextStripper stripper = new PDFTextStripper();
docText = stripper.getText(new PDDocument(cosDoc));
} catch (IOException e) {
closeCOSDocument(cosDoc);
throw new DocCenterException("無法處理該P(yáng)DF文檔", 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("無法取得該P(yáng)DF文檔的元數(shù)據(jù)" + 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) {
}
}
}
代碼復(fù)制可能出錯(cuò),不過代碼經(jīng)過測(cè)試,絕對(duì)能用,POI為3.0-rc4,PDFBOX為0.7.3

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

相關(guān)文章

  • 深入理解 Java注解及實(shí)例

    深入理解 Java注解及實(shí)例

    這篇文章主要介紹了深入理解 Java注解及實(shí)例的相關(guān)資料,希望通過本文大家能夠掌握java注解的知識(shí),需要的朋友可以參考下
    2017-09-09
  • RHEL6.5下JDK1.8安裝教程

    RHEL6.5下JDK1.8安裝教程

    這篇文章主要為大家詳細(xì)介紹了RHEL6.5下JDK1.8安裝教程的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • Java基于動(dòng)態(tài)規(guī)劃法實(shí)現(xiàn)求最長(zhǎng)公共子序列及最長(zhǎng)公共子字符串示例

    Java基于動(dòng)態(tài)規(guī)劃法實(shí)現(xiàn)求最長(zhǎng)公共子序列及最長(zhǎng)公共子字符串示例

    這篇文章主要介紹了Java基于動(dòng)態(tài)規(guī)劃法實(shí)現(xiàn)求最長(zhǎng)公共子序列及最長(zhǎng)公共子字符串,簡(jiǎn)單描述了動(dòng)態(tài)規(guī)劃法的概念、原理,并結(jié)合實(shí)例形式分析了Java使用動(dòng)態(tài)規(guī)劃法求最長(zhǎng)公共子序列以及最長(zhǎng)公共子字符串相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2018-08-08
  • 手把手教你如何獲取微信用戶openid

    手把手教你如何獲取微信用戶openid

    眾所周知小程序的openid相當(dāng)重要,它是用戶的唯一標(biāo)識(shí)id,牽扯的支付,登錄,授權(quán)等,下面這篇文章主要給大家介紹了關(guān)于如何獲取微信用戶openid的相關(guān)資料,需要的朋友可以參考下
    2023-02-02
  • 詳解Servlet入門級(jí)設(shè)置(超詳細(xì) IDEA2020版)

    詳解Servlet入門級(jí)設(shè)置(超詳細(xì) IDEA2020版)

    這篇文章主要介紹了詳解Servlet入門級(jí)設(shè)置(超詳細(xì) IDEA2020版),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • java題解leetcode242.有效的字母異位詞

    java題解leetcode242.有效的字母異位詞

    這篇文章主要為大家介紹了java題解leetcode242.有效的字母異位詞方法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • 深入了解Java行為型設(shè)計(jì)模式之策略模式

    深入了解Java行為型設(shè)計(jì)模式之策略模式

    策略模式屬于Java-設(shè)計(jì)模式中行為模式之一,該模式定義了一系列算法,并將每個(gè)算法封裝起來,使它們可以相互替換。本文將通過示例詳細(xì)講解這一模式,需要的可以參考一下
    2022-09-09
  • 阿里Druid數(shù)據(jù)連接池引發(fā)的線上異常解決

    阿里Druid數(shù)據(jù)連接池引發(fā)的線上異常解決

    這篇文章主要為大家介紹了一次關(guān)于阿里Druid數(shù)據(jù)連接池引發(fā)的線上異常問題的解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-03-03
  • 深入聊一聊springboot項(xiàng)目全局異常處理那些事兒

    深入聊一聊springboot項(xiàng)目全局異常處理那些事兒

    最近在做項(xiàng)目時(shí)需要對(duì)異常進(jìn)行全局統(tǒng)一處理,所以下面這篇文章主要給大家介紹了關(guān)于springboot項(xiàng)目全局異常處理那些事兒,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-01-01
  • Java配置DBeaver的詳細(xì)步驟

    Java配置DBeaver的詳細(xì)步驟

    DBeaver是一個(gè)集成的數(shù)據(jù)庫客戶端工具,需要java語言支持,所以安裝之前需要配置JDK環(huán)境,這篇文章主要介紹了Java配置DBeaver的詳細(xì)步驟,需要的朋友可以參考下
    2021-03-03

最新評(píng)論