Apache Poi-java Excel APIs

Source: Internet
Author: User
Tags border color

Document Source: https://www.yiibai.com/apache_poi/

Poi what is Apache POI?

Apache POI is a popular API that allows programmers to create, modify, and display MS Office files using Java programs. This is an open source library developed by the Apache Software Foundation that uses Java distributed design or modifies Microsoft Office files. It contains classes and methods to decode user input data or files into MS Office documents.

Apache POI API, it is essential to work with Java programs to manipulate Excel files There are several classes and methods.

Workbooks

This is a hyper-interface for all classes that create or maintain an Excel workbook. It belongs to the Org.apache.poi.ss.usermodel package. Is the two classes that implement this interface, as follows:

    • Hssfworkbook: This class has read and. xls formats and methods for writing to Microsoft Excel files. It is compatible with the Microsoft office97-2003 version.

    • Xssfworkbook: This class has a format for reading and writing XML files for Microsoft Excel and OpenOffice. xls or. xlsx methods. It is compatible with Ms-office version 2007 or later.

Hssfworkbook

It is a high-level class in the Org.apache.poi.hssf.usermodel package. It implements the workbook interface for the. xls format in an Excel file. Listed below are some methods and constructors under this class.

Constructors for classes
s.no. Constructors and descriptions
1

Hssfworkbook ()

When you create a new Hssfworkbook object from the beginning.

2

Hssfworkbook (Directorynode directory, Boolean preservenodes)

Creates a new Hssfwworkbook object in a specific directory.

3

Hssfworkbook (Directorynode directory, Poifsfilesystem FS, Boolean preservenodes)

Given a Poifsfilesystem object and a specific directory, it creates a Ssfworkbook object that reads the specified workbook.

4

Hssfworkbook (Java.io.InputStream s)

When you create a new Hssfworkbook object that uses the input stream.

5

Hssfworkbook (Java.io.InputStream S, boolean preservenodes)

Build the Poi file system on the input stream.

6

Hssfworkbook (Poifsfilesystem FS)

When you use a Poifsfilesystem object to construct a new Hssfworkbook object.

7

Hssfworkbook (Poifsfilesystem FS, Boolean preservenodes)

When given a Poifsfilesystem object, it creates a new Hssfworkbook object when the specified workbook is read.

Common parameters within these constructs are:

    • Directory: This is the catalog that is processed from the POI file system.

    • FS: It is a file system that contains a book stream for that poi.

    • Preservenodes: This is an optional parameter that determines whether to keep other nodes like macros. It consumes a lot of memory, because it stores all the Poifilesystem in memory (if set).

Note: The Hssfworkbook class contains many methods, however, they are only compatible with the XLS format. In this tutorial, the focus is on the latest version of the Excel file format. Therefore, the methods of the Hssfworkbook class are not listed here. If you need the methods of these classes, then refer to the Poi-hssfworkbook Class API in https://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFWorkbook.html.

Xssfworkbook

It is a class used to represent high and low-level Excel file formats. It belongs to the Org.apache.xssf.usemodel package and implements the workbook interface. The methods and constructors of this class are listed below.

Constructors for classes
s.no. Constructors and descriptions
1

Xssfworkbook ()

Create a new Xssfworkbook object from the beginning.

2

Xssfworkbook (Java.io.File File)

Constructs a Xssfworkbook object from a given file.

3

Xssfworkbook (Java.io.InputStream is)

Constructs a Xssfworkbook object that buffers the entire input stream into memory and then opens a Opcpackage object for it.

4

Xssfworkbook (java.lang.String Path)

A Xssfworkbook object that builds the full path of a given file.

Class method
s.no. methods and descriptions
1

createsheet ()

Create a Xssfsheet This workbook, add it to the table, and return to the high-level representation.

2

createsheet (java.lang.String sheetname)

Create a new table for this workbook. and returns a high-level representation.

3

createfont ()

Create a new font and add it to the workbook's font table.

4

createcellstyle ()

Create a new xssfcellstyle and add it to the workbook's style sheet 。

5

createfont ()

Create a new font and add 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 table according to the specified parameters.

For the remaining methods of this class, see the Full API documentation: Http://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFWorkbook.html. All methods are listed.

Sheet

Sheet is the interface in the Org.apache.poi.ss.usermodel package, which is a hyper-interface that creates all classes of a high or low-level spreadsheet with a specific name. The most common type of spreadsheet is a worksheet, which is represented as a grid of cells.

Hssfsheet

This is the class in the Org.apache.poi.hssf.usermodel package. It can create Excel spreadsheets, which allow in sheet and table data formats.

Constructors for classes
s.no. Constructors and descriptions
1

Hssfsheet (Hssfworkbook Workbook)

Create a new hssfsheet create a table from scratch by calling Hssfworkbook.

2

