📅  最后修改于: 2020-11-18 09:00:45             🧑  作者: Mango
本章介绍了Apache POI API下的一些类和方法,这些类和方法对于使用Java程序处理Excel文件至关重要。
这是创建或维护Excel工作簿的所有类的超级接口。它属于org.apache.poi.ss.usermodel包。实现此接口的两个类如下:
HSSFWorkbook-此类具有读取和写入.xls格式的Microsoft Excel文件的方法。它与MS-Office 97-2003版本兼容。
XSSFWorkbook-此类具有读取和写入.xls或.xlsx格式的Microsoft Excel和OpenOffice xml文件的方法。它与MS-Office 2007版或更高版本兼容。
它是org.apache.poi.hssf.usermodel包下的一个高级类。它实现了Workbook界面,并用于.xls格式的Excel文件。下面列出了此类下的一些方法和构造函数。
Sr.No. | Constructor & Description |
---|---|
1 |
HSSFWorkbook() Creates a new HSSFWorkbook object from scratch. |
2 |
HSSFWorkbook(DirectoryNode directory, boolean preserveNodes) Creates a new HSSFWworkbook objectinside a specific directory. |
3 |
HSSFWorkbook(DirectoryNode directory, POIFSFileSystem fs, boolean preserveNodes) Given a POIFSFileSystem object and a specific directory within it, it creates an SSFWorkbook object to read a specified workbook. |
4 |
HSSFWorkbook(java.io.InputStream s) Creates a new HSSFWorkbook object using an input stream. |
5 |
HSSFWorkbook(java.io.InputStream s, boolean preserveNodes) Constructs a POI file system around your input stream. |
6 |
HSSFWorkbook(POIFSFileSystem fs) Constructs a new HSSFWorkbook object using a POIFSFileSystem object. |
7 |
HSSFWorkbook(POIFSFileSystem fs, boolean preserveNodes) Given a POIFSFileSystem object, it creates a new HSSFWorkbook object to read a specified workbook. |
这些构造函数中经常使用的参数是-
目录-这是要处理的POI文件系统目录。
fs –是包含工作簿流的POI文件系统。
preparenodes-这是一个可选参数,用于决定是否保留其他节点(例如宏)。由于它会将所有POIFileSystem存储在内存中(如果已设置),因此会占用大量内存。
注– HSSFWorkbook类包含许多方法。但是它们仅与xls格式兼容。在本教程中,重点是Excel文件格式的最新版本。因此,此处未列出HSSFWorkbook的类方法。如果您需要这些类方法,请参阅https://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFWorkbook.html上的POI-HSSFWorkbook类API 。
该类用于表示高级和低级Excel文件格式。它属于org.apache.xssf.usemodel包,并实现Workbook接口。下面列出的是此类下的方法和构造函数。
Sr.No. | Constructor & Description |
---|---|
1 |
XSSFWorkbook() Creates a new XSSFworkbook object from scratch. |
2 |
XSSFWorkbook(java.io.File file) Constructs an XSSFWorkbook object from a given file. |
3 |
XSSFWorkbook(java.io.InputStream is) Constructs an XSSFWorkbook object, by buffering the whole input stream into memory and then opening an OPCPackage object for it. |
4 |
XSSFWorkbook(java.lang.String path) Constructs an XSSFWorkbook object given the full path of a file. |
Sr.No. | Method & Description |
---|---|
1 |
createSheet() Creates an XSSFSheet for this workbook, adds it to the sheets, and returns the high level representation. |
2 |
createSheet(java.lang.String sheetname) Creates a new sheet for this Workbook and returns the high level representation. |
3 |
createFont() Creates a new font and adds it to the workbook’s font table. |
4 |
createCellStyle() Creates a new XSSFCellStyle and adds it to the workbook’s style table. |
5 |
createFont() Creates a new font and adds it to the workbook’s font table. |
6 |
setPrintArea(int sheetIndex, int startColumn, int endColumn, int startRow,int endRow) Sets the print area of a given sheet as per the specified parameters. |
有关此类的其余方法,请参阅以下位置的完整API文档: https : //poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFWorkbook.html。有关方法的完整列表。
Sheet是org.apache.poi.ss.usermodel包下的接口,并且是创建具有特定名称的高级或低级电子表格的所有类的超级接口。电子表格最常见的类型是工作表,它表示为单元格网格。
这是org.apache.poi.hssf.usermodel包下的类。它可以创建excel电子表格,并允许格式化工作表样式和工作表数据。
Sr.No. | Constructor & Description |
---|---|
1 |
HSSFSheet(HSSFWorkbook workbook) Creates new HSSFSheet called by HSSFWorkbook to create a sheet from scratch. |
2 |
HSSFSheet(HSSFWorkbook workbook, InternalSheet sheet) Creates an HSSFSheet representing the given sheet object. |
这是一个代表excel电子表格高级表示的类。它在org.apache.poi.hssf.usermodel包下。
Sr.No. | Constructor & Description |
---|---|
1 |
XSSFSheet() Creates new XSSFSheet – called by XSSFWorkbook to create a sheet from scratch. |
2 |
XSSFSheet(PackagePart part, PackageRelationship rel) Creates an XSSFSheet representing the given package part and relationship. |
Sr.No. | Method & Description |
---|---|
1 |
addMergedRegion(CellRangeAddress region) Adds a merged region of cells (hence those cells form one). |
2 |
autoSizeColumn(int column) Adjusts the column width to fit the contents. |
3 |
iterator() This method is an alias for rowIterator() to allow foreach loops |
4 |
addHyperlink(XSSFHyperlink hyperlink) Registers a hyperlink in the collection of hyperlinks on this sheet |
有关此类的其余方法,请参见以下网址的完整API: https : //poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFSheet.html。
这是org.apache.poi.ss.usermodel包下的接口。它用于电子表格行的高级表示。它是代表POI库中行的所有类的超级接口。
这是org.apache.poi.xssf.usermodel包下的类。它实现了Row接口,因此可以在电子表格中创建行。下面列出的是此类下的方法和构造函数。
Sr.No. | Method & Description |
---|---|
1 |
createCell(int columnIndex) Creates new cells within the row and returns it. |
2 |
setHeight(short height) Sets the height in short units. |
对于此类的其余方法,请访问给定的链接https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFRow.html
这是org.apache.poi.ss.usermodel包下的接口。它是表示电子表格行中单元格的所有类的超级接口。
单元格可以具有各种属性,例如空白,数字,日期,错误等。单元格在添加到行之前应具有自己的数字(从0开始)。
这是org.apache.poi.xssf.usermodel包下的类。它实现了Cell接口。它是电子表格行中单元格的高级表示。
下面列出了XSSFCell类的一些字段及其说明。
Sr.No. | Cell Type & Description |
---|---|
1 |
CELL_TYPE_BLANK Represents blank cell |
2 |
CELL_TYPE_BOOLEAN Represents Boolean cell (true or false) |
3 |
CELL_TYPE_ERROR Represents error value on a cell |
4 |
CELL_TYPE_FORMULA Represents formula result on a cell |
5 |
CELL_TYPE_NUMERIC Represents numeric data on a cell |
6 |
CELL_TYPE_STRING Represents string (text) on a cell |
Sr.No. | Method & Description |
---|---|
1 |
setCellStyle(CellStyle style) Sets the style for the cell. |
2 |
setCellType(int cellType) Sets the type of cells (numeric, formula, or string). |
3 |
setCellValue(boolean value) Sets a boolean value for the cell. |
4 |
setCellValue(java.util.Calendar value) Sets a date value for the cell. |
5 |
setCellValue(double value) Sets a numeric value for the cell. |
6 |
setCellValue(java.lang.String str) Sets a string value for the cell. |
7 |
setHyperlink(Hyperlink hyperlink) Assigns a hyperlink to this cell. |
有关此类的其余方法和字段,请访问以下链接: https : //poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFCell.html
这是org.apache.poi.xssf.usermodel包下的类。它将在电子表格的单元格中提供有关内容格式的可能信息。它还提供用于修改该格式的选项。它实现了CellStyle接口。
下表列出了从CellStyle接口继承的一些字段。
Sr.No. | Field & Description |
---|---|
1 |
ALIGN_CENTER Center align the cell contents |
2 |
ALIGN_CENTER_SELECTION Center-selection horizontal alignment |
3 |
ALIGN_FILL Cell fit to the content size |
4 |
ALIGN_JUSTIFY Fit cell contents to its width |
5 |
ALIGN_LEFT Left align the cell contents |
6 |
ALIGN_RIGHT Right align the cell contents |
7 |
BORDER_DASH_DOT Cell style with dash and dot |
8 |
BORDER_DOTTED Cell style with dotted border |
9 |
BORDER_DASHED Cell style with dashed border |
10 |
BORDER_THICK Cell style with thick border |
11 |
BORDER_THIN Cell style with thin border |
12 |
VERTICAL_BOTTOM Align the cell contents vertical bottom |
13 |
VERTICAL_CENTER Align the cell contents vertical center |
15 |
VERTICAL_JUSTIFY Align and justify the cell contents vertically |
16 |
VERTICAL_TOP Top aligned vertical alignment |
Sr.No. | Constructor & Description |
---|---|
1 |
XSSFCellStyle(int cellXfId, int cellStyleXfId, StylesTable stylesSource, ThemesTable theme) Creates a cell style from the supplied parts |
2 |
XSSFCellStyle(StylesTable stylesSource) Creates an empty cell Style |
S.No | Method & Description |
---|---|
1 |
setAlignment(short align) Sets the type of horizontal alignment for the cell |
2 |
setBorderBottom(short border) Sets the type of border for the bottom border of the cell |
3 |
setBorderColor(XSSFCellBorder.BorderSide side, XSSFColor color) Sets the color for the selected border |
4 |
setBorderLeft(Short border) Sets the type of border for the left border of the cell |
5 |
setBorderRight(short border) Sets the type of border for the right border of the cell |
6 |
setBorderTop(short border) Sets the type of border for the top border of the cell |
7 |
setFillBackgroundColor(XSSFColor color) Sets the background fill color represented as an XSSFColor value. |
8 |
setFillForegroundColor(XSSFColor color) Sets the foreground fill color represented as an XSSFColor value. |
9 |
setFillPattern(short fp) Specifies the cell fill information for pattern and solid color cell fills. |
10 |
setFont(Font font) Sets the font for this style. |
11 |
setRotation(short rotation) Sets the degree of rotation for the text in the cell. |
12 |
setVerticalAlignment(short align) Sets the type of vertical alignment for the cell. |
对于此类中的其余方法和字段,请通过以下链接: https : //poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFCellStyle.html
这是org.apache.poi.hssf.util包下的类。它提供不同的颜色作为嵌套类。通常,这些嵌套类使用其自己的索引表示。它实现了Color接口。
该类的所有嵌套类都是静态的,并且每个类都有其索引。这些嵌套的颜色类用于单元格格式设置,例如单元格内容,边框,前景和背景。下面列出了一些嵌套的类。
Sr.No. | Class names (colors) |
---|---|
1 | HSSFColor.AQUA |
2 | HSSFColor.AUTOMATIC |
3 | HSSFColor.BLACK |
4 | HSSFColor.BLUE |
5 | HSSFColor.BRIGHT_GREEN |
6 | HSSFColor.BRIGHT_GRAY |
7 | HSSFColor.CORAL |
8 | HSSFColor.DARK_BLUE |
9 | HSSFColor.DARK_GREEN |
10 | HSSFColor.SKY_BLUE |
11 | HSSFColor.WHITE |
12 | HSSFColor.YELLOW |
此类的一种方法很重要,该方法用于获取索引值。
Sr.No. | Method & Description |
---|---|
1 |
getIndex() This method is used to get the index value of a nested class. |
有关其余方法和嵌套类,请参见以下链接: https : //poi.apache.org/apidocs/org/apache/poi/hssf/util/HSSFColor.html
这是org.apache.poi.xssf.usermodel包下的类。它用于表示电子表格中的颜色。它实现了Color接口。下面列出了它的一些方法和构造函数。
Sr.No. | Constructor & Description |
---|---|
1 |
XSSFColor() Creates a new instance of XSSFColor. |
2 |
XSSFColor(byte[] rgb) Creates a new instance of XSSFColor using RGB. |
3 |
XSSFColor(java.awt.Color clr) Creates a new instance of XSSFColor using the Color class from the awt package. |
Sr.No. | Method & Description |
---|---|
1 |
setAuto(boolean auto) Sets a boolean value to indicate that the ctColor is automatic and the system ctColor is dependent. |
2 |
setIndexed(int indexed) Sets indexed ctColor value as system ctColor. |
对于其余方法,请访问以下链接: https : //poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFColor.html
这是org.apache.poi.xssf.usermodel包下的类。它实现了Font接口,因此可以处理工作簿中的不同字体。
Sr.No. | Constructor & Description |
---|---|
1 |
XSSFFont() Creates a new XSSFont instance. |
Sr.No. | Method & Description |
---|---|
1 |
setBold(boolean bold) Sets a Boolean value for the ‘bold’ attribute. |
2 |
setColor(short color) Sets the indexed color for the font. |
3 |
setColor(XSSFColor color) Sets the color for the font in Standard Alpha RGB color value. |
4 |
setFontHeight(short height) Sets the font height in points. |
5 |
setFontName(java.lang.String name) Sets the name for the font. |
6 |
setItalic(boolean italic) Sets a Boolean value for the ‘italic’ property. |
对于其余方法,请通过以下链接: https : //poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFFont.html
这是org.apache.poi.xssf.usermodel包下的类。它实现了Hyperlink接口。它用于设置指向电子表格单元格内容的超链接。
此类的字段如下。在此,字段表示使用的超链接的类型。
Sr.No. | Field & Description |
---|---|
1 |
LINK_DOCUMENT Used to link any other document |
2 |
LINK_EMAIL Used to link email |
3 |
LINK_FILE Used to link any other file in any format |
4 |
LINK_URL Used to link a web URL |
Sr.No. | Method & Description |
---|---|
1 |
setAddress(java.lang.String address) Hyperlink address. |
对于其余方法,请访问以下链接: https : //poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFHyperlink.html
这是org.apache.poi.xssf.usermodel包下的类。它实现了CreationHelper接口。它用作公式评估和设置超链接的支持类。
Sr.No. | Method & Description |
---|---|
1 |
createFormulaEvaluator() Creates an XSSFFormulaEvaluator instance, the object that evaluates formula cells. |
2 |
createHyperlink(int type) Creates a new XSSFHyperlink. |
对于其余方法,请参考以下链接: https : //poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFCreationHelper.html
这是org.apache.poi.xsssf.usermodel包下的类。它实现了PrintSetup接口。它用于设置打印页面的大小,区域,选项和设置。
Sr.No. | Method & Description |
---|---|
1 |
setLandscape(boolean ls) Sets a boolean value to allow or block landscape printing. |
2 |
setLeftToRight(boolean ltor) Sets whether to go left to right or top down in ordering while printing. |
3 |
setPaperSize(short size) Sets the paper size. |
对于其余方法,请访问以下链接: https : //poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFPrintSetup.html