Top Description Inners Fields Constructors Methods
org.apache.poi.ss.util

public final Class CellUtil

extends Object
Class Inheritance
Imports
java.util.Arrays, .Collections, .HashMap, .HashSet, .Locale, .Map, .Objects, .Set, org.apache.logging.log4j.LogManager, .Logger, org.apache.poi.common.Duplicatable, org.apache.poi.ss.usermodel.*, org.apache.poi.util.Beta

Various utility functions that make working with a cells and rows easier. The various methods that deal with style's allow you to create your CellStyles as you need them. When you apply a style change to a cell, the code will attempt to see if a style already exists that meets your needs. If not, then it will create a new style. This is to prevent creating too many styles. there is an upper limit in Excel on the number of styles that can be supported.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
public static final String
private static final Set<String>
public static final String
public static final String
public static final String
public static final String
private static final Set<String>
public static final String
private static final Set<String>
public static final String
public static final String
public static final String
public static final String
public static final String
public static final String
public static final String
public static final String
public static final String
private static final Set<String>
public static final String
public static final String
private static final Logger
public static final String
public static final String
public static final String
private static final Set<String>
public static final String
public static final String
private static final CellUtil.UnicodeMapping[]
public static final String
public static final String

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static void
copyCell(Cell
The cell to take value, formula and style from
srcCell
,
Cell
The cell to copy to
destCell
,
CellCopyPolicy
The policy for copying the information, see CellCopyPolicy
policy
,
CellCopyContext
The context for copying, see CellCopyContext
context
)

Copy cell value, formula and style, from srcCell per cell copy policy If srcCell is null, clears the cell value and cell style per cell copy policy.

public static Cell

Returns:

A new Cell
createCell
(Row
the row to create the cell in
row
,
int
the column index to create the cell in
column
,
String
The value of the cell
value
,
CellStyle
If the style is not null, then set
style
)

Creates a cell, gives it a value, and applies a style if provided

public static Cell

Returns:

A new Cell.
createCell
(Row
the row to create the cell in
row
,
int
the column index to create the cell in
column
,
String
The value of the cell
value
)

Create a cell, and give it a value.

private static boolean

Returns:

false if the property does not exist, or is not a Boolean, true otherwise
getBoolean
(Map<String, Object>
map of properties (String -> Object)
properties
,
String
property name
name
)

Utility method that returns the named boolean value from the given map.

private static BorderStyle

Returns:

Border style if set, otherwise BorderStyle#NONE
getBorderStyle
(Map<String, Object>
map of named properties (String -> Object)
properties
,
String
property name
name
)

Utility method that returns the named BorderStyle value from the given map.

public static Cell

Returns:

The cell indicated by the column.
getCell
(Row
The row that the cell is part of
row
,
int
The column index that the cell is in.
columnIndex
)

Get a specific cell from a row.

private static Color

Returns:

null if the property does not exist, or is not a Color otherwise the property value
getColor
(Map<String, Object>
map of named properties (String -> Object)
properties
,
String
property name
name
)

Utility method that returns the named Color value from the given map.

private static FillPatternType

Returns:

FillPatternType style if set, otherwise FillPatternType#NO_FILL
getFillPattern
(Map<String, Object>
map of named properties (String -> Object)
properties
,
String
property name
name
)

Utility method that returns the named FillPatternType value from the given map.

private static Map<String, Object>

Returns:

map of format properties (String -> Object)
getFormatProperties
(CellStyle
cell style
style
)

Returns a map containing the format properties of the given cell style.

private static HorizontalAlignment

Returns:

HorizontalAlignment style if set, otherwise HorizontalAlignment#GENERAL
getHorizontalAlignment
(Map<String, Object>
map of named properties (String -> Object)
properties
,
String
property name
name
)

Utility method that returns the named HorizontalAlignment value from the given map.

private static int

Returns:

zero if the property does not exist, or is not a Integer otherwise the property value
getInt
(Map<String, Object>
map of named properties (String -> Object)
properties
,
String
property name
name
)

Utility method that returns the named int value from the given map.

public static Row

Returns:

The row indicated by the rowCounter
getRow
(int
The 0 based row number
rowIndex
,
Sheet
The sheet that the row is part of.
sheet
)

Get a row from the spreadsheet, and create it if it doesn't exist.

private static short

Returns:

zero if the property does not exist, or is not a Short otherwise the property value
getShort
(Map<String, Object>
map of named properties (String -> Object)
properties
,
String
property name
name
)

Utility method that returns the named short value from the given map.

private static VerticalAlignment

Returns:

VerticalAlignment style if set, otherwise VerticalAlignment#BOTTOM
getVerticalAlignment
(Map<String, Object>
map of named properties (String -> Object)
properties
,
String
property name
name
)

Utility method that returns the named VerticalAlignment value from the given map.

private static Short
nullableShort(Map<String, Object> properties, String name)

private static void
put(Map<String, Object>
map of properties (String -> Object)
properties
,
String
property name
name
,
Object
property value
value
)

Utility method that puts the given value to the given map.

private static void
putAll(final Map<String, Object>
the property map to copy from (read-only)
src
,
Map<String, Object>
the property map to copy into
dest
)

Copies the entries in src to dest, using the preferential data type so that maps can be compared for equality

public static void
setAlignment(Cell
the cell to set the alignment for
cell
,
HorizontalAlignment
the horizontal alignment to use.
align
)

Take a cell, and align it.

public static void
setCellStyleProperties(Cell
The cell to change the style of
cell
,
Map<String, Object>
The properties to be added to a cell style, as {propertyName: propertyValue}.
properties
)

This method attempts to find an existing CellStyle that matches the cell's current style plus styles properties in properties.

private static void
setCellStyleProperties(final Cell cell, final Map<String, Object> properties, final boolean disableNullColorCheck)

public static void
setCellStyleProperty(Cell
The cell that is to be changed.
cell
,
String
The name of the property that is to be changed.
propertyName
,
Object
The value of the property that is to be changed.
propertyValue
)

This method attempts to find an existing CellStyle that matches the cell's current style plus a single style property propertyName with value propertyValue.

public static void
setFont(Cell
the cell to set the alignment for
cell
,
Font
The Font that you want to set.
font
)

Take a cell, and apply a font to it

private static void
setFormatProperties(CellStyle
cell style
style
,
Workbook
parent workbook
workbook
,
Map<String, Object>
map of format properties (String -> Object)
properties
)

Sets the format properties of the given style based on the given map.

public static void
setVerticalAlignment(Cell
the cell to set the alignment for
cell
,
VerticalAlignment
the vertical alignment to use.
align
)

Take a cell, and vertically align it.

private static boolean
styleMapsMatch(final Map<String, Object> newProps, final Map<String, Object> storedProps, final boolean disableNullColorCheck)

public static Cell

Returns:

translated to unicode
translateUnicodeValues
(Cell
The cell to check for unicode values
cell
)

Looks for text in the cell that should be unicode, like α and provides the unicode version of it.

private static CellUtil.UnicodeMapping
um(String entityName, String resolvedValue)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ALIGNMENTback to summary
public static final String ALIGNMENT
booleanValuesback to summary
private static final Set<String> booleanValues
BORDER_BOTTOMback to summary
public static final String BORDER_BOTTOM
BORDER_LEFTback to summary
public static final String BORDER_LEFT
BORDER_RIGHTback to summary
public static final String BORDER_RIGHT
BORDER_TOPback to summary
public static final String BORDER_TOP
borderTypeValuesback to summary
private static final Set<String> borderTypeValues
BOTTOM_BORDER_COLORback to summary
public static final String BOTTOM_BORDER_COLOR
colorValuesback to summary
private static final Set<String> colorValues
DATA_FORMATback to summary
public static final String DATA_FORMAT
FILL_BACKGROUND_COLORback to summary
public static final String FILL_BACKGROUND_COLOR
FILL_BACKGROUND_COLOR_COLORback to summary
public static final String FILL_BACKGROUND_COLOR_COLOR
FILL_FOREGROUND_COLORback to summary
public static final String FILL_FOREGROUND_COLOR
FILL_FOREGROUND_COLOR_COLORback to summary
public static final String FILL_FOREGROUND_COLOR_COLOR
FILL_PATTERNback to summary
public static final String FILL_PATTERN
FONTback to summary
public static final String FONT
HIDDENback to summary
public static final String HIDDEN
INDENTIONback to summary
public static final String INDENTION
intValuesback to summary
private static final Set<String> intValues
LEFT_BORDER_COLORback to summary
public static final String LEFT_BORDER_COLOR
LOCKEDback to summary
public static final String LOCKED
LOGGERback to summary
private static final Logger LOGGER
QUOTE_PREFIXEDback to summary
public static final String QUOTE_PREFIXED
RIGHT_BORDER_COLORback to summary
public static final String RIGHT_BORDER_COLOR
ROTATIONback to summary
public static final String ROTATION
shortValuesback to summary
private static final Set<String> shortValues
SHRINK_TO_FITback to summary
public static final String SHRINK_TO_FIT
TOP_BORDER_COLORback to summary
public static final String TOP_BORDER_COLOR
unicodeMappingsback to summary
private static final CellUtil.UnicodeMapping[] unicodeMappings
VERTICAL_ALIGNMENTback to summary
public static final String VERTICAL_ALIGNMENT
WRAP_TEXTback to summary
public static final String WRAP_TEXT

Constructor Detail

CellUtilback to summary
private CellUtil()

Method Detail

copyCellback to summary
public static void copyCell(Cell srcCell, Cell destCell, CellCopyPolicy policy, CellCopyContext context)

Copy cell value, formula and style, from srcCell per cell copy policy If srcCell is null, clears the cell value and cell style per cell copy policy. Note that if you are copying from a source cell from a different type of then you may need to disable style copying in the CellCopyPolicy (HSSF styles are not compatible with XSSF styles, for instance). This does not shift references in formulas. The copyRowFrom method on XSSFRow and HSSFRow does attempt to shift references in formulas.

Parameters
srcCell:Cell

The cell to take value, formula and style from

destCell:Cell

The cell to copy to

policy:CellCopyPolicy

The policy for copying the information, see CellCopyPolicy

context:CellCopyContext

The context for copying, see CellCopyContext

Annotations
@Beta
Exceptions
IllegalArgumentException:
if copy cell style and srcCell is from a different workbook
IllegalStateException:
if srcCell hyperlink is not an instance of Duplicatable
Since
POI 5.2.0
createCellback to summary
public static Cell createCell(Row row, int column, String value, CellStyle style)

Creates a cell, gives it a value, and applies a style if provided

Parameters
row:Row

the row to create the cell in

column:int

the column index to create the cell in

value:String

The value of the cell

style:CellStyle

If the style is not null, then set

Returns:Cell

A new Cell

createCellback to summary
public static Cell createCell(Row row, int column, String value)

Create a cell, and give it a value.

Parameters
row:Row

the row to create the cell in

column:int

the column index to create the cell in

value:String

The value of the cell

Returns:Cell

A new Cell.

getBooleanback to summary
private static boolean getBoolean(Map<String, Object> properties, String name)

Utility method that returns the named boolean value from the given map.

Parameters
properties:Map<String, Object>

map of properties (String -> Object)

name:String

property name

Returns:boolean

false if the property does not exist, or is not a Boolean, true otherwise

getBorderStyleback to summary
private static BorderStyle getBorderStyle(Map<String, Object> properties, String name)

Utility method that returns the named BorderStyle value from the given map.

Parameters
properties:Map<String, Object>

map of named properties (String -> Object)

name:String

property name

Returns:BorderStyle

Border style if set, otherwise BorderStyle#NONE

getCellback to summary
public static Cell getCell(Row row, int columnIndex)

Get a specific cell from a row. If the cell doesn't exist, then create it.

Parameters
row:Row

The row that the cell is part of

columnIndex:int

