Apache POI 是 Java 處理 Microsoft Office 格式(OLE2 與 OOXML)的標準函式庫。本文提供 POI 讀取 Word (.doc)、PowerPoint (.ppt)、Excel (.xls) 三種舊版二進位格式的完整程式碼範例。
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version> <!-- 或更新版本 -->
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.17</version>
</dependency>
注意:Office 2007+ 格式需改用 poi-ooxml(XWPFDocument、XSSFWorkbook 等類別)。
public static String readDoc1(InputStream is) throws IOException {
WordExtractor extractor = new WordExtractor(is);
return extractor.getText();
}
public static void readDoc2(InputStream is) throws IOException {
HWPFDocument doc = new HWPFDocument(is);
Range range = doc.getRange();
for (int i = 0; i < range.numSections(); i++) {
Section section = range.getSection(i);
for (int j = 0; j < section.numParagraphs(); j++) {
Paragraph para = section.getParagraph(j);
for (int k = 0; k < para.numCharacterRuns(); k++) {
CharacterRun run = para.getCharacterRun(k);
System.out.print(run.text());
}
}
}
}
第二種方式可保留結構資訊,適合需進一步處理格式、樣式的場景。
public static String readDoc1(InputStream is) throws IOException {
PowerPointExtractor extractor = new PowerPointExtractor(is);
return extractor.getText();
}
public static void readDoc2(InputStream is) throws IOException {
SlideShow ss = new SlideShow(new HSLFSlideShow(is));
Slide[] slides = ss.getSlides();
for (Slide slide : slides) {
// 取得投影片標題
String title = slide.getTitle();
System.out.println("標題: " + title);
// 取得所有文字內容
TextRun[] runs = slide.getTextRuns();
for (TextRun run : runs) {
System.out.println(run.getText());
}
}
}
public static String readDoc1(InputStream is) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(is));
ExcelExtractor extractor = new ExcelExtractor(wb);
extractor.setFormulasNotResults(false); // 顯示公式結果而非公式
extractor.setIncludeSheetNames(true); // 包含工作表名稱
return extractor.getText();
}
public static double getAvg(InputStream is) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(is));
HSSFSheet sheet = wb.getSheetAt(0); // 取第一個工作表
double molecule = 0.0;
double denominator = 0.0;
Iterator<Row> rowIter = sheet.rowIterator();
while (rowIter.hasNext()) {
HSSFRow row = (HSSFRow) rowIter.next();
HSSFCell cell1 = row.getCell(4); // 第 5 欄
HSSFCell cell2 = row.getCell(5); // 第 6 欄
// 型別檢查
if (cell1.getCellType() != HSSFCell.CELL_TYPE_NUMERIC ||
cell2.getCellType() != HSSFCell.CELL_TYPE_NUMERIC) {
throw new IllegalStateException("資料型別錯誤,預期數值型別");
}
denominator += cell2.getNumericCellValue();
molecule += cell2.getNumericCellValue() * cell1.getNumericCellValue();
}
return molecule / denominator;
}
| 檔案類型 | 主要類別 | 提取器 | 適用格式 |
|---|---|---|---|
| Word | HWPFDocument | WordExtractor | .doc (OLE2) |
| PPT | HSLFSlideShow / SlideShow | PowerPointExtractor | .ppt (OLE2) |
| Excel | HSSFWorkbook | ExcelExtractor | .xls (OLE2) |
SAX 風格事件驅動解析(XSSFReader、XLSXReader)避免 OOMXWPFDocument、XMLSlideShow、XSSFWorkbookgetCellTypeEnum() (POI 3.15+) 取代已棄用常數DateUtil.isCellDateFormatted() 判斷後再轉換