Search in sources :

Example 1 with CellType

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);
    }
}
Also used : CellType(org.apache.poi.ss.usermodel.CellType) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) RichTextString(org.apache.poi.ss.usermodel.RichTextString)

Example 2 with CellType

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);
    }
}
Also used : CellType(org.apache.poi.ss.usermodel.CellType) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) CellStyle(org.apache.poi.ss.usermodel.CellStyle)

Example 3 with CellType

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;
    }
}
Also used : CellType(org.apache.poi.ss.usermodel.CellType) STCellType(org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType)

Example 4 with CellType

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 + ")");
    }
}
Also used : CellType(org.apache.poi.ss.usermodel.CellType) STCellType(org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType) RichTextString(org.apache.poi.ss.usermodel.RichTextString)

Example 5 with 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 + ")");
    }
}
Also used : CellType(org.apache.poi.ss.usermodel.CellType) STCellType(org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType) RichTextString(org.apache.poi.ss.usermodel.RichTextString)

Aggregations

CellType (org.apache.poi.ss.usermodel.CellType)32 Test (org.junit.Test)17 STCellType (org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType)7 RichTextString (org.apache.poi.ss.usermodel.RichTextString)5 Cell (org.apache.poi.ss.usermodel.Cell)4 CellValue (org.apache.poi.ss.usermodel.CellValue)4 Row (org.apache.poi.ss.usermodel.Row)4 Ignore (org.junit.Ignore)4 CellStyle (org.apache.poi.ss.usermodel.CellStyle)3 HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)2 Workbook (org.apache.poi.ss.usermodel.Workbook)2 XSSFRichTextString (org.apache.poi.xssf.usermodel.XSSFRichTextString)2 CTCellFormula (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula)2 AttributedString (java.text.AttributedString)1 AssertionFailedError (junit.framework.AssertionFailedError)1 HSSFFormulaEvaluator (org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator)1 HSSFRow (org.apache.poi.hssf.usermodel.HSSFRow)1 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)1 CellReference (org.apache.poi.hssf.util.CellReference)1 Font (org.apache.poi.ss.usermodel.Font)1