use of org.apache.poi.ss.usermodel.CellType in project poi by apache.
the class SXSSFCell method getRichStringCellValue.
/**
* Get the value of the cell as a XSSFRichTextString
* <p>
* 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.
* </p>
* @return the value of the cell as a XSSFRichTextString
*/
@Override
public RichTextString getRichStringCellValue() {
CellType cellType = getCellTypeEnum();
if (getCellTypeEnum() != CellType.STRING)
throw typeMismatch(CellType.STRING, cellType, false);
StringValue sval = (StringValue) _value;
if (sval.isRichText())
return ((RichTextValue) _value).getValue();
else {
String plainText = getStringCellValue();
return getSheet().getWorkbook().getCreationHelper().createRichTextString(plainText);
}
}
use of org.apache.poi.ss.usermodel.CellType in project poi by apache.
the class XSSFExcelExtractor method handleNonStringCell.
private void handleNonStringCell(StringBuffer text, Cell cell, DataFormatter formatter) {
CellType type = cell.getCellTypeEnum();
if (type == CellType.FORMULA) {
type = cell.getCachedFormulaResultTypeEnum();
}
if (type == CellType.NUMERIC) {
CellStyle cs = cell.getCellStyle();
if (cs != null && cs.getDataFormatString() != null) {
String contents = formatter.formatRawCellContents(cell.getNumericCellValue(), cs.getDataFormat(), cs.getDataFormatString());
checkMaxTextSize(text, contents);
text.append(contents);
return;
}
}
// No supported styling applies to this cell
String contents = ((XSSFCell) cell).getRawValue();
if (contents != null) {
checkMaxTextSize(text, contents);
text.append(contents);
}
}
use of org.apache.poi.ss.usermodel.CellType in project poi by apache.
the class XSSFCell method setCellValue.
/**
* Set a string value for the cell.
*
* @param str value to set the cell to. For formulas we'll set the 'pre-evaluated result 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.
*/
@Override
public void setCellValue(RichTextString str) {
if (str == null || str.getString() == null) {
setCellType(CellType.BLANK);
return;
}
if (str.length() > SpreadsheetVersion.EXCEL2007.getMaxTextLength()) {
throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters");
}
CellType cellType = getCellTypeEnum();
switch(cellType) {
case FORMULA:
_cell.setV(str.getString());
_cell.setT(STCellType.STR);
break;
default:
if (_cell.getT() == STCellType.INLINE_STR) {
//set the 'pre-evaluated result
_cell.setV(str.getString());
} else {
_cell.setT(STCellType.S);
XSSFRichTextString rt = (XSSFRichTextString) str;
rt.setStylesTableReference(_stylesSource);
int sRef = _sharedStringSource.addEntry(rt.getCTRst());
_cell.setV(Integer.toString(sRef));
}
break;
}
}
use of org.apache.poi.ss.usermodel.CellType in project poi by apache.
the class XSSFCell method convertCellValueToBoolean.
/**
* Chooses a new boolean value for the cell when its type is changing.<p/>
*
* Usually the caller is calling setCellType() with the intention of calling
* setCellValue(boolean) straight afterwards. This method only exists to give
* the cell a somewhat reasonable value until the setCellValue() call (if at all).
* TODO - perhaps a method like setCellTypeAndValue(int, Object) should be introduced to avoid this
*/
private boolean convertCellValueToBoolean() {
CellType cellType = getCellTypeEnum();
if (cellType == CellType.FORMULA) {
cellType = getBaseCellType(false);
}
switch(cellType) {
case BOOLEAN:
return TRUE_AS_STRING.equals(_cell.getV());
case STRING:
int sstIndex = Integer.parseInt(_cell.getV());
XSSFRichTextString rt = new XSSFRichTextString(_sharedStringSource.getEntryAt(sstIndex));
String text = rt.getString();
return Boolean.parseBoolean(text);
case NUMERIC:
return Double.parseDouble(_cell.getV()) != 0;
case ERROR:
// fall-through
case BLANK:
return false;
default:
throw new RuntimeException("Unexpected cell type (" + cellType + ")");
}
}
use of org.apache.poi.ss.usermodel.CellType in project poi by apache.
the class XSSFCell method convertCellValueToString.
private String convertCellValueToString() {
CellType cellType = getCellTypeEnum();
switch(cellType) {
case BLANK:
return "";
case BOOLEAN:
return TRUE_AS_STRING.equals(_cell.getV()) ? TRUE : FALSE;
case STRING:
int sstIndex = Integer.parseInt(_cell.getV());
XSSFRichTextString rt = new XSSFRichTextString(_sharedStringSource.getEntryAt(sstIndex));
return rt.getString();
case NUMERIC:
case ERROR:
return _cell.getV();
case FORMULA:
// just use cached formula result instead
break;
default:
throw new IllegalStateException("Unexpected cell type (" + cellType + ")");
}
cellType = getBaseCellType(false);
String textValue = _cell.getV();
switch(cellType) {
case BOOLEAN:
if (TRUE_AS_STRING.equals(textValue)) {
return TRUE;
}
if (FALSE_AS_STRING.equals(textValue)) {
return FALSE;
}
throw new IllegalStateException("Unexpected boolean cached formula value '" + textValue + "'.");
case STRING:
// fall-through
case NUMERIC:
// fall-through
case ERROR:
return textValue;
default:
throw new IllegalStateException("Unexpected formula result type (" + cellType + ")");
}
}
Aggregations