org.apache.poi.xssf.usermodel
Class XSSFCell

java.lang.Object
  extended by org.apache.poi.xssf.usermodel.XSSFCell
All Implemented Interfaces:
Cell

public final class XSSFCell
extends java.lang.Object
implements Cell

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 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.


Field Summary
 
Fields inherited from interface org.apache.poi.ss.usermodel.Cell
CELL_TYPE_BLANK, CELL_TYPE_BOOLEAN, CELL_TYPE_ERROR, CELL_TYPE_FORMULA, CELL_TYPE_NUMERIC, CELL_TYPE_STRING
 
Constructor Summary
protected XSSFCell(XSSFRow row, org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell cell)
          Construct a XSSFCell.
 
Method Summary
 void 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.
 CellAddress getAddress()
          
 CellRangeAddress getArrayFormulaRange()
           
 boolean getBooleanCellValue()
          Get the value of the cell as a boolean.
 int getCachedFormulaResultType()
          Deprecated. 3.15. Will return a CellType enum in the future.
 CellType getCachedFormulaResultTypeEnum()
          Only valid for formula cells
 XSSFComment getCellComment()
          Returns cell comment associated with this cell
 java.lang.String getCellFormula()
          Return a formula for the cell, for example, SUM(C4:E4)
protected  java.lang.String getCellFormula(XSSFEvaluationWorkbook fpb)
          package/hierarchy use only - reuse an existing evaluation workbook if available for caching
 XSSFCellStyle getCellStyle()
          Return the cell's style.
 int getCellType()
          Deprecated. 3.15. Will return a CellType enum in the future.
 CellType getCellTypeEnum()
          Return the cell type.
 int getColumnIndex()
          Returns column index of this cell
 org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell getCTCell()
          Returns the xml bean containing information about the cell's location (reference), value, data type, formatting, and formula
 java.util.Date getDateCellValue()
          Get the value of the cell as a date.
 java.lang.String getErrorCellString()
          Returns the error message, such as #VALUE!
 byte getErrorCellValue()
          Get the value of the cell as an error code.
 XSSFHyperlink getHyperlink()
          Returns hyperlink associated with this cell
 double getNumericCellValue()
          Get the value of the cell as a number.
 java.lang.String getRawValue()
          Returns the raw, underlying ooxml value for the cell
 java.lang.String getReference()
          Returns an A1 style reference to the location of this cell
 XSSFRichTextString getRichStringCellValue()
          Get the value of the cell as a XSSFRichTextString
 XSSFRow getRow()
          Returns the row this cell belongs to
 int getRowIndex()
          Returns row index of a row in the sheet that contains this cell
protected  SharedStringsTable getSharedStringSource()
           
 XSSFSheet getSheet()
          Returns the sheet this cell belongs to
 java.lang.String getStringCellValue()
          Get the value of the cell as a string
protected  StylesTable getStylesSource()
           
 boolean isPartOfArrayFormulaGroup()
           
 void removeCellComment()
          Removes the comment for this cell, if there is one.
 void removeHyperlink()
          Removes the hyperlink for this cell, if there is one.
 void setAsActiveCell()
          
 void setCellComment(Comment comment)
          Assign a comment to this cell.
 void setCellErrorValue(byte errorCode)
          Set a error value for the cell
 void setCellErrorValue(FormulaError error)
          Set a error value for the cell
 void setCellFormula(java.lang.String formula)
          Sets formula for this cell.
protected  void setCellNum(int num)
          Sets column index of this cell
 void setCellStyle(CellStyle style)
          Set the style for the cell.
 void setCellType(CellType cellType)
          Set the cells type (numeric, formula or string)
 void setCellType(int cellType)
          Deprecated. POI 3.15 beta 3. Use setCellType(CellType) instead.
 void setCellValue(boolean value)
          Set a boolean value for the cell
 void setCellValue(java.util.Calendar value)
          Set a date value for the cell.
 void setCellValue(java.util.Date value)
          Set a date value for the cell.
 void setCellValue(double value)
          Set a numeric value for the cell
 void setCellValue(RichTextString str)
          Set a string value for the cell.
 void setCellValue(java.lang.String str)
          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.
 java.lang.String toString()
          Returns a string representation of the cell
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

XSSFCell

protected XSSFCell(XSSFRow row,
                   org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell cell)
Construct a XSSFCell.

Parameters:
row - the parent row.
cell - the xml bean containing information about the cell.
Method Detail

copyCellFrom

