public interface Cell
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.
(电子表格行中单元格的高级表示。单元格可以是数字、基于公式或基于字符串(文本)。单元格类型指定了这一点。字符串单元格不能包含数字,数字单元格不能包含字符串(至少根据我们的模型)。客户端应用程序应该自己进行转换。公式单元格具有公式字符串以及公式结果,可以是数字或字符串。单元格在添加到一行之前应该有它们的编号(从 0 开始)。)Modifier and Type | Method and Description |
---|---|
CellAddress |
getAddress()
Gets the address of this cell
(获取此单元格的地址)
|
CellRangeAddress |
getArrayFormulaRange()
Only valid for array formula cells
(仅对数组公式单元格有效)
|
boolean |
getBooleanCellValue()
Get the value of the cell as a boolean.
(获取单元格的值作为布尔值。)
|
CellType |
getCachedFormulaResultType()
Only valid for formula cells
(仅对公式单元格有效)
|
Comment |
getCellComment()
Returns comment associated with this cell
(返回与此单元格关联的评论)
|
java.lang.String |
getCellFormula()
Return a formula for the cell, for example,
SUM(C4:E4)
(返回单元格的公式,例如 SUM(C4:E4))
|
CellStyle |
getCellStyle()
Return the cell's style.
(返回单元格的样式。)
|
CellType |
getCellType()
Return the cell type.
(返回单元格类型。)
|
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.
(获取单元格的值作为错误代码。)
|
Hyperlink |
getHyperlink() |
java.time.LocalDateTime |
getLocalDateTimeCellValue()
Get the value of the cell as a LocalDateTime.
(获取单元格的值作为 LocalDateTime。)
|
double |
getNumericCellValue()
Get the value of the cell as a number.
(以数字形式获取单元格的值。)
|
RichTextString |
getRichStringCellValue()
Get the value of the cell as a XSSFRichTextString
(获取单元格的值作为 XSSFRichTextString)
|
Row |
getRow()
Returns the Row this cell belongs to
(返回此单元格所属的行)
|
int |
getRowIndex()
Returns row index of a row in the sheet that contains this cell
(返回工作表中包含此单元格的行的行索引)
|
Sheet |
getSheet()
Returns the sheet this cell belongs to
(返回此单元格所属的工作表)
|
java.lang.String |
getStringCellValue()
Get the value of the cell as a string
(以字符串形式获取单元格的值)
|
boolean |
isPartOfArrayFormulaGroup() |
void |
removeCellComment()
Removes the comment for this cell, if there is one.
(删除此单元格的注释(如果有)。)
|
void |
removeFormula()
Removes formula, if any.
(删除公式(如果有)。)
|
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 |
setBlank()
Removes formula and value from the cell, and sets its type to
CellType.BLANK .
(从单元格中删除公式和值,并将其类型设置为 CellType.BLANK。)
|
void |
setCellComment(Comment comment)
Assign a comment to this cell
(为该单元格分配注释)
|
void |
setCellErrorValue(byte value)
Set a error value for the cell
(为单元格设置错误值)
|
void |
setCellFormula(java.lang.String formula)
Sets formula for this cell.
(设置此单元格的公式。)
|
void |
setCellStyle(CellStyle style)
Set the style for the cell.
(设置单元格的样式。)
|
void |
setCellType(CellType cellType)
Deprecated.
This method is deprecated and will be removed in POI 5.0. Use explicit
(已弃用。此方法已弃用,将在 POI 5.0 中删除。使用显式 setCellFormula(String)、setCellValue(...) 或 setBlank() 来获得所需的结果。)
setCellFormula(String) , setCellValue(...) or setBlank() to get the desired result.
|
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)
Converts the supplied date to its equivalent Excel numeric value and sets that into the cell.
(将提供的日期转换为其等效的 Excel 数值并将其设置到单元格中。)
|
void |
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.
(将提供的日期转换为其等效的 Excel 数值并将其设置到单元格中。)
|
void |
setCellValue(java.time.LocalDateTime value)
Converts the supplied date to its equivalent Excel numeric value and sets that into the cell.
(将提供的日期转换为其等效的 Excel 数值并将其设置到单元格中。)
|
void |
setCellValue(RichTextString value)
Set a rich string value for the cell.
(为单元格设置丰富的字符串值。)
|
void |
setCellValue(java.lang.String value)
Set a string value for the cell.
(为单元格设置一个字符串值。)
|
void |
setHyperlink(Hyperlink link)
Assign a hyperlink to this cell
(为该单元格分配超链接)
|
int getColumnIndex()
int getRowIndex()
Sheet getSheet()
Row getRow()
@Deprecated @Removal(version="5.0") void setCellType(CellType cellType)
setCellFormula(String)
, setCellValue(...)
or setBlank()
to get the desired result.
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
.
java.lang.IllegalArgumentException
- if the specified cell type is invalid (null, _NONE or FORMULA)
(java.lang.IllegalArgumentException - 如果指定的单元格类型无效(null、_NONE 或 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
(java.lang.IllegalStateException - 如果当前值无法转换为新类型或单元格是包含其他单元格的数组公式组的一部分)
void setBlank()
CellType.BLANK
. Preserves comments and hyperlinks. While
setCellType(CellType)
exists, is an alias for
setCellType(CellType.BLANK)
.
(从单元格中删除公式和值,并将其类型设置为 CellType.BLANK。保留评论和超链接。虽然 setCellType(CellType) 存在,但它是 setCellType(CellType.BLANK) 的别名。)
CellType getCellType()
CellType getCachedFormulaResultType()
CellType.NUMERIC
,
CellType.STRING
,
CellType.BOOLEAN
,
CellType.ERROR
) depending on the cached value of the formula
((CellType.NUMERIC, CellType.STRING, CellType.BOOLEAN, CellType.ERROR) 之一,具体取决于公式的缓存值)
void setCellValue(double value)
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.
(value - 将此单元格设置为的数值。对于公式,我们将设置预先计算的值,对于数字,我们将设置其值。对于其他类型,我们会将单元格更改为数字单元格并设置其值。)
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.
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.
(value - 将此单元格设置为的数值。对于公式,我们将设置预先计算的值,对于数字,我们将设置其值。对于其他类型,我们会将单元格更改为数字单元格并设置其值。)
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.
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.
(value - 将此单元格设置为的数值。对于公式,我们将设置预先计算的值,对于数字,我们将设置其值。对于其他类型,我们会将单元格更改为数字单元格并设置其值。)
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.
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.
(value - 将此单元格设置为的数值。对于公式,我们将设置预先计算的值,对于数字,我们将设置其值。对于其他类型,我们会将单元格更改为数字单元格并设置其值。)
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.
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.
(value - 将此单元格设置为的日期值。对于公式,我们将设置预先计算的值,对于数字,我们将设置其值。对于其他类型,我们会将单元格更改为数字单元格并设置其值。)
void setCellValue(RichTextString value)
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.
(value - 将单元格设置为的值。对于公式,我们将设置公式字符串,对于字符串单元格,我们将设置其值。对于其他类型,我们会将单元格更改为字符串单元格并设置其值。如果值为 null,那么我们会将单元格更改为空白单元格。)
void setCellValue(java.lang.String value)
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.
(value - 将单元格设置为的值。对于公式,我们将设置公式字符串,对于字符串单元格,我们将设置其值。对于其他类型,我们会将单元格更改为字符串单元格并设置其值。如果值为 null,那么我们会将单元格更改为空白单元格。)
void setCellFormula(java.lang.String formula) throws FormulaParseException, java.lang.IllegalStateException
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.
(设置此单元格的公式。如果公式不为空,则设置或更新公式。如果公式为空,则删除公式。或使用 removeFormula() 删除公式。注意,此方法只设置公式字符串,不计算公式值。要设置预先计算的值,请使用 setCellValue(double)。如果单元格为空白,则将值设置为 0。否则,将值保留为预先计算的值。)formula
- the formula to set, e.g.
"SUM(C4:E4)"
. If the argument is
null
then the current formula is removed.
(公式 - 要设置的公式,例如“SUM(C4:E4)”。如果参数为空,则删除当前公式。)
java.lang.IllegalStateException
- if this cell is a part of an array formula group containing other cells
(java.lang.IllegalStateException - 如果此单元格是包含其他单元格的数组公式组的一部分)
FormulaParseException
- if the formula has incorrect syntax or is otherwise invalid
(FormulaParseException - 如果公式的语法不正确或无效)
removeFormula()
void removeFormula() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- if cell is a part of an array formula group containing other cells
(java.lang.IllegalStateException - 如果单元格是包含其他单元格的数组公式组的一部分)
java.lang.String getCellFormula()
SUM(C4:E4)
(返回单元格的公式,例如 SUM(C4:E4))
java.lang.IllegalStateException
- if the cell type returned by
getCellType()
is not
CellType.FORMULA
(java.lang.IllegalStateException - 如果 getCellType() 返回的单元格类型不是 CellType.FORMULA)
double getNumericCellValue()
For strings we throw an exception. For blank cells we return a 0. For formulas or error cells we return the precalculated value;
(以数字形式获取单元格的值。对于字符串,我们抛出异常。对于空白单元格,我们返回 0。对于公式或错误单元格,我们返回预先计算的值;)java.lang.IllegalStateException
- if the cell type returned by
getCellType()
is
CellType.STRING
(java.lang.IllegalStateException - 如果 getCellType() 返回的单元格类型是 CellType.STRING)
java.lang.NumberFormatException
- if the cell value isn't a parsable
double
.
(java.lang.NumberFormatException - 如果单元格值不是可解析的双精度值。)
for turning this number into a string similar to that which Excel would render this number as.
java.util.Date getDateCellValue()
For strings we throw an exception. For blank cells we return a null.
(获取单元格的值作为日期。对于字符串,我们抛出异常。对于空白单元格,我们返回 null。)java.lang.IllegalStateException
- if the cell type returned by
getCellType()
is
CellType.STRING
(java.lang.IllegalStateException - 如果 getCellType() 返回的单元格类型是 CellType.STRING)
java.lang.NumberFormatException
- if the cell value isn't a parsable
double
.
(java.lang.NumberFormatException - 如果单元格值不是可解析的双精度值。)
for formatting this date into a string similar to how excel does.
java.time.LocalDateTime getLocalDateTimeCellValue()
For strings we throw an exception. For blank cells we return a null.
(获取单元格的值作为 LocalDateTime。对于字符串,我们抛出异常。对于空白单元格,我们返回 null。)java.lang.IllegalStateException
- if the cell type returned by
getCellType()
is
CellType.STRING
(java.lang.IllegalStateException - 如果 getCellType() 返回的单元格类型是 CellType.STRING)
java.lang.NumberFormatException
- if the cell value isn't a parsable
double
.
(java.lang.NumberFormatException - 如果单元格值不是可解析的双精度值。)
for formatting this date into a string similar to how excel does.
RichTextString getRichStringCellValue()
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.
(获取单元格的值作为 XSSFRichTextString 对于数字单元格,我们抛出异常。对于空白单元格,我们返回一个空字符串。对于公式单元格,如果是字符串,我们将返回预先计算的值,否则返回异常。)java.lang.String getStringCellValue()
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.
(将单元格的值作为字符串获取 对于数字单元格,我们会抛出异常。对于空白单元格,我们返回一个空字符串。对于不是字符串公式的公式单元格,我们会抛出异常。)void setCellValue(boolean value)
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.
(value - 将此单元格设置为的布尔值。对于公式,我们将设置预先计算的值,对于布尔值,我们将设置它的值。对于其他类型,我们会将单元格更改为布尔单元格并设置其值。)
void setCellErrorValue(byte value)
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.
(value - 将此单元格设置为的错误值。对于公式,我们将设置预先计算的值,对于错误,我们将设置其值。对于其他类型,我们会将单元格更改为错误单元格并设置其值。)
FormulaError
boolean getBooleanCellValue()
For strings, numbers, and errors, we throw an exception. For blank cells we return a false.
(获取单元格的值作为布尔值。对于字符串、数字和错误,我们会抛出异常。对于空白单元格,我们返回 false。)java.lang.IllegalStateException
- if the cell type returned by
getCellType()
is not
CellType.BOOLEAN
,
CellType.BLANK
or
CellType.FORMULA
(java.lang.IllegalStateException - 如果 getCellType() 返回的单元格类型不是 CellType.BOOLEAN、CellType.BLANK 或 CellType.FORMULA)
byte getErrorCellValue()
For strings, numbers, and booleans, we throw an exception. For blank cells we return a 0.
(获取单元格的值作为错误代码。对于字符串、数字和布尔值,我们会抛出异常。对于空白单元格,我们返回 0。)java.lang.IllegalStateException
- if the cell type returned by
getCellType()
isn't
CellType.ERROR
(java.lang.IllegalStateException - 如果 getCellType() 返回的单元格类型不是 CellType.ERROR)
for error codes
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)
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.
(样式 - 工作簿中包含的参考。如果值为 null,则删除样式信息,导致单元格使用默认工作簿样式。)
Workbook.createCellStyle()
CellStyle getCellStyle()
workbook.getCellStyleAt(0)
(单元格的样式。始终不为空。默认单元格样式的索引为零,可以通过 workbook.getCellStyleAt(0) 获得)
Workbook.getCellStyleAt(int)
void setAsActiveCell()
CellAddress getAddress()
A1
style address of this cell
(此单元格的 A1 样式地址)
void setCellComment(Comment comment)
comment
- comment associated with this cell
(评论 - 与此单元格关联的评论)
Comment getCellComment()
null
if not found
(与此单元格关联的注释,如果未找到,则返回 null)
void removeCellComment()
Hyperlink getHyperlink()
null
if not found
(与此单元格关联的超链接,如果未找到,则为 null)
void setHyperlink(Hyperlink link)
link
- hyperlink associated with this cell
(链接 - 与此单元格关联的超链接)
void removeHyperlink()
CellRangeAddress getArrayFormulaRange()
boolean isPartOfArrayFormulaGroup()
true
if this cell is part of group of cells having a common array formula.
(如果此单元格是具有公共数组公式的单元格组的一部分,则为 true。)
Copyright 2021 The Apache Software Foundation or its licensors, as applicable.