Example usage for org.apache.poi.ss.usermodel Cell getCellFormula

List of usage examples for org.apache.poi.ss.usermodel Cell getCellFormula

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Cell getCellFormula.

Prototype

String getCellFormula();

Source Link

Document

Return a formula for the cell, for example, SUM(C4:E4)

Usage

From source file:org.bbreak.excella.core.test.util.TestUtil.java

License:Open Source License

private static Object getCellValue(Cell cell) {
    String value = null;/* www  . j  a  v  a 2s.c  o  m*/

    if (cell != null) {
        switch (cell.getCellTypeEnum()) {
        case BLANK:
            value = cell.getStringCellValue();
            break;
        case BOOLEAN:
            value = String.valueOf(cell.getBooleanCellValue());
            break;
        case ERROR:
            value = String.valueOf(cell.getErrorCellValue());
            break;
        case NUMERIC:
            value = String.valueOf(cell.getNumericCellValue());
            break;
        case STRING:
            value = cell.getStringCellValue();
            break;
        case FORMULA:
            value = cell.getCellFormula();
        default:
            value = "";
        }
    }
    return value;
}

From source file:org.bbreak.excella.core.util.PoiUtil.java

License:Open Source License

/**
 * ?//  w  ww.java 2  s.  c o  m
 * 
 * @param fromCell 
 * @param toCell 
 */
public static void copyCell(Cell fromCell, Cell toCell) {

    if (fromCell != null) {

        // 
        CellType cellType = fromCell.getCellTypeEnum();
        switch (cellType) {
        case BLANK:
            break;
        case FORMULA:
            String cellFormula = fromCell.getCellFormula();
            toCell.setCellFormula(cellFormula);
            break;
        case BOOLEAN:
            toCell.setCellValue(fromCell.getBooleanCellValue());
            break;
        case ERROR:
            toCell.setCellErrorValue(fromCell.getErrorCellValue());
            break;
        case NUMERIC:
            toCell.setCellValue(fromCell.getNumericCellValue());
            break;
        case STRING:
            toCell.setCellValue(fromCell.getRichStringCellValue());
            break;
        default:
        }

        // 
        if (fromCell.getCellStyle() != null
                && fromCell.getSheet().getWorkbook().equals(toCell.getSheet().getWorkbook())) {
            toCell.setCellStyle(fromCell.getCellStyle());
        }

        // 
        if (fromCell.getCellComment() != null) {
            toCell.setCellComment(fromCell.getCellComment());
        }
    }
}

From source file:org.bbreak.excella.reports.ReportsTestUtil.java

License:Open Source License

/**
 * ?????// w ww. j  ava 2 s  .c  om
 * 
 * @param cell 
 * @return ???
 */
private static String getCellValue(Cell cell) {
    String value = null;

    if (cell != null) {
        switch (cell.getCellTypeEnum()) {
        case BLANK:
            value = cell.getStringCellValue();
            break;
        case BOOLEAN:
            value = String.valueOf(cell.getBooleanCellValue());
            break;
        case ERROR:
            value = String.valueOf(cell.getErrorCellValue());
            break;
        case NUMERIC:
            value = String.valueOf(cell.getNumericCellValue());
            break;
        case STRING:
            value = cell.getStringCellValue();
            break;
        case FORMULA:
            value = cell.getCellFormula();
        default:
            value = "";
        }
    }
    return value;
}

From source file:org.easybatch.extensions.msexcel.MsExcelRecordWriter.java

License:Open Source License

