org.apache.poi.hssf.usermodel
Class HSSFCell

java.lang.Object
  extended by org.apache.poi.hssf.usermodel.HSSFCell
All Implemented Interfaces:
Cell

public class HSSFCell
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 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. Only cells that have values should be added.

Author:
Andrew C. Oliver (acoliver at apache dot org), Dan Sherman (dsherman at isisph.com), Brian Sanders (kestrel at burdell dot org) Active Cell support, Yegor Kozlov cell comments support

Field Summary
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
 
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 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, int type)
          Creates new Cell - Should only be called by HSSFRow.
 
Method Summary
protected static HSSFComment findCellComment(InternalSheet sheet, int row, int column)
          Cell comment finder.
 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
 int getCachedFormulaResultType()
          Only valid for formula cells
 HSSFComment getCellComment()
          Returns comment associated with this cell
 java.lang.String getCellFormula()
          Return a formula for the cell, for example, SUM(C4:E4)
 short getCellNum()
          Deprecated. (Oct 2008) use getColumnIndex()
 HSSFCellStyle getCellStyle()
          get the style for the cell.
 int 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()
           
 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
 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.
 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)
          set a error value for the cell
 void setCellFormula(java.lang.String formula)
          Sets formula for this cell.
 void setCellNum(short num)
          Deprecated. (Jan 2008) Doesn't update the row's idea of what cell this is, use HSSFRow.moveCell(HSSFCell, short) instead
 void setCellStyle(CellStyle style)
          set the style for the cell.
 void setCellStyle(HSSFCellStyle style)
           
 void setCellType(int cellType)
          set the cells type (numeric, formula or string)
 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 value)
          Set a string value for the cell.
 void setCellValue(java.lang.String value)
          set a string value for the cell.
 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, anthing 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 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

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 CELL_TYPE_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(short)

HSSFCell

protected HSSFCell(HSSFWorkbook book,
                   HSSFSheet sheet,
                   int row,
                   short col,
                   int 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 - - CELL_TYPE_NUMERIC, CELL_TYPE_STRING, CELL_TYPE_FORMULA, CELL_TYPE_BLANK, CELL_TYPE_BOOLEAN, CELL_TYPE_ERROR Type of cell
See Also:
HSSFRow.createCell(short,int)

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

getSheet

public HSSFSheet getSheet()
Returns the HSSFSheet this cell belongs to

Specified by:
getSheet in interface Cell
Returns:
the HSSFSheet that owns this cell

getRow

public HSSFRow getRow()
Returns the HSSFRow this cell belongs to

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

Specified by:
getRowIndex in interface Cell
Returns:
the (zero based) index of the row containing this cell

setCellNum

public void setCellNum(short num)
Deprecated. (Jan 2008) Doesn't update the row's idea of what cell this is, use HSSFRow.moveCell(HSSFCell, short) instead

Set the cell's number within the row (0 based).

Parameters:
num - short the cell number

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

getCellNum

public short getCellNum()
Deprecated. (Oct 2008) use getColumnIndex()


getColumnIndex

public int getColumnIndex()
Description copied from interface: Cell
Returns column index of this cell

Specified by:
getColumnIndex in interface Cell
Returns:
zero-based column index of a column in a sheet.

setCellType

public void setCellType(int cellType)
set the cells type (numeric, formula or string)

Specified by:
setCellType in interface Cell
See Also:
Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_STRING, Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_BLANK, Cell.CELL_TYPE_BOOLEAN, Cell.CELL_TYPE_ERROR

getCellType

public int getCellType()
get the cells type (numeric, formula or string)

Specified by:
getCellType in interface Cell
Returns:
the cell type
See Also:
Cell.CELL_TYPE_STRING, Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_BOOLEAN, Cell.CELL_TYPE_ERROR

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.

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.

setCellValue

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

Specified by:
setCellValue in interface Cell
Parameters:
value - 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 value)
Set a string value for the cell.

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

setCellFormula

public void setCellFormula(java.lang.String formula)
Description copied from interface: Cell
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 Cell.setCellValue(double) or Cell.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.

getCellFormula

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

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

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

Specified by:
getDateCellValue in interface Cell
Returns:
the value of the cell as a date
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

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

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

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.

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

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

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.

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

Specified by:
setCellStyle in interface Cell
Parameters:
style - reference contained in the workbook
See Also:
HSSFWorkbook.createCellStyle(), HSSFWorkbook.getCellStyleAt(short)

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.

Specified by:
getCellStyle in interface Cell
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(short)

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

Specified by:
setAsActiveCell in interface Cell

toString

public java.lang.String toString()
Returns a string representation of the cell This method returns a simple representation, anthing 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.

Specified by:
setCellComment in interface Cell
Parameters:
comment - comment associated with this cell

getCellComment

public HSSFComment getCellComment()
Returns comment associated with this cell

Specified by:
getCellComment in interface 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!

Specified by:
removeCellComment in interface Cell

findCellComment

protected static HSSFComment findCellComment(InternalSheet sheet,
                                             int row,
                                             int column)
Cell comment finder. Returns cell comment for the specified sheet, row and column.

Returns:
cell comment or null if not found

getHyperlink

public HSSFHyperlink getHyperlink()
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

Specified by:
setHyperlink in interface Cell
Parameters:
hyperlink - hyperlink associated with this cell

getCachedFormulaResultType

public int getCachedFormulaResultType()
Only valid for formula cells

Specified by:
getCachedFormulaResultType in interface Cell
Returns:
one of (Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_STRING, Cell.CELL_TYPE_BOOLEAN, Cell.CELL_TYPE_ERROR) depending on the cached value of the formula

getArrayFormulaRange

public CellRangeAddress getArrayFormulaRange()
Description copied from interface: Cell
Only valid for array formula cells

Specified by:
getArrayFormulaRange in interface Cell
Returns:
range of the array formula group that the cell belongs to.

isPartOfArrayFormulaGroup

public boolean isPartOfArrayFormulaGroup()
Specified by:
isPartOfArrayFormulaGroup in interface Cell
Returns:
true if this cell is part of group of cells having a common array formula.


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