Hssfsheet (Hssfworkbook workbook, Internalsheet sheet)

Creates a hssfsheet representation of a given Table object.

Xssfsheet

This is a category that represents a high-level representation of Excel spreadsheets. This is under the Org.apache.poi.hssf.usermodel package.

Constructors for classes
s.no. Constructors and descriptions
1

Xssfsheet ()

Create a new xssfsheet-call Xssfworkbook create a table from scratch.

2

Xssfsheet (PackagePart part, PackageRelationship rel)

Creates a xssfsheet that represents a part and a relationship for a given package.

Class method
s.no. Methods and Descriptions
1

Addmergedregion (cellrangeaddress region)

Add the merged area of the cell (so the cells merge to form one).

2

Autosizecolumn (int column)

Adjusts the width of the column to fit the content.

3

Iterator ()

This method is used to alias the Rowiterator () to allow the Foreach loop

4

AddHyperlink (xssfhyperlink hyperlink)

Register hyperlinks in the collection of hyperlinks on this work table

For the remaining methods of this class, see the full API in:
Https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFSheet.html.

Yes

This is the interface in the Org.apache.poi.ss.usermodel package. It is a high-level representation of the spreadsheet used in a row. It is a hyper-interface for all classes of rows representing the POI library.

Xssfrow

This is the class in the Org.apache.poi.xssf.usermodel package. It implements the row interface, so it can create rows in a spreadsheet. The following is a list of the methods and constructors in this class.

Class method
s.no. Description
1

Createcell (int columnindex)

Creates a new cell row and returns.

2

SetHeight (short height)

Sets the height of a short unit.

For the remaining methods of this class, refer to the following link:
Https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFRow.html

Cell

This is the interface in the Org.apache.poi.ss.usermodel package. It is a hyper-interface that represents all the classes in a row of cells in a spreadsheet.

Cells can use a variety of properties, such as white space, numbers, dates, errors, and so on, before a cell is added to a line that should have its own number (based on 0).

Xssfcell

This is the class in the Org.apache.poi.xssf.usermodel package. It implements the cell interface. It is a high-level representation of a unit's rows in a spreadsheet.

Field Summary

Listed below are some of the fields of the Xssfcell class and their descriptions.

cell Type Description
Cell_type_blank Represents blank cells
Cell_type_boolean Represents a Boolean unit (TRUE or FALSE)
Cell_type_error Indicates the error value in the cell
Cell_type_formula Represents the result of a cell formula
Cell_type_numeric Represents the numeric data for a cell
Cell_type_string Represents a cell string (text)
Class method
s.no. Description
1

setcellstyle (cellstyle style)

Set the style for the cell.

2

setcelltype (int celltype)

Sets the type of cell (number, formula, or string).

3

setcellvalue (boolean value)

Set cell a Boolean

4

setcellvalue (java.util.Calendar value)

Set a cell for a date value.

5

setcellvalue (double value)

is set to the numeric value of the cell.

6

setcellvalue (java.lang.String str)

Set as a string value for the cell.

7

sethyperlink (Hyperlink Hyperlink)

Assign hyperlink to this cell.

For the remaining methods and fields of this class, please visit the following links for details:
Https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFCell.html

Xssfcellstyle

This is the class in the Org.apache.poi.xssf.usermodel package. It will provide information about the format of the contents of the cell in the spreadsheet. It also provides options for correcting the format. It implements the CellStyle interface.

Field Summary

The following table lists some of the fields inherited from the CellStyle interface.

Field name Field Description
Align_center Center Align Cell contents
Align_center_selection Center Select horizontal alignment
Align_fill Cells fit to the size of the content
Align_justify Fit the width of the cell contents
Align_left Align cell contents left
Align_right Right-align cell contents
Border_dash_dot Use dashes and dot cell styles
border_dotted Cell style with dashed border
Border_dashed Cell style with dashed border
Border_thick Thick border cell style
Border_thin Cell style for thin borders
Vertical_bottom Align the contents of the cell vertically below
Vertical_center Align cell contents to center vertically
Vertical_justify Aligned and vertically aligned cell contents
Vertical_top Align top to vertical
Constructors for classes
s.no. Constructors and descriptions
1

Xssfcellstyle (int cellxfid, int cellstylexfid, stylestable stylessource, themestable theme)

Creates a cell style from the provided part

2

Xssfcellstyle (stylestable Stylessource)

Create an empty cell style

Class method

Sets the type of border as the bottom boundary of the cell

s.no Method and Description
1

Setalignment (short align)

Set the type of cell to be horizontally aligned

2

Setborderbottom (short border)

3

setBorderColor (xssfcellborder.borderside side, Xssfcolor color)

Selected border color

4

Setborderleft (short border)

Sets the border of the type cell to the left of the box

5

Setborderright (short border)

