Class XSSFCell
- All Implemented Interfaces:
Cell
Cells can be numeric, formula-based or string-based (text). The cell type specifies this. String cells cannot contain 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. Only cells that have values should be added.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
copyCellFrom
(Cell srcCell, CellCopyPolicy policy) 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 This does not shift references in formulas.Gets the address of this cellOnly valid for array formula cellsboolean
Get the value of the cell as a boolean.Only valid for formula cellsDeprecated.Returns cell comment associated with this cellReturn a formula for the cell, for example,SUM(C4:E4)
protected String
package/hierarchy use only - reuse an existing evaluation workbook if available for cachingReturn the cell's style.Return the cell type.Deprecated.usegetCellType
insteadint
Returns column index of this cellorg.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell
Returns the xml bean containing information about the cell's location (reference), value, data type, formatting, and formulaGet the value of the cell as a date.Returns the error message, such as #VALUE!byte
Get the value of the cell as an error code.Returns hyperlink associated with this celldouble
Get the value of the cell as a number.Returns the raw, underlying ooxml value for the cellReturns an A1 style reference to the location of this cellGet the value of the cell as a XSSFRichTextStringgetRow()
Returns the row this cell belongs toint
Returns row index of a row in the sheet that contains this cellprotected SharedStringsTable
getSheet()
Returns the sheet this cell belongs toGet the value of the cell as a stringprotected StylesTable
boolean
void
Removes the comment for this cell, if there is one.void
Removes the hyperlink for this cell, if there is one.void
Sets this cell as the active cell for the worksheetvoid
setCellComment
(Comment comment) Assign a comment to this cell.void
setCellErrorValue
(byte errorCode) Set a error value for the cellvoid
setCellErrorValue
(FormulaError error) Set a error value for the cellvoid
setCellFormula
(String formula) Sets formula for this cell.protected void
setCellNum
(int num) Sets column index of this cellvoid
setCellStyle
(CellStyle style) Set the style for the cell.void
setCellType
(CellType cellType) Set the cells type (numeric, formula or string)protected void
setCellType
(CellType cellType, BaseXSSFEvaluationWorkbook evalWb) Needed by bug #62834, which points out getCellFormula() expects an evaluation context or creates a new one, so if there is one in use, it needs to be carried on through.void
setCellValue
(boolean value) Set a boolean value for the cellvoid
setCellValue
(double value) Set a numeric value for the cellvoid
setCellValue
(String str) Set a string value for the cell.void
setCellValue
(Calendar value) Set a date value for the cell.void
setCellValue
(Date value) Set a date value for the cell.void
Set a string value for the cell.void
setCTCell
(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell cell) Set a new internal xml bean.void
setHyperlink
(Hyperlink hyperlink) Assign a hyperlink to this cell.toString()
Returns a string representation of the cellvoid
-
Constructor Details
-
XSSFCell
Construct a XSSFCell.- Parameters:
row
- the parent row.cell
- the xml bean containing information about the cell.
-
-
Method Details
-
copyCellFrom
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 This does not shift references in formulas. UseXSSFRowShifter
to shift references in formulas.- Parameters:
srcCell
- The cell to take value, formula and style frompolicy
- The policy for copying the information, seeCellCopyPolicy
- Throws:
IllegalArgumentException
- if copy cell style and srcCell is from a different workbook
-
getStylesSource
- Returns:
- table of cell styles shared across this workbook
-
getSheet
Returns the sheet this cell belongs to -
getRow
Returns the row this cell belongs to -
getBooleanCellValue
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.
- Specified by:
getBooleanCellValue
in interfaceCell
- Returns:
- the value of the cell as a boolean
- Throws:
IllegalStateException
- if the cell type returned bygetCellType()
is notCellType.BOOLEAN
,CellType.BLANK
orCellType.FORMULA
-
setCellValue
public void setCellValue(boolean value) Set a boolean value for the cell- Specified by:
setCellValue
in interfaceCell
- Parameters:
value
- 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.
-
getNumericCellValue
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;
- Specified by:
getNumericCellValue
in interfaceCell
- Returns:
- the value of the cell as a number
- Throws:
IllegalStateException
- if the cell type returned bygetCellType()
isCellType.STRING
NumberFormatException
- if the cell value isn't a parsabledouble
.- See Also:
-
setCellValue
public void setCellValue(double value) Set a numeric value for the cell- Specified by:
setCellValue
in interfaceCell
- Parameters:
value
- 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.
-
getStringCellValue
Get the value of the cell as a stringFor 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
- Specified by:
getStringCellValue
in interfaceCell
- Returns:
- the value of the cell as a string
-
getRichStringCellValue
Get the value of the cell as a XSSFRichTextStringFor 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
- Specified by:
getRichStringCellValue
in interfaceCell
- Returns:
- the value of the cell as a XSSFRichTextString
-
setCellValue
Set a string value for the cell.- Specified by:
setCellValue
in interfaceCell
- Parameters:
str
- value to set the cell to. For formulas we'll set the formula cached string result, 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.
-
setCellValue
Set a string value for the cell.- Specified by:
setCellValue
in interfaceCell
- Parameters:
str
- value to set the cell to. For formulas we'll set the 'pre-evaluated result 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.
-
getCellFormula
Return a formula for the cell, for example,SUM(C4:E4)
- Specified by:
getCellFormula
in interfaceCell
- Returns:
- a formula for the cell
- Throws:
IllegalStateException
- if the cell type returned bygetCellType()
is notCellType.FORMULA
-
getCellFormula
package/hierarchy use only - reuse an existing evaluation workbook if available for caching- Parameters:
fpb
- evaluation workbook for reuse, if available, or null to create a new one as needed- Returns:
- a formula for the cell
- Throws:
IllegalStateException
- if the cell type returned bygetCellType()
is notCellType.FORMULA
-
setCellFormula
Sets formula for this cell.Note, this method only sets the formula string and does not calculate the formula value. To set the precalculated value use
setCellValue(double)
orsetCellValue(String)
Note, if there are any shared formulas, his will invalidate any
FormulaEvaluator
instances based on this workbook.- Specified by:
setCellFormula
in interfaceCell
- Parameters:
formula
- the formula to set, e.g."SUM(C4:E4)"
. If the argument isnull
then the current formula is removed.- Throws:
FormulaParseException
- if the formula has incorrect syntax or is otherwise invalidIllegalStateException
- if the operation is not allowed, for example, when the cell is a part of a multi-cell array formula
-
getColumnIndex
public int getColumnIndex()Returns column index of this cell- Specified by:
getColumnIndex
in interfaceCell
- Returns:
- zero-based column index of a column in a sheet.
-
getRowIndex
public int getRowIndex()Returns row index of a row in the sheet that contains this cell- Specified by:
getRowIndex
in interfaceCell
- Returns:
- zero-based row index of a row in the sheet that contains this cell
-
getReference
Returns an A1 style reference to the location of this cell- Returns:
- A1 style reference to the location of this cell
-
getAddress
Gets the address of this cell- Specified by:
getAddress
in interfaceCell
- Returns:
A1
style address of this cell
-
getCellStyle
Return the cell's style.- Specified by:
getCellStyle
in interfaceCell
- Returns:
- the cell's style.
- See Also:
-
setCellStyle
Set the style for the cell. The style should be an XSSFCellStyle created/retreived from the XSSFWorkbook.
To change the style of a cell without affecting other cells that use the same style, use
CellUtil.setCellStyleProperties(Cell, java.util.Map)
- Specified by:
setCellStyle
in interfaceCell
- Parameters:
style
- 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.- Throws:
IllegalArgumentException
- if style belongs to a different styles source (most likely because style is from a different Workbook)- See Also:
-
getCellType
Return the cell type. Tables in an array formula returnCellType.FORMULA
for all cells, even though the formula is only defined in the OOXML file for the top left cell of the array.NOTE: POI does not support data table formulas. Cells in a data table appear to POI as plain cells typed from their cached value.
- Specified by:
getCellType
in interfaceCell
- Returns:
- the cell type
-
getCellTypeEnum
Deprecated.usegetCellType
insteadReturn the cell type. Tables in an array formula returnCellType.FORMULA
for all cells, even though the formula is only defined in the OOXML file for the top left cell of the array.NOTE: POI does not support data table formulas. Cells in a data table appear to POI as plain cells typed from their cached value.
- Specified by:
getCellTypeEnum
in interfaceCell
- Returns:
- the cell type
- Since:
- POI 3.15 beta 3
-
getCachedFormulaResultType
Only valid for formula cells- Specified by:
getCachedFormulaResultType
in interfaceCell
- Returns:
- one of (
CellType.NUMERIC
,CellType.STRING
,CellType.BOOLEAN
,CellType.ERROR
) depending on the cached value of the formula
-
getCachedFormulaResultTypeEnum
Deprecated.usegetCachedFormulaResultType
instead Will be deleted when we make the CellType enum transition. See bug 59791.Only valid for formula cells- Specified by:
getCachedFormulaResultTypeEnum
in interfaceCell
- Returns:
- one of (
CellType.NUMERIC
,CellType.STRING
,CellType.BOOLEAN
,CellType.ERROR
) depending on the cached value of the formula - Since:
- POI 3.15 beta 3
-
getDateCellValue
Get the value of the cell as a date.For strings we throw an exception. For blank cells we return a null.
- Specified by:
getDateCellValue
in interfaceCell
- Returns:
- the value of the cell as a date
- Throws:
IllegalStateException
- if the cell type returned bygetCellType()
isCellType.STRING
NumberFormatException
- if the cell value isn't a parsabledouble
.- See Also:
-
setCellValue
Set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as a date.- Specified by:
setCellValue
in interfaceCell
- Parameters:
value
- 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.
-
setCellValue
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.- Specified by:
setCellValue
in interfaceCell
- Parameters:
value
- the date value to set this cell to. For formulas we'll set the precalculated value, for numerics we'll set its value. For othertypes we will change the cell to a numeric cell and set its value.
-
getErrorCellString
Returns the error message, such as #VALUE!- Returns:
- the error message such as #VALUE!
- Throws:
IllegalStateException
- if the cell type returned bygetCellType()
isn'tCellType.ERROR
- See Also:
-
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.
- Specified by:
getErrorCellValue
in interfaceCell
- Returns:
- the value of the cell as an error code
- Throws:
IllegalStateException
- if the cell type returned bygetCellType()
isn't#ERROR
- See Also:
-
setCellErrorValue
public void setCellErrorValue(byte errorCode) Set a error value for the cell- Specified by:
setCellErrorValue
in interfaceCell
- Parameters:
errorCode
- 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:
-
setCellErrorValue
Set a error value for the cell- Parameters:
error
- 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.
-
setAsActiveCell
public void setAsActiveCell()Sets this cell as the active cell for the worksheet- Specified by:
setAsActiveCell
in interfaceCell
-
setCellNum
protected void setCellNum(int num) Sets column index of this cell- Parameters:
num
- column index of this cell
-
setCellType
Set the cells type (numeric, formula or string)- Specified by:
setCellType
in interfaceCell
- Throws:
IllegalArgumentException
- if the specified cell type is invalid
-
setCellType
Needed by bug #62834, which points out getCellFormula() expects an evaluation context or creates a new one, so if there is one in use, it needs to be carried on through.- Parameters:
cellType
-evalWb
- BaseXSSFEvaluationWorkbook already in use, or null if a new implicit one should be used
-
toString
Returns a string representation of the cellFormula cells return the formula string, rather than the formula result. Dates are displayed in dd-MMM-yyyy format Errors are displayed as #ERR<errIdx>
-
getRawValue
Returns the raw, underlying ooxml value for the cellIf the cell contains a string, then this value is an index into the shared string table, pointing to the actual string value. Otherwise, the value of the cell is expressed directly in this element. Cells containing formulas express the last calculated result of the formula in this element.
- Returns:
- the raw cell value as contained in the underlying CTCell bean,
null
for blank cells.
-
getCellComment
Returns cell comment associated with this cell- Specified by:
getCellComment
in interfaceCell
- Returns:
- the cell comment associated with this cell or
null
-
setCellComment
Assign a comment to this cell. If the supplied comment is null, the comment for this cell will be removed.- Specified by:
setCellComment
in interfaceCell
- Parameters:
comment
- the XSSFComment associated with this cell
-
removeCellComment
public void removeCellComment()Removes the comment for this cell, if there is one.- Specified by:
removeCellComment
in interfaceCell
-
getHyperlink
Returns hyperlink associated with this cell- Specified by:
getHyperlink
in interfaceCell
- Returns:
- hyperlink associated with this cell or
null
if not found
-
setHyperlink
Assign a hyperlink to this cell. If the supplied hyperlink is null, the hyperlink for this cell will be removed.- Specified by:
setHyperlink
in interfaceCell
- Parameters:
hyperlink
- the hyperlink to associate with this cell
-
removeHyperlink
public void removeHyperlink()Removes the hyperlink for this cell, if there is one.- Specified by:
removeHyperlink
in interfaceCell
-
getCTCell
Returns the xml bean containing information about the cell's location (reference), value, data type, formatting, and formula- Returns:
- the xml bean containing information about this cell
-
setCTCell
Set a new internal xml bean. This is only for internal use, do not call this from outside! This is necessary in some rare cases to work around XMLBeans specialties. -
getArrayFormulaRange
Description copied from interface:Cell
Only valid for array formula cells- Specified by:
getArrayFormulaRange
in interfaceCell
- Returns:
- range of the array formula group that the cell belongs to.
-
isPartOfArrayFormulaGroup
public boolean isPartOfArrayFormulaGroup()- Specified by:
isPartOfArrayFormulaGroup
in interfaceCell
- Returns:
true
if this cell is part of group of cells having a common array formula.
-
updateCellReferencesForShifting
-
getCachedFormulaResultType
instead Will be deleted when we make the CellType enum transition.