Search in sources :

Example 6 with CellType

use of org.apache.poi.ss.usermodel.CellType in project poi by apache.

the class XSSFCell 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 XSSFRichTextString getRichStringCellValue() {
    CellType cellType = getCellTypeEnum();
    XSSFRichTextString rt;
    switch(cellType) {
        case BLANK:
            rt = new XSSFRichTextString("");
            break;
        case STRING:
            if (_cell.getT() == STCellType.INLINE_STR) {
                if (_cell.isSetIs()) {
                    //string is expressed directly in the cell definition instead of implementing the shared string table.
                    rt = new XSSFRichTextString(_cell.getIs());
                } else if (_cell.isSetV()) {
                    //cached result of a formula
                    rt = new XSSFRichTextString(_cell.getV());
                } else {
                    rt = new XSSFRichTextString("");
                }
            } else if (_cell.getT() == STCellType.STR) {
                //cached formula value
                rt = new XSSFRichTextString(_cell.isSetV() ? _cell.getV() : "");
            } else {
                if (_cell.isSetV()) {
                    int idx = Integer.parseInt(_cell.getV());
                    rt = new XSSFRichTextString(_sharedStringSource.getEntryAt(idx));
                } else {
                    rt = new XSSFRichTextString("");
                }
            }
            break;
        case FORMULA:
            checkFormulaCachedValueType(CellType.STRING, getBaseCellType(false));
            rt = new XSSFRichTextString(_cell.isSetV() ? _cell.getV() : "");
            break;
        default:
            throw typeMismatch(CellType.STRING, cellType, false);
    }
    rt.setStylesTableReference(_stylesSource);
    return rt;
}
Also used : CellType(org.apache.poi.ss.usermodel.CellType) STCellType(org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType)

Example 7 with CellType

use of org.apache.poi.ss.usermodel.CellType in project poi by apache.

the class XSSFCell method setCellType.

/**
     * Set the cells type (numeric, formula or string)
     *
     * @throws IllegalArgumentException if the specified cell type is invalid
     */
@Override
public void setCellType(CellType cellType) {
    CellType prevType = getCellTypeEnum();
    if (isPartOfArrayFormulaGroup()) {
        notifyArrayFormulaChanging();
    }
    if (prevType == CellType.FORMULA && cellType != CellType.FORMULA) {
        getSheet().getWorkbook().onDeleteFormula(this);
    }
    switch(cellType) {
        case NUMERIC:
            _cell.setT(STCellType.N);
            break;
        case STRING:
            if (prevType != CellType.STRING) {
                String str = convertCellValueToString();
                XSSFRichTextString rt = new XSSFRichTextString(str);
                rt.setStylesTableReference(_stylesSource);
                int sRef = _sharedStringSource.addEntry(rt.getCTRst());
                _cell.setV(Integer.toString(sRef));
            }
            _cell.setT(STCellType.S);
            break;
        case FORMULA:
            if (!_cell.isSetF()) {
                CTCellFormula f = CTCellFormula.Factory.newInstance();
                f.setStringValue("0");
                _cell.setF(f);
                if (_cell.isSetT()) {
                    _cell.unsetT();
                }
            }
            break;
        case BLANK:
            setBlank();
            break;
        case BOOLEAN:
            String newVal = convertCellValueToBoolean() ? TRUE_AS_STRING : FALSE_AS_STRING;
            _cell.setT(STCellType.B);
            _cell.setV(newVal);
            break;
        case ERROR:
            _cell.setT(STCellType.E);
            break;
        default:
            throw new IllegalArgumentException("Illegal cell type: " + cellType);
    }
    if (cellType != CellType.FORMULA && _cell.isSetF()) {
        _cell.unsetF();
    }
}
Also used : CellType(org.apache.poi.ss.usermodel.CellType) STCellType(org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType) RichTextString(org.apache.poi.ss.usermodel.RichTextString) CTCellFormula(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula)

Example 8 with CellType

use of org.apache.poi.ss.usermodel.CellType in project poi by apache.

the class SheetDataWriter method writeCell.

