首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 程式設計入門

Java 使用 Apache POI 與 PDFBox 讀取 Word、Excel、PPT、PDF 文字內容完整範例

     編輯:Java 程式設計入門

前言

本文整合 Apache POI(處理 Office 97-2003 格式)與 PDFBox,提供四種常見文件格式的文字擷取完整代碼,適合建立全文檢索、文件轉檔、內容分析等系統。

依賴庫版本(原文測試版本)

  • POI:3.0-rc4(現建議升級至 5.x+)
  • PDFBox:0.7.3(現建議升級至 3.x+)

官網:POI | PDFBox

1. Word (.doc) - 使用 WordExtractor

import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import java.io.InputStream;

public Document getWordDocument(Index index, String url, String title, InputStream is) throws DocCenterException {
    try {
        WordExtractor ex = new WordExtractor(is);
        String bodyText = ex.getText();
        if (bodyText != null && !bodyText.isEmpty()) {
            index.AddIndex(url, title, bodyText);
        }
    } catch (Exception e) {
        throw new DocCenterException("無法從 Word 文件提取內容", e);
    }
    return null;
}

注意:hwpf 僅支援 .doc(二進位格式),.docx 請改用 org.apache.poi.xwpf.extractor.XWPFWordExtractor。

2. Excel (.xls) - 使用 HSSF 逐列逐儲存格讀取

import org.apache.poi.hssf.usermodel.*;
import java.io.InputStream;
import java.lang.StringBuffer;

public Document getExcelDocument(Index index, String url, String title, InputStream is) throws DocCenterException {
    StringBuffer content = new StringBuffer();
    try {
        HSSFWorkbook wb = new HSSFWorkbook(is);
        for (int s = 0; s < wb.getNumberOfSheets(); s++) {
            HSSFSheet sheet = wb.getSheetAt(s);
            if (sheet == null) continue;
            for (int r = 0; r <= sheet.getLastRowNum(); r++) {
                HSSFRow row = sheet.getRow(r);
                if (row == null) continue;
                for (short c = 0; c <= row.getLastCellNum(); c++) {
                    HSSFCell cell = row.getCell(c);
                    if (cell != null) {
                        content.append(cell.getStringCellValue());
                    }
                }
            }
        }
        if (content.length() > 0) index.AddIndex(url, title, content.toString());
    } catch (Exception e) {
        throw new DocCenterException("無法從 Excel 文件提取內容", e);
    }
    return null;
}

注意:.xlsx 請改用 XSSFWorkbook;儲存格型別判斷建議用 cell.getCellType() 後再取值。

3. PowerPoint (.ppt) - 使用 HSLF 擷取投影片文字

import org.apache.poi.hslf.*;
import org.apache.poi.hslf.model.*;
import java.io.InputStream;

public Document getPptDocument(Index index, String url, String title, InputStream is) throws DocCenterException {
    StringBuffer content = new StringBuffer();
    try {
        SlideShow ss = new SlideShow(new HSLFSlideShow(is));
        Slide[] slides = ss.getSlides();
        for (Slide slide : slides) {
            TextRun[] runs = slide.getTextRuns();
            for (TextRun run : runs) content.append(run.getText());
            content.append(slide.getTitle());
        }
        index.AddIndex(url, title, content.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

注意:.pptx 請改用 org.apache.poi.xslf 套件。

4. PDF - 使用 PDFBox 擷取文字與中繼資料

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 java.io.InputStream;
import java.io.IOException;

public Document getPdfDocument(Index index, String url, String title, InputStream is) throws DocCenterException {
    COSDocument cosDoc = null;
    PDDocument pdDoc = null;
    try {
        PDFParser parser = new PDFParser(is);
        parser.parse();
        cosDoc = parser.getDocument();

        if (cosDoc.isEncrypted()) throw new DocCenterException("加密 PDF 無法處理");

        PDFTextStripper stripper = new PDFTextStripper();
        String text = stripper.getText(new PDDocument(cosDoc));
        index.AddIndex(url, title, text);

        // 讀取中繼資料標題
        pdDoc = new PDDocument(cosDoc);
        PDDocumentInformation info = pdDoc.getDocumentInformation();
        if (info.getTitle() != null && !info.getTitle().isEmpty()) {
            title = info.getTitle();
        }
    } catch (IOException e) {
        throw new DocCenterException("無法處理 PDF 文件", e);
    } finally {
        closeQuietly(cosDoc);
        closeQuietly(pdDoc);
    }
    return null;
}

private void closeQuietly(COSDocument doc) {
    if (doc != null) try { doc.close(); } catch (IOException ignored) {}
}
private void closeQuietly(PDDocument doc) {
    if (doc != null) try { doc.close(); } catch (IOException ignored) {}
}

現代化升級建議(POI 5.x / PDFBox 3.x)

  • 統一使用 try-with-resources 管理資源。
  • Word:XWPFWordExtractor (.docx) + WordExtractor (.doc)
  • Excel:WorkbookFactory.create(is) 自動偵測 .xls/.xlsx
  • PPT:SlideShowFactory.create(is) 自動偵測 .ppt/.pptx
  • PDF:PDFTextStripper 支援排序、區域擷取等進階功能。

總結

掌握 POI 與 PDFBox 的核心 API,即可快速建立企業級文件內容擷取管線。實務上建議封裝為統一介面 DocumentExtractor,依副檔名自動派發對應實作,並加入例外處理、日誌、編碼偵測等健壯性機制。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved