Top Description Methods
org.apache.poi.ss.usermodel

public Interface Cell

Known Direct Implementers
org.apache.poi.ss.usermodel.CellBase
Imports
java.time.LocalDate, .LocalDateTime, java.util.Calendar, .Date, .Map, org.apache.poi.ss.formula.FormulaParseException, org.apache.poi.ss.util.CellAddress, .CellRangeAddress, org.apache.poi.util.Removal

High level representation of a cell in a row of a spreadsheet.

Cells can be numeric, formula-based or string-based (text). The cell type specifies this. String cells cannot conatin numbers and numeric cells cannot contain strings (at least according to our model). Client apps should do the conversions themselves. Formula cells have the formula string, as well as the formula result, which can be numeric or string.

Cells should have their number (0 based) before being added to a row.

Method Summary

Modifier and TypeMethod and Description
public CellAddress

Returns:

A1 style address of this cell
getAddress
()

Gets the address of this cell

public CellRangeAddress

Returns:

range of the array formula group that the cell belongs to.
getArrayFormulaRange
()

Only valid for array formula cells

public boolean

Returns:

the value of the cell as a boolean
getBooleanCellValue
()

Get the value of the cell as a boolean.

public CellType

Returns:

one of (CellType#NUMERIC, CellType#STRING, CellType#BOOLEAN, CellType#ERROR) depending on the cached value of the formula
getCachedFormulaResultType
()

Only valid for formula cells

public Comment

Returns:

comment associated with this cell or null if not found
getCellComment
()

Returns comment associated with this cell

public String

Returns:

a formula for the cell
getCellFormula
()

Return a formula for the cell, for example, SUM(C4:E4)

public CellStyle

Returns:

the cell's style. Always not-null. Default cell style has zero index and can be obtained as workbook.getCellStyleAt(0)
getCellStyle
()

Return the cell's style.

public CellType

Returns:

the cell type
getCellType
()

Return the cell type.

public int

Returns:

zero-based column index of a column in a sheet.
getColumnIndex
()

Returns column index of this cell

public Date

Returns:

the value of the cell as a date
getDateCellValue
()

Get the value of the cell as a date.

public byte

Returns:

the value of the cell as an error code
getErrorCellValue
()

Get the value of the cell as an error code.

public LocalDateTime

Returns:

the value of the cell as a LocalDateTime
getLocalDateTimeCellValue
()

Get the value of the cell as a LocalDateTime.

public double

Returns:

the value of the cell as a number
getNumericCellValue
()

Get the value of the cell as a number.

public RichTextString

Returns:

the value of the cell as a XSSFRichTextString
getRichStringCellValue
()

Get the value of the cell as a XSSFRichTextString

For numeric cells we throw an exception.

public Row

Returns:

the Row that owns this cell
getRow
()

Returns the Row this cell belongs to

public int

Returns:

zero-based row index of a row in the sheet that contains this cell
getRowIndex
()

Returns row index of a row in the sheet that contains this cell

public Sheet

Returns:

the sheet this cell belongs to
getSheet
()

Returns the sheet this cell belongs to

public String

Returns:

the value of the cell as a string
getStringCellValue
()

Get the value of the cell as a string

For numeric cells we throw an exception.

public boolean

Returns:

true if this cell is part of group of cells having a common array formula.
isPartOfArrayFormulaGroup
()

public void
removeCellComment()

Removes the comment for this cell, if there is one.

public void
removeFormula()

Removes formula, if any.

public void
setAsActiveCell()

Sets this cell as the active cell for the worksheet

public void
setBlank()

Removes formula and value from the cell, and sets its type to CellType#BLANK.

public void
setCellComment(Comment
comment associated with this cell
comment
)

Assign a comment to this cell

public void
setCellErrorValue(byte
the error value to set this cell to. For formulas, we'll set the precalculated value , for errors we'll set its value. For other types, we will change the cell to an error cell and set its value.
value
)

Set an error value for the cell

public void
setCellFormula(String
the formula to set, e.g. "SUM(C4:E4)". If the argument is null then the current formula is removed.
formula
)

Sets formula for this cell.

public void
setCellStyle(CellStyle
reference contained in the workbook. If the value is null then the style information is removed causing the cell to used the default workbook style.
style
)

Set the style for the cell.

public void
setCellType(CellType cellType)
Deprecated This method is deprecated and will be removed in POI 5.0.

Set the cells type (blank, numeric, boolean, error or string).

public void
setCellValue(double
the numeric value to set this cell to. For formulas, we'll set the precalculated value, for numerics we'll set its value. For other types, we will change the cell to a numeric cell and set its value.
value
)

Set a numeric value for the cell.

public void
setCellValue(Date
the numeric value to set this cell to. For formulas, we'll set the precalculated value, for numerics we'll set its value. For other types, we will change the cell to a numerics cell and set its value.
value
)

Converts the supplied date to its equivalent Excel numeric value and sets that into the cell.

public void
setCellValue(LocalDateTime
the numeric value to set this cell to. For formulas, we'll set the precalculated value, for numerics we'll set its value. For other types, we will change the cell to a numerics cell and set its value.
value
)

Converts the supplied date to its equivalent Excel numeric value and sets that into the cell.

public default void
setCellValue(LocalDate
the numeric value to set this cell to. For formulas, we'll set the precalculated value, for numerics we'll set its value. For other types, we will change the cell to a numerics cell and set its value.
value
)

Converts the supplied date to its equivalent Excel numeric value and sets that into the cell.

public void
setCellValue(Calendar
the date value to set this cell to. For formulas, we'll set the precalculated value, for numerics we'll set its value. For other types, we will change the cell to a numeric cell and set its value.
value
)

Set a date value for the cell.

public void
setCellValue(RichTextString
value to set the cell to. For formulas, we'll set the formula string, for String cells we'll set its value. For other types, we will change the cell to a string cell and set its value. If value is null then we will change the cell to a Blank cell.
value
)

Set a rich string value for the cell.

public void
setCellValue(String
value to set the cell to. For formulas, we'll set the formula string, for String cells we'll set its value. For other types, we will change the cell to a string cell and set its value. If value is null then we will change the cell to a Blank cell.
value
)

Set a string value for the cell.

public void
setCellValue(boolean
the boolean value to set this cell to. For formulas, we'll set the precalculated value, for booleans we'll set its value. For other types, we will change the cell to a boolean cell and set its value.
value
)

Set a boolean value for the cell

Method Detail

getAddressback to summary
public CellAddress getAddress()

Gets the address of this cell

Returns:CellAddress

A1 style address of this cell

Since
3.14beta1
getArrayFormulaRangeback to summary
public CellRangeAddress getArrayFormulaRange()

Only valid for array formula cells

Returns:CellRangeAddress

range of the array formula group that the cell belongs to.

getBooleanCellValueback to summary
public boolean getBooleanCellValue()

Get the value of the cell as a boolean.

For strings, numbers, and errors, we throw an exception. For blank cells we return a false.

Returns:boolean

the value of the cell as a boolean

Exceptions
IllegalStateException:
if the cell type returned by getCellType() is not CellType#BOOLEAN, CellType#BLANK or CellType#FORMULA
getCachedFormulaResultTypeback to summary
public CellType getCachedFormulaResultType()

Only valid for formula cells

Returns:CellType

one of (CellType#NUMERIC, CellType#STRING, CellType#BOOLEAN, CellType#ERROR) depending on the cached value of the formula

getCellCommentback to summary
public Comment getCellComment()

Returns comment associated with this cell

Returns:Comment

comment associated with this cell or null if not found

getCellFormulaback to summary
public String getCellFormula()

Return a formula for the cell, for example, SUM(C4:E4)

Returns:String

a formula for the cell

Exceptions
IllegalStateException:
if the cell type returned by getCellType() is not CellType#FORMULA
getCellStyleback to summary
public CellStyle getCellStyle()

Return the cell's style.

Returns:CellStyle

the cell's style. Always not-null. Default cell style has zero index and can be obtained as workbook.getCellStyleAt(0)

See Also
Workbook#getCellStyleAt(int)
getCellTypeback to summary
public CellType getCellType()

Return the cell type.

Returns:CellType

the cell type

getColumnIndexback to summary
public int getColumnIndex()

Returns column index of this cell

Returns:int

zero-based column index of a column in a sheet.

getDateCellValueback to summary
public Date getDateCellValue()

Get the value of the cell as a date.

For strings we throw an exception. For blank cells we return a null.

Returns:Date

the value of the cell as a date

Exceptions
IllegalStateException:
if the cell type returned by getCellType() is CellType#STRING
NumberFormatException:
if the cell value isn't a parsable double.
See Also
{@link DataFormatter} for formatting this date into a string similar to how excel does.
getErrorCellValueback to summary
public byte getErrorCellValue()

Get the value of the cell as an error code.

For strings, numbers, and booleans, we throw an exception. For blank cells we return a 0.

Returns:byte

the value of the cell as an error code

Exceptions
IllegalStateException:
if the cell type returned by getCellType() isn't CellType#ERROR
See Also
{@link FormulaError} for error codes
getLocalDateTimeCellValueback to summary
public LocalDateTime getLocalDateTimeCellValue()

Get the value of the cell as a LocalDateTime.

For strings we throw an exception. For blank cells we return a null.

Returns:LocalDateTime

the value of the cell as a LocalDateTime

Exceptions
IllegalStateException:
if the cell type returned by getCellType() is CellType#STRING
NumberFormatException:
if the cell value isn't a parsable double.
See Also
{@link DataFormatter} for formatting this date into a string similar to how excel does.
getNumericCellValueback to summary
public double getNumericCellValue()

Get the value of the cell as a number.

For strings we throw an exception. For blank cells we return a 0. For formulas or error cells we return the precalculated value;

Returns:double

the value of the cell as a number

Exceptions
IllegalStateException:
if the cell type returned by getCellType() is CellType#STRING
NumberFormatException:
if the cell value isn't a parsable double.
See Also
{@link DataFormatter} for turning this number into a string similar to that which Excel would render this number as.
getRichStringCellValueback to summary
public RichTextString getRichStringCellValue()

Get the value of the cell as a XSSFRichTextString

For numeric cells we throw an exception. For blank cells we return an empty string. For formula cells we return the pre-calculated value if a string, otherwise an exception.

Returns:RichTextString

the value of the cell as a XSSFRichTextString

getRowback to summary
public Row getRow()

Returns the Row this cell belongs to

Returns:Row

the Row that owns this cell

getRowIndexback to summary
public int getRowIndex()

Returns row index of a row in the sheet that contains this cell

Returns:int

zero-based row index of a row in the sheet that contains this cell

getSheetback to summary
public Sheet getSheet()

Returns the sheet this cell belongs to

Returns:Sheet

the sheet this cell belongs to

getStringCellValueback to summary
public String getStringCellValue()

Get the value of the cell as a string

For numeric cells we throw an exception. For blank cells we return an empty string. For formulaCells that are not string Formulas, we throw an exception.

Returns:String

the value of the cell as a string

isPartOfArrayFormulaGroupback to summary
public boolean isPartOfArrayFormulaGroup()
Returns:boolean

true if this cell is part of group of cells having a common array formula.

removeCellCommentback to summary
public void removeCellComment()

Removes the comment for this cell, if there is one.

removeFormulaback to summary
public void removeFormula() throws IllegalStateException

Removes formula, if any. If cell was blank, leaves it as is. If it is a part of an array formula group, blanks the cell. If has a regular formula, removes the formula preserving the "cached" value.

Exceptions
IllegalStateException:
if cell is a part of an array formula group containing other cells
setAsActiveCellback to summary
public void setAsActiveCell()

Sets this cell as the active cell for the worksheet

setBlankback to summary
public void setBlank()

Removes formula and value from the cell, and sets its type to CellType#BLANK. Preserves comments and hyperlinks. While setCellType(CellType) exists, is an alias for setCellType(CellType.BLANK).

setCellCommentback to summary
public void setCellComment(Comment comment)

Assign a comment to this cell

Parameters
comment:Comment

comment associated with this cell

setCellErrorValueback to summary
public void setCellErrorValue(byte value)

Set an error value for the cell

Parameters
value:byte

the error value to set this cell to. For formulas, we'll set the precalculated value , for errors we'll set its value. For other types, we will change the cell to an error cell and set its value.

See Also
FormulaError
setCellFormulaback to summary
public void setCellFormula(String formula) throws FormulaParseException, IllegalStateException

Sets formula for this cell.

If formula is not null, sets or updates the formula. If formula is null, removes the formula. Or use removeFormula() to remove the formula.

Note, this method only sets the formula string and does not calculate the formula value. To set the precalculated value use setCellValue.

If the cell was blank, sets value to 0. Otherwise, preserves the value as precalculated.

In XSSF implementation, there is a XSSFWorkbook.setCellFormulaValidation(boolean) setting that can validate (but also in some cases, reformat) the formula.

Parameters
formula:String

the formula to set, e.g. "SUM(C4:E4)". If the argument is null then the current formula is removed.

Exceptions
FormulaParseException:
if the formula has incorrect syntax or is otherwise invalid
IllegalStateException:
if this cell is a part of an array formula group containing other cells
See Also
Cell#removeFormula, Workbook#setForceFormulaRecalculation(boolean)
setCellStyleback to summary
public void setCellStyle(CellStyle style)

Set the style for the cell. The style should be an CellStyle created/retrieved from the Workbook.

To change the style of a cell without affecting other cells that use the same style, use org.apache.poi.ss.util.CellUtil#setCellStyleProperties(Cell, Map)

Parameters
style:CellStyle

reference contained in the workbook. If the value is null then the style information is removed causing the cell to used the default workbook style.

See Also
org.apache.poi.ss.usermodel.Workbook#createCellStyle()
setCellTypeback to summary
public void setCellType(CellType cellType)

Deprecated

This method is deprecated and will be removed in POI 5.0. Use explicit setCellFormula(String), setCellValue(...) or setBlank() to get the desired result.

Set the cells type (blank, numeric, boolean, error or string).

If the cell currently contains a value, the value will be converted to match the new type, if possible. Formatting is generally lost in the process however.

Conversion rules:

to NUMERIC: numeric value is left as is. True converts to 1.0, false converts to 0. otherwise, the value is set to 0. Formula is removed.

If what you want to do is get a String value for your numeric cell, stop! This is not the way to do it. Instead, for fetching the string value of a numeric or boolean or date cell, use DataFormatter instead.

If cell is a member of an array formula group containing more than 1 cell, an IllegalStateException is thrown. If the array formula group contains only this cell, it is removed.

Passing CellType#FORMULA is illegal and will result in an IllegalArgumentException.

Annotations
@Deprecated
@Removal
version:5.0
Exceptions
IllegalArgumentException:
if the specified cell type is invalid (null, _NONE or FORMULA)
IllegalStateException:
if the current value cannot be converted to the new type or if the cell is a part of an array formula group containing other cells
setCellValueback to summary
public void setCellValue(double value)

Set a numeric value for the cell.

Parameters
value:double

the numeric value to set this cell to. For formulas, we'll set the precalculated value, for numerics we'll set its value. For other types, we will change the cell to a numeric cell and set its value.

setCellValueback to summary
public void setCellValue(Date value)

Converts the supplied date to its equivalent Excel numeric value and sets that into the cell.

Note

- There is actually no 'DATE' cell type in Excel. In many cases (when entering date values), Excel automatically adjusts the cell style to some date format, creating the illusion that the cell data type is now something besides CellType#NUMERIC. POI does not attempt to replicate this behaviour. To make a numeric cell display as a date, use setCellStyle(CellStyle) etc.

Parameters
value:Date

the numeric value to set this cell to. For formulas, we'll set the precalculated value, for numerics we'll set its value. For other types, we will change the cell to a numerics cell and set its value.

setCellValueback to summary
public void setCellValue(LocalDateTime value)

Converts the supplied date to its equivalent Excel numeric value and sets that into the cell.

Note

- There is actually no 'DATE' cell type in Excel. In many cases (when entering date values), Excel automatically adjusts the cell style to some date format, creating the illusion that the cell data type is now something besides CellType#NUMERIC. POI does not attempt to replicate this behaviour. To make a numeric cell display as a date, use setCellStyle(CellStyle) etc.

Parameters
value:LocalDateTime

the numeric value to set this cell to. For formulas, we'll set the precalculated value, for numerics we'll set its value. For other types, we will change the cell to a numerics cell and set its value.

setCellValueback to summary
public default void setCellValue(LocalDate value)

Converts the supplied date to its equivalent Excel numeric value and sets that into the cell.

Note

- There is actually no 'DATE' cell type in Excel. In many cases (when entering date values), Excel automatically adjusts the cell style to some date format, creating the illusion that the cell data type is now something besides CellType#NUMERIC. POI does not attempt to replicate this behaviour. To make a numeric cell display as a date, use setCellStyle(CellStyle) etc.

Parameters
value:LocalDate

the numeric value to set this cell to. For formulas, we'll set the precalculated value, for numerics we'll set its value. For other types, we will change the cell to a numerics cell and set its value.

setCellValueback to summary
public void setCellValue(Calendar value)

Set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as a date.

This will set the cell value based on the Calendar's timezone. As Excel does not support timezones this means that both 20:00+03:00 and 20:00-03:00 will be reported as the same value (20:00) even that there are 6 hours difference between the two times. This difference can be preserved by using setCellValue(value.getTime()) which will automatically shift the times to the default timezone.

Parameters
value:Calendar

the date value to set this cell to. For formulas, we'll set the precalculated value, for numerics we'll set its value. For other types, we will change the cell to a numeric cell and set its value.

setCellValueback to summary
public void setCellValue(RichTextString value)

Set a rich string value for the cell.

Parameters
value:RichTextString

value to set the cell to. For formulas, we'll set the formula string, for String cells we'll set its value. For other types, we will change the cell to a string cell and set its value. If value is null then we will change the cell to a Blank cell.

setCellValueback to summary
public void setCellValue(String value)

Set a string value for the cell.

Parameters
value:String

value to set the cell to. For formulas, we'll set the formula string, for String cells we'll set its value. For other types, we will change the cell to a string cell and set its value. If value is null then we will change the cell to a Blank cell.

setCellValueback to summary
public void setCellValue(boolean value)

Set a boolean value for the cell

Parameters
value:boolean

the boolean value to set this cell to. For formulas, we'll set the precalculated value, for booleans we'll set its value. For other types, we will change the cell to a boolean cell and set its value.