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.
Deprecated.
setCellValue
(java.util.Date value)
Converts the supplied date to its equivalent Excel numeric value and sets
that into the cell.
setCellValue
(double value)
Set a numeric value for the cell.
default void
setCellValue
(java.time.LocalDate value)
Converts the supplied date to its equivalent Excel numeric value and sets
that into the cell.
setCellValue
(java.time.LocalDateTime value)
Converts the supplied date to its equivalent Excel numeric value and sets
that into the cell.
setCellValue
(
RichTextString
value)
Set a rich string value for the cell.
setCellValue
(java.lang.String value)
Set a string value for the cell.
setHyperlink
(
Hyperlink
link)
Assign a hyperlink to this cell
getRowIndex
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
@Removal
(
version
="5.0")
void setCellType(
CellType
cellType)
Deprecated.
Set the cells type (blank, numeric, boolean, error or string).
If the cell currently contains a value, the value will
be converted to match the new type, if possible. Formatting
is generally lost in the process however.
Conversion rules:
to NUMERIC: numeric value is left as is. True converts to 1.0, false converts to 0. otherwise, the
value is set to 0. Formula is removed.
If what you want to do is get a String value for your
numeric cell,
stop!
This is not the way to do it.
Instead, for fetching the string value of a numeric or boolean
or date cell, use
DataFormatter
instead.
If cell is a member of an array formula group containing more than 1 cell, an
IllegalStateException
is thrown. If the array formula group contains only this cell, it is removed.
Passing
CellType.FORMULA
is illegal and will result in an
IllegalArgumentException
.
Throws:
java.lang.IllegalArgumentException
- if the specified cell type is invalid (null, _NONE or FORMULA)
java.lang.IllegalStateException
- if the current value cannot be converted to the new type or
if the cell is a part of an array formula group containing other cells
setBlank
void setBlank()
Removes formula and value from the cell, and sets its type to
CellType.BLANK
.
Preserves comments and hyperlinks.
While
setCellType(CellType)
exists, is an alias for
setCellType(CellType.BLANK)
.
getCachedFormulaResultType
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
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
void setCellValue(java.util.Date value)
Converts the supplied date to its equivalent Excel numeric value and sets
that into the cell.
Note
- There is actually no 'DATE' cell type in Excel. In many
cases (when entering date values), Excel automatically adjusts the
cell style
to some date format, creating the illusion that the cell
data type is now something besides
CellType.NUMERIC
. POI
does not attempt to replicate this behaviour. To make a numeric cell
display as a date, use
setCellStyle(CellStyle)
etc.
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 numerics cell and set its value.
setCellValue
void setCellValue(java.time.LocalDateTime value)
Converts the supplied date to its equivalent Excel numeric value and sets
that into the cell.
Note
- There is actually no 'DATE' cell type in Excel. In many
cases (when entering date values), Excel automatically adjusts the
cell style
to some date format, creating the illusion that the cell
data type is now something besides
CellType.NUMERIC
. POI
does not attempt to replicate this behaviour. To make a numeric cell
display as a date, use
setCellStyle(CellStyle)
etc.
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 numerics cell and set its value.
setCellValue
default void setCellValue(java.time.LocalDate value)
Converts the supplied date to its equivalent Excel numeric value and sets
that into the cell.
Note
- There is actually no 'DATE' cell type in Excel. In many
cases (when entering date values), Excel automatically adjusts the
cell style
to some date format, creating the illusion that the cell
data type is now something besides
CellType.NUMERIC
. POI
does not attempt to replicate this behaviour. To make a numeric cell
display as a date, use
setCellStyle(CellStyle)
etc.
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 numerics cell and set its value.
setCellValue
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.
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
void setCellValue(RichTextString value)
Set a rich string value for the 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.
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
void setCellFormula(java.lang.String formula)
throws FormulaParseException,
java.lang.IllegalStateException
Sets formula for this cell.
If
formula
is not null, sets or updates the formula. If
formula
is null, removes the formula.
Or use
removeFormula()
to remove the formula.
Note, this method only sets the formula string and does not calculate the formula value.
To set the precalculated value use
setCellValue(double)
.
If the cell was blank, sets value to 0. Otherwise, preserves the value as precalculated.
In XSSF implementation, there is a
XSSFWorkbook.setCellFormulaValidation(boolean)
setting that can validate (but also in some cases, reformat) the formula.
Parameters:
formula
- the formula to set, e.g.
"SUM(C4:E4)"
.
If the argument is
null
then the current formula is removed.
Throws:
java.lang.IllegalStateException
- if this cell is a part of an array formula group containing other cells
FormulaParseException
- if the formula has incorrect syntax or is otherwise invalid
See Also:
removeFormula()
,
Workbook.setForceFormulaRecalculation(boolean)
If cell was blank, leaves it as is.
If it is a part of an array formula group, blanks the cell.
If has a regular formula, removes the formula preserving the "cached" value.
Throws:
java.lang.IllegalStateException
- if cell is a part of an array formula group containing other cells
getCellFormula
java.lang.String getCellFormula()
Return a formula for the cell, for example,
SUM(C4:E4)
Returns:
a formula for the cell
Throws:
java.lang.IllegalStateException
- if the cell type returned by
getCellType()
is not
CellType.FORMULA
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;
Returns:
the value of the cell as a number
Throws:
java.lang.IllegalStateException
- if the cell type returned by
getCellType()
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.
Get the value of the cell as a date.
For strings we throw an exception. For blank cells we return a null.
Returns:
the value of the cell as a date
Throws:
java.lang.IllegalStateException
- if the cell type returned by
getCellType()
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.
getLocalDateTimeCellValue
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.
Returns:
the value of the cell as a LocalDateTime
Throws:
java.lang.IllegalStateException
- if the cell type returned by
getCellType()
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.
getRichStringCellValue
RichTextString 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
getStringCellValue
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
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.
Parameters:
value
- 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
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
Throws:
java.lang.IllegalStateException
- if the cell type returned by
getCellType()
is not
CellType.BOOLEAN
,
CellType.BLANK
or
CellType.FORMULA
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:
java.lang.IllegalStateException
- if the cell type returned by
getCellType()
isn't
CellType.ERROR
See Also:
for error codes
setCellStyle
void setCellStyle(CellStyle style)
Set the style for the cell. The style should be an CellStyle created/retrieved from
the Workbook.
To change the style of a cell without affecting other cells that use the same style,
use
CellUtil.setCellStyleProperties(Cell, 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.
See Also:
Workbook.createCellStyle()
Returns:
the cell's style. Always not-null. Default cell style has zero index and can be obtained as
workbook.getCellStyleAt(0)
See Also:
Workbook.getCellStyleAt(int)
getArrayFormulaRange
CellRangeAddress getArrayFormulaRange()
Only valid for array formula cells
Returns:
range of the array formula group that the cell belongs to.