The column index that the cell is in.

Returns:Cell

The cell indicated by the column.

getColorback to summary
private static Color getColor(Map<String, Object> properties, String name)

Utility method that returns the named Color value from the given map.

Parameters
properties:Map<String, Object>

map of named properties (String -> Object)

name:String

property name

Returns:Color

null if the property does not exist, or is not a Color otherwise the property value

getFillPatternback to summary
private static FillPatternType getFillPattern(Map<String, Object> properties, String name)

Utility method that returns the named FillPatternType value from the given map.

Parameters
properties:Map<String, Object>

map of named properties (String -> Object)

name:String

property name

Returns:FillPatternType

FillPatternType style if set, otherwise FillPatternType#NO_FILL

Since
POI 3.15 beta 3
getFormatPropertiesback to summary
private static Map<String, Object> getFormatProperties(CellStyle style)

Returns a map containing the format properties of the given cell style. The returned map is not tied to style, so subsequent changes to style will not modify the map, and changes to the returned map will not modify the cell style. The returned map is mutable.

Parameters
style:CellStyle

cell style

Returns:Map<String, Object>

map of format properties (String -> Object)

See Also
setFormatProperties(CellStyle, Workbook, Map)
getHorizontalAlignmentback to summary
private static HorizontalAlignment getHorizontalAlignment(Map<String, Object> properties, String name)

Utility method that returns the named HorizontalAlignment value from the given map.

Parameters
properties:Map<String, Object>

map of named properties (String -> Object)

name:String

property name

Returns:HorizontalAlignment

HorizontalAlignment style if set, otherwise HorizontalAlignment#GENERAL

Since
POI 3.15 beta 3
getIntback to summary
private static int getInt(Map<String, Object> properties, String name)

Utility method that returns the named int value from the given map.

Parameters
properties:Map<String, Object>

map of named properties (String -> Object)

name:String

property name

Returns:int

zero if the property does not exist, or is not a Integer otherwise the property value

getRowback to summary
public static Row getRow(int rowIndex, Sheet sheet)

Get a row from the spreadsheet, and create it if it doesn't exist.

Parameters
rowIndex:int

The 0 based row number

sheet:Sheet

The sheet that the row is part of.

Returns:Row

The row indicated by the rowCounter

getShortback to summary
private static short getShort(Map<String, Object> properties, String name)

Utility method that returns the named short value from the given map.

Parameters
properties:Map<String, Object>

map of named properties (String -> Object)

name:String

property name

Returns:short

zero if the property does not exist, or is not a Short otherwise the property value

getVerticalAlignmentback to summary
private static VerticalAlignment getVerticalAlignment(Map<String, Object> properties, String name)

Utility method that returns the named VerticalAlignment value from the given map.

Parameters
properties:Map<String, Object>

map of named properties (String -> Object)

name:String

property name

Returns:VerticalAlignment

VerticalAlignment style if set, otherwise VerticalAlignment#BOTTOM

Since
POI 3.15 beta 3
nullableShortback to summary
private static Short nullableShort(Map<String, Object> properties, String name)
putback to summary
private static void put(Map<String, Object> properties, String name, Object value)

Utility method that puts the given value to the given map.

Parameters
properties:Map<String, Object>

map of properties (String -> Object)

name:String

property name

value:Object

property value

putAllback to summary
private static void putAll(final Map<String, Object> src, Map<String, Object> dest)

Copies the entries in src to dest, using the preferential data type so that maps can be compared for equality

Parameters
src:Map<String, Object>

the property map to copy from (read-only)

dest:Map<String, Object>

the property map to copy into

Since
POI 3.15 beta 3
setAlignmentback to summary
public static void setAlignment(Cell cell, HorizontalAlignment align)

Take a cell, and align it. This is superior to cell.getCellStyle().setAlignment(align) because this method will not modify the CellStyle object that may be referenced by multiple cells. Instead, this method will search for existing CellStyles that match the desired CellStyle, creating a new CellStyle with the desired style if no match exists.

Parameters
cell:Cell

the cell to set the alignment for