private void setValue(XSSFCell cell, Cell next) {
    switch (next.getCellType()) {
    case Cell.CELL_TYPE_BOOLEAN:
        cell.setCellValue(next.getBooleanCellValue());
        cell.setCellType(Cell.CELL_TYPE_BOOLEAN);
        break;/*from   ww  w  .  j a  v a2 s  .c o  m*/
    case Cell.CELL_TYPE_NUMERIC:
        cell.setCellValue(next.getNumericCellValue());
        cell.setCellType(Cell.CELL_TYPE_NUMERIC);
        break;
    case Cell.CELL_TYPE_STRING:
        cell.setCellValue(next.getStringCellValue());
        cell.setCellType(Cell.CELL_TYPE_STRING);
        break;
    case Cell.CELL_TYPE_FORMULA:
        cell.setCellValue(next.getCellFormula());
        cell.setCellType(Cell.CELL_TYPE_FORMULA);
        break;
    }
}

From source file:org.eclipse.rcptt.ecl.data.apache.poi.impl.internal.commands.ExcelFileService.java

License:Open Source License

public static String getCellValue(Cell cell) {
    if (cell == null) {
        return "";
    }//from   w ww  . jav  a2 s  .  c  o m
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        return cell.getStringCellValue();
    case Cell.CELL_TYPE_NUMERIC:
        String number = String.valueOf(cell.getNumericCellValue());
        number = number.replaceAll(".0$", "");
        return number;
    case Cell.CELL_TYPE_BOOLEAN:
        return String.valueOf(cell.getBooleanCellValue());
    case Cell.CELL_TYPE_FORMULA:
        return "=" + cell.getCellFormula();
    default:
        return "";
    }
}

From source file:org.efaps.esjp.common.file.FileUtil_Base.java

License:Apache License

/**
 * Copy cell./*  w w  w.  j  av  a2s .c o m*/
 *
 * @param _oldCell the old cell
 * @param _newCell the new cell
 * @param _styleMap the style map
 */
