in poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFCell.java [273:414]
private void setCellType(CellType cellType, boolean setValue, int row, short col, short styleIndex)
{
switch (cellType)
{
case FORMULA :
FormulaRecordAggregate frec;
if (cellType != _cellType) {
frec = _sheet.getSheet().getRowsAggregate().createFormula(row, col);
} else {
frec = (FormulaRecordAggregate) _record;
frec.setRow(row);
frec.setColumn(col);
}
if (getCellType() == CellType.BLANK) {
frec.getFormulaRecord().setValue(0);
}
frec.setXFIndex(styleIndex);
_record = frec;
break;
case NUMERIC :
NumberRecord nrec;
if (cellType != _cellType)
{
nrec = new NumberRecord();
}
else
{
nrec = ( NumberRecord ) _record;
}
nrec.setColumn(col);
if (setValue)
{
nrec.setValue(getNumericCellValue());
}
nrec.setXFIndex(styleIndex);
nrec.setRow(row);
_record = nrec;
break;
case STRING :
LabelSSTRecord lrec;
if (cellType == _cellType) {
lrec = (LabelSSTRecord) _record;
} else {
lrec = new LabelSSTRecord();
lrec.setColumn(col);
lrec.setRow(row);
lrec.setXFIndex(styleIndex);
}
if (setValue) {
String str = convertCellValueToString();
if(str == null) {
// bug 55668: don't try to store null-string when formula
// results in empty/null value
setCellType(CellType.BLANK, false, row, col, styleIndex);
return;
} else {
int sstIndex = _book.getWorkbook().addSSTString(new UnicodeString(str));
lrec.setSSTIndex(sstIndex);
UnicodeString us = _book.getWorkbook().getSSTString(sstIndex);
_stringValue = new HSSFRichTextString();
_stringValue.setUnicodeString(us);
}
}
_record = lrec;
break;
case BLANK :
BlankRecord brec;
if (cellType != _cellType)
{
brec = new BlankRecord();
}
else
{
brec = ( BlankRecord ) _record;
}
brec.setColumn(col);
// During construction the cellStyle may be null for a Blank cell.
brec.setXFIndex(styleIndex);
brec.setRow(row);
_record = brec;
break;
case BOOLEAN :
BoolErrRecord boolRec;
if (cellType != _cellType)
{
boolRec = new BoolErrRecord();
}
else
{
boolRec = ( BoolErrRecord ) _record;
}
boolRec.setColumn(col);
if (setValue)
{
boolRec.setValue(convertCellValueToBoolean());
}
boolRec.setXFIndex(styleIndex);
boolRec.setRow(row);
_record = boolRec;
break;
case ERROR :
BoolErrRecord errRec;
if (cellType != _cellType)
{
errRec = new BoolErrRecord();
}
else
{
errRec = ( BoolErrRecord ) _record;
}
errRec.setColumn(col);
if (setValue)
{
errRec.setValue(FormulaError.VALUE.getCode());
}
errRec.setXFIndex(styleIndex);
errRec.setRow(row);
_record = errRec;
break;
default :
throw new IllegalStateException("Invalid cell type: " + cellType);
}
if (cellType != _cellType &&
_cellType != CellType._NONE ) // Special Value to indicate an uninitialized Cell
{
_sheet.getSheet().replaceValueRecord(_record);
}
_cellType = cellType;
}