Set the border's type to the right edge of the cell

6

Setbordertop (short border)

Sets the bounds of the type of the cell top border

7

Setfillbackgroundcolor (xssfcolor color)

Sets the color that is represented as the Xssfcolor value background fill.

8

Setfillforegroundcolor (xssfcolor color)

Sets the value of the foreground fill color that is represented as Xssfcolor.

9

Setfillpattern (short FP)

Specifies the cell's fill information mode and the solid color fill cell.

10

SetFont (font font)

Sets the font for this style.

11

Setrotation (short rotation)

Sets the degree to which the text in the cell is rotated.

12

Setverticalalignment (short align)

Sets the cell type to vertical orientation.

For the remaining methods and fields of this class, use the following link:
Https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFCellStyle.html

Hssfcolor

This is the class in the Org.apache.poi.hssf.util package. It provides a different color as a nested class. Typically, these nested classes are represented using their own indexes. It implements the color interface.

Nested classes

All nested classes This class is static, and each class has its index. These nested color classes are used for cell formatting, such as cell contents, borders, foreground, and background. Some of the nested classes are listed below.

s.no. class name (color)
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
Class method

Only one method of this class is important and is used to get the index value.

s.no. Methods and Descriptions
1

GetIndex ()

This method is used to obtain the index value of a nested class

For the remaining methods and nested classes, see the following links:
Https://poi.apache.org/apidocs/org/apache/poi/hssf/util/HSSFColor.html.

Xssfcolor

This is the class in the Org.apache.poi.xssf.usermodel package. It is used to represent the color in the spreadsheet. It implements the interface of the color. The following is a list of some of its methods and constructors.

Constructors for classes
s.no. Constructor and Description
1

Xssfcolor ()

Creates a new instance of the Xssfcolor.

2

Xssfcolor (byte[] RGB)

Create a new instance of Xssfcolor using RGB.

3

Xssfcolor (Java.awt.Color CLR)

Create a new instance of Xssfcolor using the color class from the AWT package.

Class method
s.no. Methods and Descriptions
1

Setauto (Boolean Auto)

Sets a Boolean value that indicates that Ctcolor is automatic and the system Ctcolor dependent.

2

setindexed (int indexed)

Sets the index Ctcolor value system Ctcolor.

For the rest of the methods, please visit the following links:
Https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFColor.html.

Xssffont

This is the class in the Org.apache.poi.xssf.usermodel package. It implements the font interface, so it can handle different fonts in the workbook.

Constructors for classes
s.no. Constructors and descriptions
1

Xssffont ()

Creates a new Xssfont instance.

Class method
s.no. Methods and Descriptions
1

Setbold (Boolean bold)

Sets the Boolean value of the "bold" property.

2

SetColor (short color)

Sets the font for the indexed color.

3

SetColor (xssfcolor color)

Sets the font color to the standard alpha RGB color value.

4

Setfontheight (short height)

Sets the height of the font at the point.

5

Setfontname (java.lang.String name)

Sets the name of the font.

6

Setitalic (Boolean italic)

Sets a Boolean value for the "Italic" property.

For the rest of the methods, use the following link:
Https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFFont.html.

Xssfhyperlink

This is the class in the Org.apache.poi.xssf.usermodel package. It implements the hyperlink interface. It is used to link the contents of a cell set to a spreadsheet.

Field

The fields that belong to this class are as follows. In this case, the field means the type of the hyperlink being used.

Field Description
Link_document Used to connect to any other file
Link_email e-mail for linking
Link_file Used to link any other file in any format
Link_url Used to connect a Web page URL
Class method
s.no. Method and Description
1

Setaddress (java.lang.String address)

The hyperlink address.

For the rest of the methods, please visit the following links:
Https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFHyperlink.html

Xssfcreationhelper

This is the class in the Org.apache.poi.xssf.usermodel package. It implements the Creationhelper interface. It is used as a formula to evaluate and set hypertext link support classes.

Class method
s.no. Methods and Descriptions
1

Createformulaevaluator ()

Create a Xssfformulaevaluator For example, the result evaluates the formula for the Cell object.

2

Createhyperlink (int type)

Creates a new xssfhyperlink.

For the rest of the methods, refer to the following link: https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFCreationHelper.html.

Xssfprintsetup

This is the class under the Org.apache.poi.xsssf.usermodel package. It implements the Printsetup interface. It is used to set the print page size, area, options and settings.

Class method
s.no. Method and Description
1

Setlandscape (Boolean ls)

Sets a Boolean value that allows or prevents horizontal printing.

2

Setlefttoright (Boolean Ltor)

Sets whether to move left to the right or top-down order while printing.

3

Setpapersize (short size)

Set the paper size.

For the rest of the methods, please visit the following link: https://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFPrintSetup.html

Apache Poi-java Excel APIs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.