使用Apache POI HSSF来创建新的excel文件

大多数人都应该使用高级的API (包名: org.apache.poi.ss.usermodel) ,它的使用非常简单。

The high level API (package: org.apache.poi.ss.usermodel) is what most people should use. Usage is very simple.

工作簿(Workbook)是通过创建(org.apache.poi.ss.usermodel.Workbook)的一个实例来创建的,也可以直接了当地通过具体的类(org.apache.poi.hssf.usermodel.HSSFWorkbook或者org.apache.poi.xssf.usermodel.XSSFWorkbook)来创建,亦或通过使用非常方便的工厂类(org.apache.poi.ss.usermodel.WorkbookFactory)来创建。

Workbooks are created by creating an instance of org.apache.poi.ss.usermodel.Workbook. Either create a concrete class directly (org.apache.poi.hssf.usermodel.HSSFWorkbook or org.apache.poi.xssf.usermodel.XSSFWorkbook), or use the handy factory class org.apache.poi.ss.usermodel.WorkbookFactory.

表单(Sheet)可以通过调用一个已经存在的工作簿(Workbook)实例的createSheet()方法来创建,通过这个方法创建的表单(Sheet)会自动地按顺序添加到这个工作簿(Workbook)里,表单(Sheet)本身并不具有名称(在表单底部的标签上显示的);你可以通过调用Workbook.setSheetName(sheetindex,”SheetName”,encoding)来设置一个表单(Sheet)的名称。

/格式:Workbook.setSheetName(表单序号,”表单名称”,编码格式)/

对于HSSF,表单(Sheet)的名称可能是8bit格式(HSSFWorkbook.ENCODING_COMPRESSED_UNICODE)或者Unicode格式(HSSFWorkbook.ENCODING_UTF_16)的。HSSF的默认编码格式是每个字符8个bit。对于XSSF,表单(Sheet)的名称默认作为Unicode编码格式处理。

Sheets are created by calling createSheet() from an existing instance of Workbook, the created sheet is automatically added in sequence to the workbook. Sheets do not in themselves have a sheet name (the tab at the bottom); you set the name associated with a sheet by calling Workbook.setSheetName(sheetindex,”SheetName”,encoding). 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.

(Rows)是通过调用一个已经存在的表单(Sheet)实例的createRow(rowNumber)方法来创建的。只有拥有单元格(cell)的行能被添加到表单(Sheet)里。你可以通过调用一个行(Rows)对象的setRowHeight(height)方法来设置行高。行高必须以缇(twips)或者二十分之一pt为单位。如果你愿意,我们也提供一个以pt为单位设置行高的函数。

/关于单位”point”:1/20th means “one twentieth”, i.e, 5%. So 1/20th point is 1/1440th inch. At 72 pixels-per-inch that means that 20 of those units equal one pixel./

Rows are created by calling createRow(rowNumber) from an existing instance of Sheet. Only rows that have cell values should be added to the sheet. To set the row’s height, you just call 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.

单元格(cell)是通过调用一个已经存在的行(Rows)实例的createCell(column, type)方法来创建的。只有非空的单元格(cell)能被添加到行(Rows)里。单元格(cell)应该依照它们的值是数字值还是文本值来设置为Cell.CELL_TYPE_NUMERIC类型或者Cell.CELL_TYPE_STRING类型。单元格(cell)必须设置一个值。你可以通过调用setCellValue方法来设置这个值,参数为String类型或者double类型。单个单元格(cell)没有宽度;比必须通过Sheet对象调用setColumnWidth(colindex, width)方法(宽度单位是256分之1个字符)来设置。(在GUI中你也不能对单个basis这么干。)

Cells are created by calling createCell(column, type) from an existing Row. Only cells that have values should be added to the row. Cells should have their cell type set to either Cell.CELL_TYPE_NUMERIC or Cell.CELL_TYPE_STRING depending on whether they contain a numeric or textual value. Cells must also have a value set. Set the value by calling setCellValue with either a String or double as a parameter. Individual cells do not have a width; you must call setColumnWidth(colindex, width) (use units of 1/256th of a character) on the Sheet object. (You can’t do it on an individual basis in the GUI either).

单元格(cell)是通过CellStyle对象来风格化的,CellStyle对象包含一个Font对象的引用。它们都是通过调用一个工作簿(Workbook)对象的createCellStyle()createFont()方法来创建。一旦你创建了它们,你必须给它们设置参数(包括颜色,边框等)。给一个CellStyle设置Font可以通过调用setFont(fontobj)方法来实现。

Cells are styled with CellStyle objects which in turn contain a reference to an Font object. These are created via the Workbook 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 CellStyle call setFont(fontobj).

一旦你创建了你的工作簿(Workbook),你可以通过调用write(outputStream)方法来输出你的工作簿(Workbook)实例到输出流里(例如:一个FileOutputStream或者ServletOutputStream)。你必须自己关闭输出流,HSSF不会帮你关了它。

Once you have generated your workbook, you can write it out by calling write(outputStream) from your instance of Workbook, passing it an OutputStream (for instance, a FileOutputStream or ServletOutputStream). You must close the OutputStream yourself. HSSF does not close it for you.

翻译自:https://poi.apache.org/spreadsheet/how-to.html

原创文章,转载请注明: 转载自Sdcxv

本文链接地址: 使用Apache POI HSSF来创建新的excel文件

作者: Sdcxv

Mostly Harmless