align:HorizontalAlignment

the horizontal alignment to use.

Since
POI 3.15 beta 3
See Also
{@link HorizontalAlignment} for alignment options
setCellStylePropertiesback to summary
public static void setCellStyleProperties(Cell cell, Map<String, Object> properties)

This method attempts to find an existing CellStyle that matches the cell's current style plus styles properties in properties. A new style is created if the workbook does not contain a matching style.

Modifies the cell style of cell without affecting other cells that use the same style.

This is necessary because Excel has an upper limit on the number of styles that it supports.

This function is more efficient than multiple calls to setCellStyleProperty(Cell, String, Object) if adding multiple cell styles.

For performance reasons, if this is the only cell in a workbook that uses a cell style, this method does NOT remove the old style from the workbook.

Parameters
cell:Cell

The cell to change the style of

properties:Map<String, Object>

The properties to be added to a cell style, as {propertyName: propertyValue}.

Since
POI 3.14 beta 2
setCellStylePropertiesback to summary
private static void setCellStyleProperties(final Cell cell, final Map<String, Object> properties, final boolean disableNullColorCheck)
setCellStylePropertyback to summary
public static void setCellStyleProperty(Cell cell, String propertyName, Object propertyValue)

This method attempts to find an existing CellStyle that matches the cell's current style plus a single style property propertyName with value propertyValue. A new style is created if the workbook does not contain a matching style.

Modifies the cell style of cell without affecting other cells that use the same style.

If setting more than one cell style property on a cell, use setCellStyleProperties(Cell, Map), which is faster and does not add unnecessary intermediate CellStyles to the workbook.

Parameters
cell:Cell

The cell that is to be changed.

propertyName:String

The name of the property that is to be changed.

propertyValue:Object

The value of the property that is to be changed.

setFontback to summary
public static void setFont(Cell cell, Font font)

Take a cell, and apply a font to it

Parameters
cell:Cell

the cell to set the alignment for

font:Font

The Font that you want to set.

Exceptions
IllegalArgumentException:
if font and cell do not belong to the same workbook
setFormatPropertiesback to summary
private static void setFormatProperties(CellStyle style, Workbook workbook, Map<String, Object> properties)

Sets the format properties of the given style based on the given map.

Parameters
style:CellStyle

cell style

workbook:Workbook

parent workbook

properties:Map<String, Object>

map of format properties (String -> Object)

See Also
getFormatProperties(CellStyle)
setVerticalAlignmentback to summary
public static void setVerticalAlignment(Cell cell, VerticalAlignment align)

Take a cell, and vertically align it. This is superior to cell.getCellStyle().setVerticalAlignment(align) because this method will not modify the CellStyle object that may be referenced by multiple cells. Instead, this method will search for existing CellStyles that match the desired CellStyle, creating a new CellStyle with the desired style if no match exists.

Parameters
cell:Cell

the cell to set the alignment for

align:VerticalAlignment

the vertical alignment to use.

Since
POI 3.15 beta 3
See Also
{@link VerticalAlignment} for alignment options
styleMapsMatchback to summary
private static boolean styleMapsMatch(final Map<String, Object> newProps, final Map<String, Object> storedProps, final boolean disableNullColorCheck)
translateUnicodeValuesback to summary
public static Cell translateUnicodeValues(Cell cell)

Looks for text in the cell that should be unicode, like α and provides the unicode version of it.

Parameters
cell:Cell

The cell to check for unicode values

Returns:Cell

translated to unicode

umback to summary
private static CellUtil.UnicodeMapping um(String entityName, String resolvedValue)
org.apache.poi.ss.util back to summary

private final Class CellUtil.UnicodeMapping

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
public final String
public final String

Constructor Summary

AccessConstructor and Description
public
UnicodeMapping(String pEntityName, String pResolvedValue)

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

entityNameback to summary
public final String entityName
resolvedValueback to summary
public final String resolvedValue

Constructor Detail

UnicodeMappingback to summary
public UnicodeMapping(String pEntityName, String pResolvedValue)