使用 Apache POI 處理 Microsoft Office 文檔

原文同步至:http://www.waylau.com/apache-poi-handle-microsoft-documents/html

##POI 概述java

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 格式。git

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

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

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

該項目儘量與其餘項目合做提供此功能。好比:Cocoon提供 HSSF 的序列化;與Open Office.org合做處理 XLS 格式;和 Tika/ Lucene提供格式解釋器。安全

<!-- more -->ide

##組件ui

<table class="ForrestTable" cellspacing="1" cellpadding="4"> <tbody><tr class="b"> <th colspan="1" rowspan="1">組件</th> <th colspan="1" rowspan="1">應用類型</th> <th colspan="1" rowspan="1">Maven artifactId</th> <th colspan="1" rowspan="1">備註</th> <tr class="a"> <td colspan="1" rowspan="1"><a href="http://poi.apache.org/poifs/index.html">POIFS</a></td> <td colspan="1" rowspan="1">OLE2 Filesystem</td> <td colspan="1" rowspan="1"><em>poi</em></td> <td colspan="1" rowspan="1">Required to work with OLE2 / POIFS based files</td> </tr> <tr class="b"> <td colspan="1" rowspan="1"><a href="http://poi.apache.org/hpsf/index.html">HPSF</a></td> <td colspan="1" rowspan="1">OLE2 Property Sets</td> <td colspan="1" rowspan="1">poi</td> <td colspan="1" rowspan="1">&nbsp;</td> </tr> <tr class="a"> <td colspan="1" rowspan="1"><a href="http://poi.apache.org/spreadsheet/index.html">HSSF</a></td> <td colspan="1" rowspan="1">Excel XLS</td> <td colspan="1" rowspan="1">poi</td> <td colspan="1" rowspan="1">For HSSF only, if common SS is needed see below</td> </tr> <tr class="b"> <td colspan="1" rowspan="1"><a href="http://poi.apache.org/slideshow/index.html">HSLF</a></td> <td colspan="1" rowspan="1">PowerPoint PPT</td> <td colspan="1" rowspan="1">poi-scratchpad</td> <td colspan="1" rowspan="1">&nbsp;</td> </tr> <tr class="a"> <td colspan="1" rowspan="1"><a href="http://poi.apache.org/document/index.html">HWPF</a></td> <td colspan="1" rowspan="1">Word DOC</td> <td colspan="1" rowspan="1">poi-scratchpad</td> <td colspan="1" rowspan="1">&nbsp;</td> </tr> <tr class="b"> <td colspan="1" rowspan="1"><a href="http://poi.apache.org/hdgf/index.html">HDGF</a></td> <td colspan="1" rowspan="1">Visio VSD</td> <td colspan="1" rowspan="1">poi-scratchpad</td> <td colspan="1" rowspan="1">&nbsp;</td> </tr> <tr class="a"> <td colspan="1" rowspan="1"><a href="http://poi.apache.org/hpbf/index.html">HPBF</a></td> <td colspan="1" rowspan="1">Publisher PUB</td> <td colspan="1" rowspan="1">poi-scratchpad</td> <td colspan="1" rowspan="1">&nbsp;</td> </tr> <tr class="b"> <td colspan="1" rowspan="1"><a href="http://poi.apache.org/hsmf/index.html">HSMF</a></td> <td colspan="1" rowspan="1">Outlook MSG</td> <td colspan="1" rowspan="1">poi-scratchpad</td> <td colspan="1" rowspan="1">&nbsp;</td> </tr> <tr class="a"> <td colspan="1" rowspan="1"><a href="http://poi.apache.org/oxml4j/index.html">OpenXML4J</a></td> <td colspan="1" rowspan="1">OOXML</td> <td colspan="1" rowspan="1">poi-ooxml plus either poi-ooxml-schemas or<br>ooxml-schemas and ooxml-security</td> <td colspan="1" rowspan="1">see below for differences</td> </tr> <tr class="b"> <td colspan="1" rowspan="1"><a href="http://poi.apache.org/spreadsheet/index.html">XSSF</a></td> <td colspan="1" rowspan="1">Excel XLSX</td> <td colspan="1" rowspan="1">poi-ooxml</td> <td colspan="1" rowspan="1">&nbsp;</td> </tr> <tr class="a"> <td colspan="1" rowspan="1"><a href="http://poi.apache.org/slideshow/index.html">XSLF</a></td> <td colspan="1" rowspan="1">PowerPoint PPTX</td> <td colspan="1" rowspan="1">poi-ooxml</td> <td colspan="1" rowspan="1">&nbsp;</td> </tr> <tr class="b"> <td colspan="1" rowspan="1"><a href="http://poi.apache.org/document/index.html">XWPF</a></td> <td colspan="1" rowspan="1">Word DOCX</td> <td colspan="1" rowspan="1">poi-ooxml</td> <td colspan="1" rowspan="1">&nbsp;</td> </tr> <tr class="a"> <td colspan="1" rowspan="1"><a href="http://poi.apache.org/spreadsheet/index.html">Common SS</a></td> <td colspan="1" rowspan="1">Excel XLS and XLSX</td> <td colspan="1" rowspan="1">poi-ooxml</td> <td colspan="1" rowspan="1">WorkbookFactory and friends all require poi-ooxml, not just core poi</td> </tr> </tbody></table>spa

##使用

下面展現使用 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);

建立數據 行。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);

最後生成 excle 文件

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

效果

##項目源碼

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

##參考:

相關文章
相關標籤/搜索