@Beta
@Internal
public void 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. Use XSSFRowShifter to shift references in formulas.

Parameters:
srcCell - The cell to take value, formula and style from
policy - The policy for copying the information, see CellCopyPolicy
Throws:
java.lang.IllegalArgumentException - if copy cell style and srcCell is from a different workbook

getSharedStringSource

protected SharedStringsTable getSharedStringSource()
Returns:
table of strings shared across this workbook

getStylesSource

protected StylesTable getStylesSource()
Returns:
table of cell styles shared across this workbook

getSheet

public XSSFSheet getSheet()
Returns the sheet this cell belongs to

Specified by:
getSheet in interface Cell
Returns:
the sheet this cell belongs to

getRow

public XSSFRow getRow()
Returns the row this cell belongs to

Specified by:
getRow in interface Cell
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 interface Cell
Returns:
the value of the cell as a boolean
Throws:
java.lang.IllegalStateException - if the cell type returned by getCellTypeEnum() is not CellType.BOOLEAN, CellType.BLANK or CellType.FORMULA

setCellValue

public void setCellValue(boolean value)
Set a boolean value for the cell

Specified by:
setCellValue in interface Cell
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 interface Cell
Returns:
the value of the cell as a number
Throws:
java.lang.IllegalStateException - if the cell type returned by getCellTypeEnum() is CellType.STRING
java.lang.NumberFormatException - if the cell value isn't a parsable double.
See Also:
for turning this number into a string similar to that which Excel would render this number as.

setCellValue

public void setCellValue(double value)
Set a numeric value for the cell

Specified by:
setCellValue in interface Cell
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

public java.lang.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

Specified by:
getStringCellValue in interface Cell
Returns:
the value of the cell as a string

getRichStringCellValue

public XSSFRichTextString 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

Specified by:
getRichStringCellValue in interface Cell
Returns:
the value of the cell as a XSSFRichTextString

setCellValue

public void setCellValue(java.lang.String str)
Set a string value for the cell.

Specified by:
setCellValue in interface Cell
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

public void setCellValue(RichTextString str)
Set a string value for the cell.

Specified by:
setCellValue in interface Cell
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

public java.lang.String getCellFormula()
Return a formula for the cell, for example, SUM(C4:E4)

Specified by:
getCellFormula in interface Cell
Returns:
a formula for the cell
Throws:
java.lang.IllegalStateException - if the cell type returned by getCellTypeEnum() is not CellType.FORMULA

getCellFormula

protected java.lang.String getCellFormula(XSSFEvaluationWorkbook fpb)
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:
java.lang.IllegalStateException - if the cell type returned by getCellTypeEnum() is not CellType.FORMULA

setCellFormula

public void setCellFormula(java.lang.String formula)
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) or setCellValue(String)

Specified by:
setCellFormula in interface Cell
Parameters:
formula - the formula to set, e.g. "SUM(C4:E4)". If the argument is null then the current formula is removed.
Throws:
FormulaParseException - if the formula has incorrect syntax or is otherwise invalid
java.lang.IllegalStateException - 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 interface Cell
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 interface Cell
Returns:
zero-based row index of a row in the sheet that contains this cell

getReference

public java.lang.String getReference()
Returns an A1 style reference to the location of this cell

Returns:
A1 style reference to the location of this cell

getAddress

public CellAddress getAddress()

Specified by:
getAddress in interface Cell

getCellStyle

public XSSFCellStyle getCellStyle()
Return the cell's style.

Specified by:
getCellStyle in interface Cell
Returns:
the cell's style.

setCellStyle

public void setCellStyle(CellStyle style)

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 interface Cell
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:
java.lang.IllegalArgumentException - if style belongs to a different styles source (most likely because style is from a different Workbook)

getCellType

@Deprecated
@Removal(version="3.17")
public int getCellType()
Deprecated. 3.15. Will return a CellType enum in the future.

Return the cell type. Will return CellType in a future version of POI. For forwards compatibility, do not hard-code cell type literals in your code.

Specified by:
getCellType in interface Cell
Returns:
the cell type

getCellTypeEnum

public CellType getCellTypeEnum()
Return the cell type. Tables in an array formula return CellType.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 interface Cell
Returns:
the cell type
Since:
POI 3.15 beta 3 Will be deleted when we make the CellType enum transition. See bug 59791.

getCachedFormulaResultType

@Deprecated
@Removal(version="3.17")
public int getCachedFormulaResultType()
Deprecated. 3.15. Will return a CellType enum in the future.

