日期:2014-05-16  浏览次数:20822 次

Apache poi生成excel文件
使用HSSF API创建excel文档
几个实体
HSSFWorkBook  整个excel文件
HSSFSheet    
HSSFRow
HSSFCell

创建excel的类在org.apache.poi.hssf.usermodel包中。
HSSFWorkBook
WorkBook是通过new HSSFWorkBook实例来创建。
HSSFSheet
Sheet通过HSSFWorkBook实例的createSheet()函数来创建。
新创建的多个sheet自动按照顺序添加到WorkBook。
Sheet创建的时候并没有名字(底部tab显示的名称),需要调用HSSFWorkbook的setSheetName函数来手工设置。如
HSSFWorkbook.setSheetName(sheetindex,"SheetName",encoding).
参数sheetindex
      从0开始
参数encoding可以取两个值
HSSFWorkbook.ENCODING_COMPRESSED_UNICODE
HSSFWorkbook.ENCODING_UTF_16
encoding可以不指定,默认是ENCODING_COMPRESSED_UNICODE(8bit)
HSSFRow
Row是通过HSSFSheet实例的createRow(rowNumber)函数创建的。
参数rowNumber从0开始。
Only rows that hava cell values should be added to the sheet.
可以调用setRowHeight(height)函数设置Row的高度;
其中height单位为twip,即1/20个point。
高度也可以通过setRowHeightInPoints函数来设置。
HSSFCell
Cell通过HSSFRow实例的createCell(column, type)函数来创建。
Only cells that have values should be added to the row.
Cell的type
HSSFCell.CELL_TYPE_NUMERIC  numeric
HSSFCell.CELL_TYPE_STRING    texual
Cell的值
      调用setCellValue(para)函数来设置。Para参数是String或者double。
单个Cell没有width值,必须HSSFSheet实例的setColumnWidth(colindex, width)函数来设置,单位是1/256个character。
HSSFCellStyle  HSSFFont
Cells are styled with HSSFCellStyle objects which in turn contain a reference to an HSSFFont object. These are created via the HSSFWorkbook object by calling createCellStyle() and createFont(). Once you create the object you must set its parameters (colors, borders, etc). To set a font for an HSSFCellStyle call setFont(fontobj).

把poi-2.5-final-20040302.jar一个包添加到CLASSPATH就可以