target:learning the APi of .xlsx generation and .xlsx data outputting etc
component APIs
OverView
Excel() (SS=HSSF+XSSF+SXSSF)
1:definition:
HSSF is the POI Project‘s pure Java implementation of the Excel ‘97(-2007) file format
XSSF is the POI Project‘s pure Java implementation of the Excel 2007 OOXML(.xlsx) file format
2:functions:
HSSF and XSSF provides ways to read spreadsheets create,modify,read and write XLS
spreadsheet.they provide:
low level structures for those with special needs
an eventmodel api for efficient read-only access
a full usermodel api for creating, reading and modifying XLS files
3:Other
(1)SS Usermodel for HSSF and XSSF support, seeing the ss usermodel converting guide
(2)An alrernate way of generating a spreadsheet is via the Cocoon seralizer
(3)merely reading spreadsheet data, then use the eventmodel api in either the org.apache.poi.hssf.eventusermodel package
or the org.apache.poi.xssf.eventusermodel package, depending on your file format.
(4)If you‘re modifying spreadsheet data then use the usermodel api. You can also generate spreadsheets this way.
SXSSF:
origin:Since 3.8-beta3,POI provides a low-memory footprint SXSSF API built on top of XSSF.
快速指导:
How to use The HSSF APi:
Capabilities:
HSSF
(1)
allows numeric,string,date or formula cell values to be written to or read
from an XLS files.
(2)
supports row and aolumn sizing,and cell styling(bold,italics etc)
supports for both built-in and user defined data formats
(3)
is available that event-based(only read)APi for reading XLS files.
General use
User API(HSSF and XSSF)
Writing a new file
(1)The high level Api(package:org.apache.poi.ss.usermodel)is what most people should
use,usage is very simple.
first step(create Workbooks)
one way:creating an instance of org.apache.poi.ss.usermodel.Workbook
two way:create a concrete class directly (org.apache.poi.hssf.usermodel.
HSSFWorkbook or org.apache.poi.xssf.usermodel.XSSFWorkbook)
three way:use the handy factory class org.apache.poi.ss.usermodel.WorkbookFactory.
second step(create sheet):
(1)Sheets are created by calling createSheet() from an existing instance of Workbook
(2)the created sheet is automatically added in sequence to the workbook.
(3)you set the name associated with a sheet by calling Workbook.setSheetName(sheetindex,"SheetName",encoding).
(4)For HSSF, the name may be in 8bit format (HSSFWorkbook.ENCODING_COMPRESSED_UNICODE) or Unicode (HSSFWorkbook.ENCODING_UTF_16). Default encoding for
HSSF is 8bit per char. For XSSF, the name is automatically handled as unicode.
third step(create Row)
(1)Row are created by calling createRow(rowNumber)from an existing instance of Sheet
setRowHeight(height) on the row object
The height must be given in twips, or 1/20th of a point. If you prefer, there is also a setRowHeightInPoints method.
forth step(create Cells)
(1)Cells are created by calling createCell(column,type)from an existing Row.
Cells should have their cell type set to either Cell.CELL_TYPE_NUMERIC or Cell.CELL_TYPE_STRING
Cells must also have a value set. Set the value by calling setCellValue with either a String or double as a parameter.
you must call setColumnWidth(colindex, width) (use units of 1/256th of a character) on the Sheet object.
fifth step(create Styles)
Cells are styled with CellStyle objects which in turn contain a reference to an Font object
calling createCellStyle() and createFont()
To set a font for an CellStyle call setFont(fontobj).
Once you have generated your workbook, you can write it out by calling write(outputStream)
passing it an OutputStream (for instance, a FileOutputStream or ServletOutputStream).
组件映射:
Component Map
The Apache POI distribution consists of support for many document file formats. This support is provided in several Jar files. Not all of the Jars are needed for every format. The following tables show the relationships between POI components, Maven repository tags, and the project‘s Jar files.
Component | Application type | Maven artifactId | Notes |
---|---|---|---|
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 | |
DDF | Escher common drawings | poi | |
HWMF | WMF drawings | poi-scratchpad | |
OpenXML4J | OOXML | poi-ooxml plus either poi-ooxml-schemasor ooxml-schemas and ooxml-security |
See notes below for differences between these options |
XSSF | Excel XLSX | poi-ooxml | |
XSLF | PowerPoint PPTX | poi-ooxml | |
XWPF | Word DOCX | poi-ooxml | |
XDGF | Visio VSDX | poi-ooxml | |
Common SL | PowerPoint PPT and PPTX | poi-scratchpad and poi-ooxml | SL code is in the core POI jar, but implementations are in poi-scratchpad and poi-ooxml. |
Common SS | Excel XLS and XLSX | poi-ooxml | WorkbookFactory and friends all require poi-ooxml, not just core poi |