public void writeCell(int columnIndex, Cell cell) throws IOException {
    if (cell == null) {
        return;
    }
    String ref = new CellReference(_rownum, columnIndex).formatAsString();
    _out.write("<c r=\"" + ref + "\"");
    CellStyle cellStyle = cell.getCellStyle();
    if (cellStyle.getIndex() != 0) {
        // need to convert the short to unsigned short as the indexes can be up to 64k
        // ideally we would use int for this index, but that would need changes to some more 
        // APIs
        _out.write(" s=\"" + (cellStyle.getIndex() & 0xffff) + "\"");
    }
    CellType cellType = cell.getCellTypeEnum();
    switch(cellType) {
        case BLANK:
            {
                _out.write(">");
                break;
            }
        case FORMULA:
            {
                _out.write(">");
                _out.write("<f>");
                outputQuotedString(cell.getCellFormula());
                _out.write("</f>");
                switch(cell.getCachedFormulaResultTypeEnum()) {
                    case NUMERIC:
                        double nval = cell.getNumericCellValue();
                        if (!Double.isNaN(nval)) {
                            _out.write("<v>" + nval + "</v>");
                        }
                        break;
                    default:
                        break;
                }
                break;
            }
        case STRING:
            {
                if (_sharedStringSource != null) {
                    XSSFRichTextString rt = new XSSFRichTextString(cell.getStringCellValue());
                    int sRef = _sharedStringSource.addEntry(rt.getCTRst());
                    _out.write(" t=\"" + STCellType.S + "\">");
                    _out.write("<v>");
                    _out.write(String.valueOf(sRef));
                    _out.write("</v>");
                } else {
                    _out.write(" t=\"inlineStr\">");
                    _out.write("<is><t");
                    if (hasLeadingTrailingSpaces(cell.getStringCellValue())) {
                        _out.write(" xml:space=\"preserve\"");
                    }
                    _out.write(">");
                    outputQuotedString(cell.getStringCellValue());
                    _out.write("</t></is>");
                }
                break;
            }
        case NUMERIC:
            {
                _out.write(" t=\"n\">");
                _out.write("<v>" + cell.getNumericCellValue() + "</v>");
                break;
            }
        case BOOLEAN:
            {
                _out.write(" t=\"b\">");
                _out.write("<v>" + (cell.getBooleanCellValue() ? "1" : "0") + "</v>");
                break;
            }
        case ERROR:
            {
                FormulaError error = FormulaError.forInt(cell.getErrorCellValue());
                _out.write(" t=\"e\">");
                _out.write("<v>" + error.getString() + "</v>");
                break;
            }
        default:
            {
                throw new IllegalStateException("Invalid cell type: " + cellType);
            }
    }
    _out.write("</c>");
}
Also used : XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) FormulaError(org.apache.poi.ss.usermodel.FormulaError) CellType(org.apache.poi.ss.usermodel.CellType) STCellType(org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) CellStyle(org.apache.poi.ss.usermodel.CellStyle) CellReference(org.apache.poi.ss.util.CellReference)

Example 9 with CellType

use of org.apache.poi.ss.usermodel.CellType in project poi by apache.

the class ExcelComparator method compareDataInCell.

private void compareDataInCell(Locator loc1, Locator loc2) {
    if (isCellTypeMatches(loc1, loc2)) {
        final CellType loc1cellType = loc1.cell.getCellTypeEnum();
        switch(loc1cellType) {
            case BLANK:
            case STRING:
            case ERROR:
                isCellContentMatches(loc1, loc2);
                break;
            case BOOLEAN:
                isCellContentMatchesForBoolean(loc1, loc2);
                break;
            case FORMULA:
                isCellContentMatchesForFormula(loc1, loc2);
                break;
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(loc1.cell)) {
                    isCellContentMatchesForDate(loc1, loc2);
                } else {
                    isCellContentMatchesForNumeric(loc1, loc2);
                }
                break;
            default:
                throw new IllegalStateException("Unexpected cell type: " + loc1cellType);
        }
    }
    isCellFillPatternMatches(loc1, loc2);
    isCellAlignmentMatches(loc1, loc2);
    isCellHiddenMatches(loc1, loc2);
    isCellLockedMatches(loc1, loc2);
    isCellFontFamilyMatches(loc1, loc2);
    isCellFontSizeMatches(loc1, loc2);
    isCellFontBoldMatches(loc1, loc2);
    isCellUnderLineMatches(loc1, loc2);
    isCellFontItalicsMatches(loc1, loc2);
    isCellBorderMatches(loc1, loc2, 't');
    isCellBorderMatches(loc1, loc2, 'l');
    isCellBorderMatches(loc1, loc2, 'b');
    isCellBorderMatches(loc1, loc2, 'r');
    isCellFillBackGroundMatches(loc1, loc2);
}
Also used : CellType(org.apache.poi.ss.usermodel.CellType)

Example 10 with CellType

use of org.apache.poi.ss.usermodel.CellType in project poi by apache.

the class ExcelComparator method isCellTypeMatches.

/**
     * Checks if cell type matches.
     */
private boolean isCellTypeMatches(Locator loc1, Locator loc2) {
    CellType type1 = loc1.cell.getCellTypeEnum();
    CellType type2 = loc2.cell.getCellTypeEnum();
    if (type1 == type2)
        return true;
    addMessage(loc1, loc2, "Cell Data-Type does not Match in :: ", type1.name(), type2.name());
    return false;
}
Also used : CellType(org.apache.poi.ss.usermodel.CellType)

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