使用 Apache POI 處理 Microsoft Office 文檔

jopen 9年前發布 | 50K 次閱讀 POI Office文檔處理

POI 概述

Apache POI 項目的使命是創造和維護 Java API 操縱各種格式的文件,其中包括基于 Office Open XML 標準(OOXML)和微軟的OLE 2 Compound Document 格式(OLE2)。總之,你可以使用 Java 讀寫 MS Excel 文件。此外,您可以使用 Java 讀取和寫入 MS Word 和 MS PowerPoint 文件。Apache POI 是你的 Java Excel 解決方案(用于Excel 97-2008)。包含了一個完整的 API 用于移植其他 OOXML 和OLE2 格式。

OLE2 文件包括了 Microsoft Office 文件,比如 XLS, DOC, PPT 以及 MFC 的序列化 API 為基礎的文件格式。項目提供 OLE2 Filesystem (POIFS)OLE2 Document Properties (HPSF) 等 API。

Office OpenXML Format 是 Microsoft Office 2007 和 2008 中新的基于 XML 的標準。包括 XLSX, DOCX 和 PPTX。該項目提供了一個低級別的 API 使用 openxml4j來支持 Open Packaging Conventions(開放打包約定)。

針對每個 現存的 MS Office 模塊組件,試圖提供一個共同的高級別 Java api 給 OLE2 和 OOXML 文檔格式。Excel (SS=HSSF+XSSF)
Word (HWPF+XWPF),PowerPoint (HSLF+XSLF),Outlook (HSMF), Visio (HDGF), TNEF (HMEF), 和 Publisher (HPBF).

該項目盡可能與其他項目合作提供此功能。比如:Cocoon提供 HSSF 的序列化;與Open Office.org合作處理 XLS 格式;和 Tika/ Lucene提供格式解釋器。

組件

</tr>

</tr>

</tr>

</tr>

</tr>

</tr>

</tr>

</tr>

</tr>

</tr>

</tr>

</tr>

</tr>

</tr> </tbody> </table>

使用

下面展示使用 HSSF 來創建 excle 的案例

創建一個 POI 項目

mvn archetype:create -DgroupId=com.waylau.poi -DartifactId=poi-demos

引入相關 POI 依賴

本例子是版本 3.11

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.11</version>
</dependency>

第一個 HelloWorld 程序

新增一個 HelloWorld.java

HSSFWorkbook() 用來創建工作簿

Workbook wb = new HSSFWorkbook();  // 或 new XSSFWorkbook();

wb.createShee 是用來創建 Sheet

Sheet sheet1 = wb.createSheet("第1個 sheet");
Sheet sheet2 = wb.createSheet("第2個 sheet");

有 Sheet 的命名具有一定的限制性,比如,不能用*、?等,為了方便、安全的命名,可以使用
org.apache.poi.ss.util.WorkbookUtil#createSafeSheetName方法,

String safeName = WorkbookUtil.createSafeSheetName("[WayLau's Blog*?]"); // 返回 " WayLau's Blog   "

Sheet sheet3 = wb.createSheet(safeName);</pre>

創建數據 行。Row 是從 0 開始的

Row row = sheet1.createRow((short)0);

創建 Cell 放值進去

Cell cell0 = row.createCell(0);
cell0.setCellValue(1);

更簡單的做法是在一行代碼實現

row.createCell(1).setCellValue(1.2);
row.createCell(2).setCellValue(true);

可以用 CreationHelper 處理實例化具體類的不同實例

CreationHelper createHelper = wb.getCreationHelper();
row.createCell(3).setCellValue(createHelper.createRichTextString("This is a string"));

使用 CellStyle,用來設置 Cell 的樣式

CellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(
    createHelper.createDataFormat().getFormat("m/d/yy h:mm"));

Cell cell4 = row.createCell(4); cell4.setCellValue(new Date()); cell4.setCellStyle(cellStyle);</pre>

最后生成 excle 文件

FileOutputStream fileOut = new FileOutputStream("helloword.xls");
wb.write(fileOut);
fileOut.close();

效果

項目源碼

https://github.com/waylau/poi-demos 項目中 HelloWorld.java 示例

參考:

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!
組件 應用類型 Maven artifactId 備注
POIFS OLE2 Filesystem poi Required to work with OLE2 / POIFS based files
HPSF OLE2 Property Sets poi  
HSSF Excel XLS poi For HSSF only, if common SS is needed see below
HSLF PowerPoint PPT poi-scratchpad  
HWPF Word DOC poi-scratchpad  
HDGF Visio VSD poi-scratchpad  
HPBF Publisher PUB poi-scratchpad  
HSMF Outlook MSG poi-scratchpad  
OpenXML4J OOXML poi-ooxml plus either poi-ooxml-schemas or
ooxml-schemas and ooxml-security
see below for differences
XSSF Excel XLSX poi-ooxml  
XSLF PowerPoint PPTX poi-ooxml  
XWPF Word DOCX poi-ooxml  
Common SS Excel XLS and XLSX poi-ooxml WorkbookFactory and friends all require poi-ooxml, not just core poi
  • sesese色