Class XSSFCell

  • All Implemented Interfaces:
    Cell

    public final class XSSFCell
    extends CellBase
    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.

    • 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

      • 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
        Returns:
        the sheet this cell belongs to
      • getRow

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

        public void setCellValue​(boolean value)
        Set a boolean value for the 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.
      • getStringCellValue

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

        Returns:
        the value of the cell as a XSSFRichTextString
      • getCellFormula

        protected String getCellFormula​(BaseXSSFEvaluationWorkbook 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:
        IllegalStateException - if the cell type returned by getCellType() is not CellType.FORMULA
      • setCellFormulaImpl

        protected void setCellFormulaImpl​(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 CellBase.setCellValue(double) or CellBase.setCellValue(String)

        Note, if there are any shared formulas, his will invalidate any FormulaEvaluator instances based on this workbook.

        Specified by:
        setCellFormulaImpl in class CellBase
        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
        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
        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
        Returns:
        zero-based row index of a row in the sheet that contains this cell
      • getReference

        public String getReference()
        Returns an A1 style reference to the location of this cell
        Returns:
        A1 style reference to the location of this cell
      • getCellStyle

        public XSSFCellStyle getCellStyle()
        Return the cell's style.
        Returns:
        the cell's style.
      • setCellStyle

        public void setCellStyle​(CellStyle style)

        Set the style for the cell. The style should be an XSSFCellStyle created/retrieved 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)

        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)
      • getCellType

        public CellType getCellType()
        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.

        Returns:
        the cell type
      • getErrorCellValue

        public byte getErrorCellValue()
                               throws 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.

        Returns:
        the value of the cell as an error code
        Throws:
        IllegalStateException - if the cell type returned by getCellType() isn't #ERROR
        See Also:
        FormulaError
      • setCellErrorValue

        public void setCellErrorValue​(byte errorCode)
        Set a error value for the 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()
      • setCellNum

        protected void setCellNum​(int num)
        Sets column index of this cell
        Parameters:
        num - column index of this cell
      • setCellType

        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.
        Parameters:
        evalWb - BaseXSSFEvaluationWorkbook already in use, or null if a new implicit one should be used
      • toString

        public 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 Object
      • getRawValue

        public 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
        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.
        Parameters:
        comment - the XSSFComment associated with this cell
      • removeCellComment

        public void removeCellComment()
        Removes the comment for this cell, if there is one.
      • getHyperlink

        public XSSFHyperlink getHyperlink()
        Returns hyperlink associated with this 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.
        Parameters:
        hyperlink - the hyperlink to associate with this cell
      • removeHyperlink

        public void removeHyperlink()
        Removes the hyperlink for this cell, if there is one.
      • 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.
      • isPartOfArrayFormulaGroup

        public boolean isPartOfArrayFormulaGroup()
      • updateCellReferencesForShifting

        public void updateCellReferencesForShifting​(String msg)