org.apache.poi.hssf.usermodel

## Class HSSFCell

- java.lang.Object
- - org.apache.poi.ss.usermodel.CellBase
  - - org.apache.poi.hssf.usermodel.HSSFCell

- All Implemented Interfaces:
    Cell

      public class HSSFCell
      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.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static short` | `ENCODING_COMPRESSED_UNICODE`
    `static short` | `ENCODING_UNCHANGED`
    `static short` | `ENCODING_UTF_16`
    `static int` | `LAST_COLUMN_NUMBER` The maximum number of columns in BIFF8

  - ### Constructor Summary

    Constructors
    Modifier | Constructor and Description
    --- | ---
    `protected` | `HSSFCell(HSSFWorkbook book, HSSFSheet sheet, CellValueRecordInterface cval)` Creates an HSSFCell from a CellValueRecordInterface.
    `protected` | `HSSFCell(HSSFWorkbook book, HSSFSheet sheet, int row, short col)` Creates new Cell - Should only be called by HSSFRow.
    `protected` | `HSSFCell(HSSFWorkbook book, HSSFSheet sheet, int row, short col, CellType type)` Creates new Cell - Should only be called by HSSFRow.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods Deprecated Methods
    Modifier and Type | Method and Description
    --- | ---
    `CellRangeAddress` | `getArrayFormulaRange()` Only valid for array formula cells
    `boolean` | `getBooleanCellValue()` get the value of the cell as a boolean.
    `protected InternalWorkbook` | `getBoundWorkbook()` Returns the Workbook that this Cell is bound to
    `CellType` | `getCachedFormulaResultType()` Only valid for formula cells
    `CellType` | `getCachedFormulaResultTypeEnum()` Deprecated. use `getCachedFormulaResultType` Will be deleted when we make the CellType enum transition. See bug 59791.
    `HSSFComment` | `getCellComment()` Returns comment associated with this cell
    `java.lang.String` | `getCellFormula()` Return a formula for the cell, for example, `SUM(C4:E4)`
    `HSSFCellStyle` | `getCellStyle()` get the style for the cell.
    `CellType` | `getCellType()` get the cells type (numeric, formula or string)
    `protected CellValueRecordInterface` | `getCellValueRecord()` Should only be used by HSSFSheet and friends.
    `int` | `getColumnIndex()` Returns column index of this cell
    `java.util.Date` | `getDateCellValue()` Get the value of the cell as a date.
    `byte` | `getErrorCellValue()` get the value of the cell as an error code.
    `HSSFHyperlink` | `getHyperlink()`
    `java.time.LocalDateTime` | `getLocalDateTimeCellValue()` Get the value of the cell as a LocalDateTime.
    `double` | `getNumericCellValue()` Get the value of the cell as a number.
    `HSSFRichTextString` | `getRichStringCellValue()` get the value of the cell as a string - for numeric cells we throw an exception.
    `HSSFRow` | `getRow()` Returns the HSSFRow this cell belongs to
    `int` | `getRowIndex()` Returns row index of a row in the sheet that contains this cell
    `HSSFSheet` | `getSheet()` Returns the HSSFSheet this cell belongs to
    `protected SpreadsheetVersion` | `getSpreadsheetVersion()` Get the spreadsheet version for the given implementation.
    `java.lang.String` | `getStringCellValue()` get the value of the cell as a string - for numeric cells we throw an exception.
    `boolean` | `isPartOfArrayFormulaGroup()`
    `void` | `removeCellComment()` Removes the comment for this cell, if there is one.
    `protected void` | `removeFormulaImpl()` Implementation-specific removal of the formula.
    `void` | `removeHyperlink()` Removes the hyperlink for this cell, if there is one.
    `void` | `setAsActiveCell()` Sets this cell as the active cell for the worksheet
    `void` | `setCellComment(Comment comment)` Assign a comment to this cell.
    `void` | `setCellErrorValue(byte errorCode)` Deprecated. 3.15 beta 2. Use `setCellErrorValue(FormulaError)` instead.
    `void` | `setCellErrorValue(FormulaError error)` set a error value for the cell
    `protected void` | `setCellFormulaImpl(java.lang.String formula)` Implementation-specific setting the formula.
    `void` | `setCellStyle(CellStyle style)` Set the style for the cell.
    `void` | `setCellStyle(HSSFCellStyle style)`
    `protected void` | `setCellTypeImpl(CellType cellType)` Implementation-specific logic
    `void` | `setCellValue(boolean value)` set a boolean value for the cell
    `protected void` | `setCellValueImpl(java.util.Calendar value)` Implementation-specific way to set a calendar value.
    `protected void` | `setCellValueImpl(java.util.Date value)` Implementation-specific way to set a date value.
    `protected void` | `setCellValueImpl(double value)` Implementation-specific way to set a numeric value.
    `protected void` | `setCellValueImpl(java.time.LocalDateTime value)` Implementation-specific way to set a date value.
    `protected void` | `setCellValueImpl(RichTextString value)` Implementation-specific way to set a RichTextString value.
    `protected void` | `setCellValueImpl(java.lang.String value)` Implementation-specific way to set a string value.
    `void` | `setHyperlink(Hyperlink hyperlink)` Assign a hyperlink to this cell.
    `java.lang.String` | `toString()` Returns a string representation of the cell This method returns a simple representation, anything more complex should be in user code, with knowledge of the semantics of the sheet being processed.
    `protected void` | `updateCellNum(short num)` Updates the cell record's idea of what column it belongs in (0 based)

    - ### Methods inherited from class org.apache.poi.ss.usermodel.CellBase

      `getAddress, getCellTypeEnum, getValueType, removeFormula, setBlank, setCellFormula, setCellType, setCellValue, setCellValue, setCellValue, setCellValue, setCellValue, setCellValue, tryToDeleteArrayFormula`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

    - ### Methods inherited from interface org.apache.poi.ss.usermodel.Cell

      `setCellValue`

- - ### Field Detail

    - #### LAST_COLUMN_NUMBER

          public static final int LAST_COLUMN_NUMBER

      The maximum number of columns in BIFF8

    - #### ENCODING_UNCHANGED

          public static final short ENCODING_UNCHANGED

      See Also:
        Constant Field Values

    - #### ENCODING_COMPRESSED_UNICODE

          public static final short ENCODING_COMPRESSED_UNICODE

      See Also:
        Constant Field Values

    - #### ENCODING_UTF_16

          public static final short ENCODING_UTF_16

      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### HSSFCell

          protected HSSFCell(HSSFWorkbook book,
                             HSSFSheet sheet,
                             int row,
                             short col)

      Creates new Cell - Should only be called by HSSFRow. This creates a cell from scratch.

      When the cell is initially created it is set to `CellType.BLANK`. Cell types can be changed/overwritten by calling setCellValue with the appropriate type as a parameter although conversions from one type to another may be prohibited.

      Parameters:
        `book` - - Workbook record of the workbook containing this cell
        `sheet` - - Sheet record of the sheet containing this cell
        `row` - - the row of this cell
        `col` - - the column for this cell
      See Also:
        `HSSFRow.createCell(int)`

    - #### HSSFCell

          protected HSSFCell(HSSFWorkbook book,
                             HSSFSheet sheet,
                             int row,
                             short col,
                             CellType type)

      Creates new Cell - Should only be called by HSSFRow. This creates a cell from scratch.
      Parameters:
        `book` - - Workbook record of the workbook containing this cell
        `sheet` - - Sheet record of the sheet containing this cell
        `row` - - the row of this cell
        `col` - - the column for this cell
        `type` - - Type of cell
      See Also:
        `HSSFRow.createCell(int,CellType)`

    - #### HSSFCell

          protected HSSFCell(HSSFWorkbook book,
                             HSSFSheet sheet,
                             CellValueRecordInterface cval)

      Creates an HSSFCell from a CellValueRecordInterface. HSSFSheet uses this when reading in cells from an existing sheet.
      Parameters:
        `book` - - Workbook record of the workbook containing this cell
        `sheet` - - Sheet record of the sheet containing this cell
        `cval` - - the Cell Value Record we wish to represent

  - ### Method Detail

    - #### getSpreadsheetVersion

          protected SpreadsheetVersion getSpreadsheetVersion()

      Get the spreadsheet version for the given implementation.
      Specified by:
        `getSpreadsheetVersion` in class `CellBase`
      Returns:
        the spreadsheet version

    - #### getSheet

          public HSSFSheet getSheet()

      Returns the HSSFSheet this cell belongs to
      Returns:
        the HSSFSheet that owns this cell

    - #### getRow

          public HSSFRow getRow()

      Returns the HSSFRow this cell belongs to
      Returns:
        the HSSFRow that owns this cell

    - #### getBoundWorkbook

          protected InternalWorkbook getBoundWorkbook()

      Returns the Workbook that this Cell is bound to

    - #### getRowIndex

          public int getRowIndex()

      Description copied from interface: `Cell`
      Returns row index of a row in the sheet that contains this cell
      Returns:
        the (zero based) index of the row containing this cell

    - #### updateCellNum

          protected void updateCellNum(short num)

      Updates the cell record's idea of what column it belongs in (0 based)
      Parameters:
        `num` - the new cell number

    - #### getColumnIndex

          public int getColumnIndex()

      Description copied from interface: `Cell`
      Returns column index of this cell
      Returns:
        zero-based column index of a column in a sheet.

    - #### setCellTypeImpl

          protected void setCellTypeImpl(CellType cellType)

      Description copied from class: `CellBase`
      Implementation-specific logic
      Specified by:
        `setCellTypeImpl` in class `CellBase`
      Parameters:
        `cellType` - new cell type. Guaranteed non-null, not _NONE.

    - #### getCellType

          public CellType getCellType()

      get the cells type (numeric, formula or string)
      Returns:
        the cell type

    - #### setCellValueImpl

          protected void setCellValueImpl(double value)

      Implementation-specific way to set a numeric value. `value` is guaranteed to be a valid (non-NaN) double. The implementation is expected to adjust the cell type accordingly, so that after this call getCellType() or getCachedFormulaResultType() would return `CellType.NUMERIC`.
      Specified by:
        `setCellValueImpl` in class `CellBase`
      Parameters:
        `value` - the new value to set

    - #### setCellValueImpl

          protected void setCellValueImpl(java.util.Date value)

      Implementation-specific way to set a date value. `value` is guaranteed to be non-null. The implementation is expected to adjust the cell type accordingly, so that after this call getCellType() or getCachedFormulaResultType() would return `CellType.NUMERIC`.

      In HSSF, only the number of days is stored. The fractional part is ignored.

      Specified by:
        `setCellValueImpl` in class `CellBase`
      Parameters:
        `value` - the new date to set
      See Also:
        `DateUtil`, `DateUtil`

    - #### setCellValueImpl

          protected void setCellValueImpl(java.time.LocalDateTime value)

      Implementation-specific way to set a date value. `value` is guaranteed to be non-null. The implementation is expected to adjust the cell type accordingly, so that after this call getCellType() or getCachedFormulaResultType() would return `CellType.NUMERIC`.

      In HSSF, only the number of days is stored. The fractional part is ignored.

      Specified by:
        `setCellValueImpl` in class `CellBase`
      Parameters:
        `value` - the new date to set
      See Also:
        `DateUtil`, `DateUtil`

    - #### setCellValueImpl

          protected void setCellValueImpl(java.util.Calendar value)

      Implementation-specific way to set a calendar value. `value` is guaranteed to be non-null. The implementation is expected to adjust the cell type accordingly, so that after this call getCellType() or getCachedFormulaResultType() would return `CellType.NUMERIC`.
      Specified by:
        `setCellValueImpl` in class `CellBase`
      Parameters:
        `value` - the new calendar value to set

    - #### setCellValueImpl

          protected void setCellValueImpl(java.lang.String value)

      Implementation-specific way to set a string value. The value is guaranteed to be non-null and to satisfy the length limitation imposed by the spreadsheet version. The implementation is expected to adjust cell type accordingly, so that after this call getCellType() or getCachedFormulaResultType() (whichever appropriate) would return `CellType.STRING`.
      Specified by:
        `setCellValueImpl` in class `CellBase`
      Parameters:
        `value` - the new value to set.

    - #### setCellValueImpl

          protected void setCellValueImpl(RichTextString value)

      Implementation-specific way to set a RichTextString value. The value is guaranteed to be non-null, having non-null value, and to satisfy the length limitation imposed by the spreadsheet version. The implementation is expected to adjust cell type accordingly, so that after this call getCellType() or getCachedFormulaResultType() (whichever appropriate) would return `CellType.STRING`.
      Specified by:
        `setCellValueImpl` in class `CellBase`
      Parameters:
        `value` - the new value to set.

    - #### setCellFormulaImpl

          protected void setCellFormulaImpl(java.lang.String formula)

      Implementation-specific setting the formula. Formula is not null. Shall not change the value.
      Specified by:
        `setCellFormulaImpl` in class `CellBase`

    - #### removeFormulaImpl

          protected void removeFormulaImpl()

      Description copied from class: `CellBase`
      Implementation-specific removal of the formula. The cell is guaranteed to have a regular formula set. Shall preserve the "cached" value.
      Specified by:
        `removeFormulaImpl` in class `CellBase`

    - #### getCellFormula

          public java.lang.String getCellFormula()

      Description copied from interface: `Cell`
      Return a formula for the cell, for example, `SUM(C4:E4)`
      Returns:
        a formula for the cell

    - #### 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. See `HSSFDataFormatter` for turning this number into a string similar to that which Excel would render this number as.
      Returns:
        the value of the cell as a number
      See Also:
        `for turning this number into a string similar to that which Excel would render this number as.`

    - #### 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. See `HSSFDataFormatter` for formatting this date into a string similar to how excel does.
      Returns:
        the value of the cell as a date
      See Also:
        `for formatting this date into a string similar to how excel does.`

    - #### getLocalDateTimeCellValue

          public java.time.LocalDateTime getLocalDateTimeCellValue()

      Get the value of the cell as a LocalDateTime. For strings we throw an exception. For blank cells we return a null. See `HSSFDataFormatter` for formatting this date into a string similar to how excel does.
      Returns:
        the value of the cell as a LocalDateTime
      See Also:
        `for formatting this date into a string similar to how excel does.`

    - #### 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
      Returns:
        the value of the cell as a string

    - #### getRichStringCellValue

          public HSSFRichTextString getRichStringCellValue()

      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 XSSFRichTextString

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

    - #### setCellErrorValue

          public void setCellErrorValue(byte errorCode)

      Deprecated. 3.15 beta 2. Use `setCellErrorValue(FormulaError)` instead.
      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. For error code byte, see `FormulaError`.
      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.

    - #### 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.
      Returns:
        the value of the cell as a boolean

    - #### getErrorCellValue

          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:
        the value of the cell as an error code
      See Also:
        `for error codes`

    - #### setCellStyle

          public void setCellStyle(CellStyle style)

      Set the style for the cell. The style should be an HSSFCellStyle created/retreived from the HSSFWorkbook.

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

      Parameters:
        `style` - reference contained in the workbook
      See Also:
        `HSSFWorkbook.createCellStyle()`, `HSSFWorkbook.getCellStyleAt(int)`

    - #### setCellStyle

          public void setCellStyle(HSSFCellStyle style)

    - #### getCellStyle

          public HSSFCellStyle getCellStyle()

      get the style for the cell. This is a reference to a cell style contained in the workbook object.
      Returns:
        the cell's style. Always not-null. Default cell style has zero index and can be obtained as `workbook.getCellStyleAt(0)`
      See Also:
        `HSSFWorkbook.getCellStyleAt(int)`

    - #### getCellValueRecord

          protected CellValueRecordInterface getCellValueRecord()

      Should only be used by HSSFSheet and friends. Returns the low level CellValueRecordInterface record
      Returns:
        CellValueRecordInterface representing the cell via the low level api.

    - #### setAsActiveCell

          public void setAsActiveCell()

      Sets this cell as the active cell for the worksheet

    - #### toString

          public java.lang.String toString()

      Returns a string representation of the cell This method returns a simple representation, anything more complex should be in user code, with knowledge of the semantics of the sheet being processed. 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`

    - #### 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` - comment associated with this cell

    - #### getCellComment

          public HSSFComment getCellComment()

      Returns comment associated with this cell
      Returns:
        comment associated with this cell

    - #### removeCellComment

          public void removeCellComment()

      Removes the comment for this cell, if there is one. WARNING - some versions of excel will loose all comments after performing this action!

    - #### getHyperlink

          public HSSFHyperlink getHyperlink()

      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` - hyperlink associated with this cell

    - #### removeHyperlink

          public void removeHyperlink()

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

    - #### getCachedFormulaResultType

          public CellType getCachedFormulaResultType()

      Only valid for formula cells
      Returns:
        one of (`CellType.NUMERIC`, `CellType.STRING`, `CellType.BOOLEAN`, `CellType.ERROR`) depending on the cached value of the formula
      Since:
        POI 4.0

    - #### getCachedFormulaResultTypeEnum

          @Deprecated
           @Removal(version="4.2")
          public CellType getCachedFormulaResultTypeEnum()

      Deprecated. use `getCachedFormulaResultType` Will be deleted when we make the CellType enum transition. See bug 59791.
      Only valid for formula cells
      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

    - #### getArrayFormulaRange

          public CellRangeAddress getArrayFormulaRange()

      Description copied from interface: `Cell`
      Only valid for array formula cells
      Returns:
        range of the array formula group that the cell belongs to.

    - #### isPartOfArrayFormulaGroup

          public boolean isPartOfArrayFormulaGroup()

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

Copyright 2020 The Apache Software Foundation or its licensors, as applicable.