Only valid for formula cells Will return CellType in a future version of POI. For forwards compatibility, do not hard-code cell type literals in your code.

Specified by:
getCachedFormulaResultType in interface Cell
Returns:
one of (CellType.NUMERIC, CellType.STRING, CellType.BOOLEAN, CellType.ERROR) depending on the cached value of the formula

getCachedFormulaResultTypeEnum

public CellType getCachedFormulaResultTypeEnum()
Only valid for formula cells

Specified by:
getCachedFormulaResultTypeEnum in interface Cell
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 Will be deleted when we make the CellType enum transition. See bug 59791.

getDateCellValue

public java.util.Date 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 interface Cell
Returns:
the value of the cell as a date
Throws:
java.lang.IllegalStateException - if the cell type returned by getCellTypeEnum() is CellType.STRING
java.lang.NumberFormatException - if the cell value isn't a parsable double.
See Also:
for formatting this date into a string similar to how excel does.

setCellValue

public void setCellValue(java.util.Date value)
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 interface Cell
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

public void setCellValue(java.util.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.

Specified by:
setCellValue in interface Cell
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

public java.lang.String getErrorCellString()
                                    throws java.lang.IllegalStateException
Returns the error message, such as #VALUE!

Returns:
the error message such as #VALUE!
Throws:
java.lang.IllegalStateException - if the cell type returned by getCellTypeEnum() isn't CellType.ERROR
See Also:
FormulaError

getErrorCellValue

public byte getErrorCellValue()
                       throws java.lang.IllegalStateException
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 interface Cell
Returns:
the value of the cell as an error code
Throws:
java.lang.IllegalStateException - if the cell type returned by getCellTypeEnum() isn't #ERROR
See Also:
FormulaError

setCellErrorValue

public void setCellErrorValue(byte errorCode)
Set a error value for the cell

Specified by:
setCellErrorValue in interface Cell
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:
FormulaError

setCellErrorValue

public void setCellErrorValue(FormulaError error)
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()

Specified by:
setAsActiveCell in interface Cell

setCellNum

protected void setCellNum(int num)
Sets column index of this cell

Parameters:
num - column index of this cell

setCellType

@Deprecated
@Removal(version="3.17")
public void setCellType(int cellType)
Deprecated. POI 3.15 beta 3. Use setCellType(CellType) instead.

Set the cells type (numeric, formula or string)

Specified by:
setCellType in interface Cell
Throws:
java.lang.IllegalArgumentException - if the specified cell type is invalid
See Also:
CellType.NUMERIC, CellType.STRING, CellType.FORMULA, CellType.BLANK, CellType.BOOLEAN, CellType.ERROR

setCellType

public void setCellType(CellType cellType)
Set the cells type (numeric, formula or string)

Specified by:
setCellType in interface Cell
Throws:
java.lang.IllegalArgumentException - if the specified cell type is invalid

toString

public java.lang.String toString()
Returns a string representation of the cell

Formula cells return the formula string, rather than the formula result. Dates are displayed in dd-MMM-yyyy format Errors are displayed as #ERR<errIdx>

Overrides:
toString in class java.lang.Object

getRawValue

public java.lang.String getRawValue()
Returns the raw, underlying ooxml value for the cell

If 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

public XSSFComment getCellComment()
Returns cell comment associated with this cell

Specified by:
getCellComment in interface Cell
Returns:
the cell comment associated with this cell or null

setCellComment

public void setCellComment(Comment comment)
Assign a comment to this cell. If the supplied comment is null, the comment for this cell will be removed.

Specified by:
setCellComment in interface Cell
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 interface Cell

getHyperlink

public XSSFHyperlink getHyperlink()
Returns hyperlink associated with this cell

Specified by:
getHyperlink in interface Cell
Returns:
hyperlink associated with this cell or null if not found

setHyperlink

public void setHyperlink(Hyperlink hyperlink)
Assign a hyperlink to this cell. If the supplied hyperlink is null, the hyperlink for this cell will be removed.

Specified by:
setHyperlink in interface Cell
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 interface Cell

getCTCell

@Internal
public org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell 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

@Internal
public void setCTCell(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell cell)
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

public CellRangeAddress getArrayFormulaRange()
Specified by:
getArrayFormulaRange in interface Cell

isPartOfArrayFormulaGroup

public boolean isPartOfArrayFormulaGroup()
Specified by:
isPartOfArrayFormulaGroup in interface Cell