Package org.apache.poi.hssf.usermodel
Class HSSFCell
- java.lang.Object
-
- 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 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 Description static shortENCODING_COMPRESSED_UNICODEstatic shortENCODING_UNCHANGEDstatic shortENCODING_UTF_16static intLAST_COLUMN_NUMBERThe maximum number of columns in BIFF8
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description CellAddressgetAddress()Gets the address of this cellCellRangeAddressgetArrayFormulaRange()Only valid for array formula cellsbooleangetBooleanCellValue()get the value of the cell as a boolean.CellTypegetCachedFormulaResultType()Only valid for formula cellsCellTypegetCachedFormulaResultTypeEnum()Deprecated.usegetCachedFormulaResultTypeWill be deleted when we make the CellType enum transition.HSSFCommentgetCellComment()Returns comment associated with this celljava.lang.StringgetCellFormula()Return a formula for the cell, for example,SUM(C4:E4)HSSFCellStylegetCellStyle()get the style for the cell.CellTypegetCellType()get the cells type (numeric, formula or string)CellTypegetCellTypeEnum()Deprecated.intgetColumnIndex()Returns column index of this celljava.util.DategetDateCellValue()Get the value of the cell as a date.bytegetErrorCellValue()get the value of the cell as an error code.HSSFHyperlinkgetHyperlink()doublegetNumericCellValue()Get the value of the cell as a number.HSSFRichTextStringgetRichStringCellValue()get the value of the cell as a string - for numeric cells we throw an exception.HSSFRowgetRow()Returns the HSSFRow this cell belongs tointgetRowIndex()Returns row index of a row in the sheet that contains this cellHSSFSheetgetSheet()Returns the HSSFSheet this cell belongs tojava.lang.StringgetStringCellValue()get the value of the cell as a string - for numeric cells we throw an exception.booleanisPartOfArrayFormulaGroup()voidremoveCellComment()Removes the comment for this cell, if there is one.voidremoveHyperlink()Removes the hyperlink for this cell, if there is one.voidsetAsActiveCell()Sets this cell as the active cell for the worksheetvoidsetCellComment(Comment comment)Assign a comment to this cell.voidsetCellErrorValue(byte errorCode)Deprecated.3.15 beta 2.voidsetCellErrorValue(FormulaError error)set a error value for the cellvoidsetCellFormula(java.lang.String formula)Sets formula for this cell.voidsetCellStyle(HSSFCellStyle style)voidsetCellStyle(CellStyle style)Set the style for the cell.voidsetCellType(CellType cellType)Set the cells type (numeric, formula or string).voidsetCellValue(boolean value)set a boolean value for the cellvoidsetCellValue(double value)set a numeric value for the cellvoidsetCellValue(java.lang.String value)set a string value for the cell.voidsetCellValue(java.util.Calendar value)set a date value for the cell.voidsetCellValue(java.util.Date value)set a date value for the cell.voidsetCellValue(RichTextString value)Set a string value for the cell.voidsetHyperlink(Hyperlink hyperlink)Assign a hyperlink to this cell.java.lang.StringtoString()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.
-
-
-
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
-
-
Method Detail
-
getSheet
public HSSFSheet getSheet()
Returns the HSSFSheet this cell belongs to
-
getRow
public HSSFRow getRow()
Returns the HSSFRow this cell belongs to
-
getRowIndex
public int getRowIndex()
Description copied from interface:CellReturns row index of a row in the sheet that contains this cell- Specified by:
getRowIndexin interfaceCell- Returns:
- the (zero based) index of the row containing this cell
-
getColumnIndex
public int getColumnIndex()
Description copied from interface:CellReturns column index of this cell- Specified by:
getColumnIndexin interfaceCell- Returns:
- zero-based column index of a column in a sheet.
-
getAddress
public CellAddress getAddress()
Gets the address of this cell- Specified by:
getAddressin interfaceCell- Returns:
A1style address of this cell
-
setCellType
public void setCellType(CellType cellType)
Set the cells type (numeric, formula or string). If the cell currently contains a value, the value will be converted to match the new type, if possible.- Specified by:
setCellTypein interfaceCell
-
getCellType
public CellType getCellType()
get the cells type (numeric, formula or string)- Specified by:
getCellTypein interfaceCell- Returns:
- the cell type
-
getCellTypeEnum
@Deprecated @Removal(version="4.2") public CellType getCellTypeEnum()
Deprecated.get the cells type (numeric, formula or string)- Specified by:
getCellTypeEnumin interfaceCell- Returns:
- the cell type
- Since:
- POI 3.15 beta 3
-
setCellValue
public void setCellValue(double value)
set a numeric value for the cell- Specified by:
setCellValuein 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.
-
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:
setCellValuein 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
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 usingsetCellValue(value.getTime())which will automatically shift the times to the default timezone.- Specified by:
setCellValuein 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.
-
setCellValue
public void setCellValue(java.lang.String value)
set a string value for the cell.- Specified by:
setCellValuein interfaceCell- 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:
setCellValuein interfaceCell- 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 isnullthen we will change the cell to a Blank cell.
-
setCellFormula
public void setCellFormula(java.lang.String formula)
Description copied from interface:CellSets 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)orCell.setCellValue(String)- Specified by:
setCellFormulain interfaceCell- Parameters:
formula- the formula to set, e.g."SUM(C4:E4)". If the argument isnullthen the current formula is removed.
-
getCellFormula
public java.lang.String getCellFormula()
Description copied from interface:CellReturn a formula for the cell, for example,SUM(C4:E4)- Specified by:
getCellFormulain interfaceCell- 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. SeeHSSFDataFormatterfor turning this number into a string similar to that which Excel would render this number as.- Specified by:
getNumericCellValuein interfaceCell- 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. SeeHSSFDataFormatterfor formatting this date into a string similar to how excel does.- Specified by:
getDateCellValuein interfaceCell- 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:
getStringCellValuein interfaceCell- 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:
getRichStringCellValuein interfaceCell- Returns:
- the value of the cell as a XSSFRichTextString
-
setCellValue
public void setCellValue(boolean value)
set a boolean value for the cell- Specified by:
setCellValuein 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.
-
setCellErrorValue
public void setCellErrorValue(byte errorCode)
Deprecated.3.15 beta 2. UsesetCellErrorValue(FormulaError)instead.set a error value for the cell- Specified by:
setCellErrorValuein 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. For error code byte, seeFormulaError.- 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.- Specified by:
getBooleanCellValuein interfaceCell- 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:
getErrorCellValuein interfaceCell- 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)- Specified by:
setCellStylein interfaceCell- 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.- Specified by:
getCellStylein interfaceCell- 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)
-
setAsActiveCell
public void setAsActiveCell()
Sets this cell as the active cell for the worksheet- Specified by:
setAsActiveCellin interfaceCell
-
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:
toStringin classjava.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:
setCellCommentin interfaceCell- Parameters:
comment- comment associated with this cell
-
getCellComment
public HSSFComment getCellComment()
Returns comment associated with this cell- Specified by:
getCellCommentin interfaceCell- 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:
removeCellCommentin interfaceCell
-
getHyperlink
public HSSFHyperlink getHyperlink()
- Specified by:
getHyperlinkin interfaceCell- Returns:
- hyperlink associated with this cell or
nullif 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:
setHyperlinkin interfaceCell- Parameters:
hyperlink- hyperlink associated with this cell
-
removeHyperlink
public void removeHyperlink()
Removes the hyperlink for this cell, if there is one.- Specified by:
removeHyperlinkin interfaceCell
-
getCachedFormulaResultType
public CellType getCachedFormulaResultType()
Only valid for formula cells- Specified by:
getCachedFormulaResultTypein interfaceCell- 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.usegetCachedFormulaResultTypeWill be deleted when we make the CellType enum transition. See bug 59791.Only valid for formula cells- Specified by:
getCachedFormulaResultTypeEnumin 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
-
getArrayFormulaRange
public CellRangeAddress getArrayFormulaRange()
Description copied from interface:CellOnly valid for array formula cells- Specified by:
getArrayFormulaRangein interfaceCell- Returns:
- range of the array formula group that the cell belongs to.
-
isPartOfArrayFormulaGroup
public boolean isPartOfArrayFormulaGroup()
- Specified by:
isPartOfArrayFormulaGroupin interfaceCell- Returns:
trueif this cell is part of group of cells having a common array formula.
-
-