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 格式。
针对每个 现存的 ms office 模块组件,试图提供一个共同的高级别 java api 给 ole2 和 ooxml 文档格式。excel (ss=hssf+xssf) word (hwpf+xwpf),powerpoint (hslf+xslf),outlook (hsmf), visio (hdgf), tnef (hmef), 和 publisher (hpbf).
组件
应用类型
maven artifactid
备注
<a href="http://poi.apache.org/poifs/index.html">poifs</a>
ole2 filesystem
poi
required to work with ole2 / poifs based files
<a href="http://poi.apache.org/hpsf/index.html">hpsf</a>
ole2 property sets
<a href="http://poi.apache.org/spreadsheet/index.html">hssf</a>
excel xls
for hssf only, if common ss is needed see below
<a href="http://poi.apache.org/slideshow/index.html">hslf</a>
powerpoint ppt
poi-scratchpad
<a href="http://poi.apache.org/document/index.html">hwpf</a>
word doc
<a href="http://poi.apache.org/hdgf/index.html">hdgf</a>
visio vsd
<a href="http://poi.apache.org/hpbf/index.html">hpbf</a>
publisher pub
<a href="http://poi.apache.org/hsmf/index.html">hsmf</a>
outlook msg
<a href="http://poi.apache.org/oxml4j/index.html">openxml4j</a>
ooxml
poi-ooxml plus either poi-ooxml-schemas or
ooxml-schemas and ooxml-security
see below for differences
<a href="http://poi.apache.org/spreadsheet/index.html">xssf</a>
excel xlsx
poi-ooxml
<a href="http://poi.apache.org/slideshow/index.html">xslf</a>
powerpoint pptx
<a href="http://poi.apache.org/document/index.html">xwpf</a>
word docx
<a href="http://poi.apache.org/spreadsheet/index.html">common ss</a>
excel xls and xlsx
workbookfactory and friends all require poi-ooxml, not just core poi
下面展示使用 hssf 来创建 excle 的案例
本例子是版本 3.11
新增一个 helloworld.java
hssfworkbook() 用来创建工作簿
wb.createshee 是用来创建 sheet
有 sheet 的命名具有一定的限制性,比如,不能用 <code>*</code> 、<code>?</code> 等,为了方便、安全的命名,可以使用<code>org.apache.poi.ss.util.workbookutil#createsafesheetname</code> 方法,
创建数据 行。row 是从 0 开始的
创建 cell 放值进去
更简单的做法是在一行代码实现
可以用 creationhelper 处理实例化具体类的不同实例
使用 cellstyle,用来设置 cell 的样式
最后生成 excle 文件
效果

<a href="http://poi.apache.org/index.html">http://poi.apache.org/index.html</a>
<a href="http://poi.apache.org/overview.html#components">http://poi.apache.org/overview.html#components</a>