protected void copyCell(final Cell _oldCell, final Cell _newCell, final Map<Integer, CellStyle> _styleMap) {
    if (_styleMap != null) {
        if (_oldCell.getSheet().getWorkbook() == _newCell.getSheet().getWorkbook()) {
            _newCell.setCellStyle(_oldCell.getCellStyle());
        } else {
            final int stHashCode = _oldCell.getCellStyle().hashCode();
            CellStyle newCellStyle = _styleMap.get(stHashCode);
            if (newCellStyle == null) {
                newCellStyle = _newCell.getSheet().getWorkbook().createCellStyle();
                newCellStyle.cloneStyleFrom(_oldCell.getCellStyle());
                _styleMap.put(stHashCode, newCellStyle);
            }
            _newCell.setCellStyle(newCellStyle);
        }
    }
    switch (_oldCell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        _newCell.setCellValue(_oldCell.getStringCellValue());
        break;
    case Cell.CELL_TYPE_NUMERIC:
        _newCell.setCellValue(_oldCell.getNumericCellValue());
        break;
    case Cell.CELL_TYPE_BLANK:
        _newCell.setCellType(Cell.CELL_TYPE_BLANK);
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        _newCell.setCellValue(_oldCell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_ERROR:
        _newCell.setCellErrorValue(_oldCell.getErrorCellValue());
        break;
    case Cell.CELL_TYPE_FORMULA:
        _newCell.setCellFormula(_oldCell.getCellFormula());
        break;
    default:
        break;
    }

}

From source file:org.formulacompiler.spreadsheet.internal.excel.xls.loader.ExcelXLSLoader.java

License:Open Source License

private void loadCell(Cell _xlsCell, RowBuilder _rowBuilder) {
    final int xlsType = _xlsCell.getCellType();
    if (xlsType == Cell.CELL_TYPE_FORMULA) {
        final String expression;
        expression = _xlsCell.getCellFormula();
        _rowBuilder.addCellWithExpression(new LazySpreadsheetExpressionParser(expression, CellRefFormat.A1));

        if (this.config.loadAllCellValues) {
            final int cachedFormulaResultType = _xlsCell.getCachedFormulaResultType();
            if (Cell.CELL_TYPE_NUMERIC == cachedFormulaResultType) {
                _rowBuilder.setValue(getNumberValue(_xlsCell));
            } else if (Cell.CELL_TYPE_BOOLEAN == cachedFormulaResultType) {
                _rowBuilder.setValue(_xlsCell.getBooleanCellValue());
            } else if (Cell.CELL_TYPE_STRING == cachedFormulaResultType) {
                _rowBuilder.setValue(_xlsCell.getStringCellValue());
            }/*  w w  w .  ja v a  2 s .c o  m*/
        }
    } else if (Cell.CELL_TYPE_BLANK == xlsType) {
        _rowBuilder.addEmptyCell();
    } else if (Cell.CELL_TYPE_BOOLEAN == xlsType) {
        _rowBuilder.addCellWithConstant(_xlsCell.getBooleanCellValue());
    }

    else if (Cell.CELL_TYPE_NUMERIC == xlsType) {
        _rowBuilder.addCellWithConstant(getNumberValue(_xlsCell));

    } else if (Cell.CELL_TYPE_STRING == xlsType) {
        _rowBuilder.addCellWithConstant(_xlsCell.getStringCellValue());
    } else if (xlsType == Cell.CELL_TYPE_ERROR) {
        final int errorCode = _xlsCell.getErrorCellValue();
        switch (errorCode) {
        case 7:
            _rowBuilder.addCellWithError(CellWithError.DIV0);
            break;
        case 15:
            _rowBuilder.addCellWithError(CellWithError.VALUE);
            break;
        case 23:
            _rowBuilder.addCellWithError(CellWithError.REF);
            break;
        case 36:
            _rowBuilder.addCellWithError(CellWithError.NUM);
            break;
        case 42:
            _rowBuilder.addCellWithError(CellWithError.NA);
            break;
        default:
            _rowBuilder.addCellWithError("#ERR:" + errorCode);
        }
    }
}

From source file:org.generationcp.middleware.util.PoiUtil.java

License:Open Source License

private static Object getFormulaValue(final Cell cell) {
    switch (cell.getCachedFormulaResultType()) {
    case Cell.CELL_TYPE_NUMERIC:
        return cell.getNumericCellValue();
    case Cell.CELL_TYPE_STRING:
        return cell.getRichStringCellValue();
    default:/*  ww w  .j ava 2s.c o m*/
        return cell.getCellFormula();
    }
}

From source file:org.generationcp.middleware.util.PoiUtil.java

License:Open Source License

private static void cloneCell(final Cell cNew, final Cell cOld) {
    cNew.setCellComment(cOld.getCellComment());
    cNew.setCellStyle(cOld.getCellStyle());

    switch (cNew.getCellType()) {
    case Cell.CELL_TYPE_BOOLEAN: {
        cNew.setCellValue(cOld.getBooleanCellValue());
        break;/*  w w w  . j  a  v  a  2s . c  om*/
    }
    case Cell.CELL_TYPE_NUMERIC: {
        cNew.setCellValue(cOld.getNumericCellValue());
        break;
    }
    case Cell.CELL_TYPE_STRING: {
        cNew.setCellValue(cOld.getStringCellValue());
        break;
    }
    case Cell.CELL_TYPE_ERROR: {
        cNew.setCellValue(cOld.getErrorCellValue());
        break;
    }
    case Cell.CELL_TYPE_FORMULA: {
        cNew.setCellFormula(cOld.getCellFormula());
        break;
    }
    }

}

From source file:org.grible.excel.ExcelFile.java

License:Open Source License

private String getStringCellValue(Cell cell) {
    if (cell == null) {
        return "";
    }//from   www  .j ava2 s  .  co  m
    if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
        String result = String.valueOf(cell.getNumericCellValue());
        if (result.endsWith(".0")) {
            result = StringUtils.substringBeforeLast(result, ".0");
        }
        return result;
    }
    if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
        return String.valueOf(cell.getBooleanCellValue());
    }
    if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
        return cell.getCellFormula();
    }
    return cell.getStringCellValue();
}