當前位置: 首頁>>代碼示例>>Java>>正文


Java FormulaEvaluator.evaluate方法代碼示例

本文整理匯總了Java中org.apache.poi.ss.usermodel.FormulaEvaluator.evaluate方法的典型用法代碼示例。如果您正苦於以下問題:Java FormulaEvaluator.evaluate方法的具體用法?Java FormulaEvaluator.evaluate怎麽用?Java FormulaEvaluator.evaluate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.poi.ss.usermodel.FormulaEvaluator的用法示例。


在下文中一共展示了FormulaEvaluator.evaluate方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getValueOrEmptyAsObject

import org.apache.poi.ss.usermodel.FormulaEvaluator; //導入方法依賴的package包/類
public static Object getValueOrEmptyAsObject(FormulaEvaluator evaluator, Cell cell) {
    final CellValue cellValue = evaluator.evaluate(cell);
    if (cellValue == null) {
        return "";
    }
    switch (cellValue.getCellType()) {
        case Cell.CELL_TYPE_BOOLEAN:
            return Boolean.valueOf(cellValue.getBooleanValue());
        case Cell.CELL_TYPE_NUMERIC:
            if (DateUtil.isCellDateFormatted(cell)) {
                return cell.getDateCellValue();
            }
            BigDecimal bd = BigDecimal.valueOf(cell.getNumericCellValue()).setScale(DECIMAL_PRECISION, BigDecimal.ROUND_HALF_UP);
            return bd.stripTrailingZeros();
        case Cell.CELL_TYPE_STRING:
            return cellValue.getStringValue();
        case Cell.CELL_TYPE_ERROR:
            return "ERRO";
        default:
            return "";
    }
}
 
開發者ID:tecsinapse,項目名稱:tecsinapse-data-io,代碼行數:23,代碼來源:ImporterUtils.java

示例2: getCellValue

import org.apache.poi.ss.usermodel.FormulaEvaluator; //導入方法依賴的package包/類
/**
 * セルの値の取得。 セルのタイプに応じた値を返卻する。<br>
 * <br>
 * 注:セルタイプが[CELL_TYPE_ERROR]の場合<br>
 * ・xls形式 :エラーコードを返卻(HSSFErrorConstantsに定義)<br>
 * ・xlsx形式 :Excelのエラー値を返卻(ex.#DIV/0!、#N/A、#REF!・・・)
 * 
 * @param cell 対象セル
 * @return 値
 */
public static Object getCellValue( Cell cell) {
    Object value = null;

    if ( cell != null) {
        switch ( cell.getCellTypeEnum()) {
            case BLANK:
                break;
            case BOOLEAN:
                value = cell.getBooleanCellValue();
                break;
            case ERROR:
                value = cell.getErrorCellValue();
                break;
            case NUMERIC:
                // 日付の場合
                if ( isCellDateFormatted( cell)) {
                    value = cell.getDateCellValue();
                } else {
                    value = cell.getNumericCellValue();
                }
                break;
            case STRING:
                value = cell.getStringCellValue();
                break;
            case FORMULA:
                FormulaEvaluator evaluator = cell.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator();
                // 式を評価
                CellValue cellValue = evaluator.evaluate( cell);
                CellType cellType = cellValue.getCellTypeEnum();
                // 評価結果の型で分岐
                switch ( cellType) {
                    case BLANK:
                        break;
                    case BOOLEAN:
                        value = cell.getBooleanCellValue();
                        break;
                    case ERROR:
                        if ( cell instanceof XSSFCell) {
                            // XSSF形式の場合は、文字列を返卻
                            XSSFCell xssfCell = ( XSSFCell) cell;
                            CTCell ctCell = xssfCell.getCTCell();
                            value = ctCell.getV();
                        } else if ( cell instanceof HSSFCell) {
                            // HSSF形式の場合は、エラーコードを返卻
                            value = cell.getErrorCellValue();
                        }
                        break;
                    case NUMERIC:
                        // 日付の場合
                        if ( isCellDateFormatted( cell)) {
                            value = cell.getDateCellValue();
                        } else {
                            value = cell.getNumericCellValue();
                        }
                        break;
                    case STRING:
                        value = cell.getStringCellValue();
                        break;
                    default:
                        break;
                }
            default:
                break;
        }
    }
    return value;
}
 
開發者ID:excella-core,項目名稱:excella-core,代碼行數:78,代碼來源:PoiUtil.java

示例3: evaluateCellFormula

import org.apache.poi.ss.usermodel.FormulaEvaluator; //導入方法依賴的package包/類
private Object evaluateCellFormula(final HSSFWorkbook workbook, final Cell cell) {
    FormulaEvaluator evaluator = workbook.getCreationHelper()
            .createFormulaEvaluator();
    CellValue cellValue = evaluator.evaluate(cell);
    Object result = null;
    if (cellValue.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
        result = cellValue.getBooleanValue();
    } else if (cellValue.getCellType() == Cell.CELL_TYPE_NUMERIC) {
        result = cellValue.getNumberValue();
    } else if (cellValue.getCellType() == Cell.CELL_TYPE_STRING) {
        result = cellValue.getStringValue();
    }
    return result;
}
 
開發者ID:cyber-coders-j2017a,項目名稱:modern.core.java.repo,代碼行數:15,代碼來源:ExcelXlsReaderParameter.java

示例4: parseAsJsonElement

import org.apache.poi.ss.usermodel.FormulaEvaluator; //導入方法依賴的package包/類
private static JsonElement parseAsJsonElement(Cell cell, FormulaEvaluator evaluator) {
    switch (cell.getCellTypeEnum()) {
        case NUMERIC:
            if (HSSFDateUtil.isCellDateFormatted(cell)) {
                return new JsonPrimitive(DateFormatterHolder.formatter.format(cell.getDateCellValue().toInstant()));
            } else {
                return new JsonPrimitive(cell.getNumericCellValue());
            }
        case STRING:
            return new JsonPrimitive(cell.getStringCellValue());
        case FORMULA:
            CellValue cellValue = evaluator.evaluate(cell);
            switch (cellValue.getCellTypeEnum()) {
                case NUMERIC:
                    return new JsonPrimitive(cellValue.getNumberValue());
                case STRING:
                    return new JsonPrimitive(cellValue.getStringValue());
                case BLANK:
                    return new JsonPrimitive("");
                case BOOLEAN:
                    return new JsonPrimitive(cellValue.getBooleanValue());
                case ERROR:
                default:
                    return null;
            }
        case BLANK:
            return new JsonPrimitive("");
        case BOOLEAN:
            return new JsonPrimitive(cell.getBooleanCellValue());
        case ERROR:
        default:
            return null;
    }
}
 
開發者ID:zjnu-acm,項目名稱:judge,代碼行數:35,代碼來源:ExcelUtil.java

示例5: getCellValueFromFormula

import org.apache.poi.ss.usermodel.FormulaEvaluator; //導入方法依賴的package包/類
private static Object getCellValueFromFormula(FormulaEvaluator formulaEvaluator, Cell cell) {
	try {
		CellValue cellValue = formulaEvaluator.evaluate(cell);
		
		if (cellValue.getCellType() == Cell.CELL_TYPE_NUMERIC) {
			if (DateUtil.isCellDateFormatted(cell)) {
				Calendar calendar = GregorianCalendar.getInstance();
				calendar.setTime(DateUtil.getJavaDate(cellValue.getNumberValue()));
				
				return calendar.getTime();
			} else {
				return DECIMAL_FORMAT.format(cellValue.getNumberValue());
			}
		} else if (cellValue.getCellType() == Cell.CELL_TYPE_STRING) {
			if (StringUtils.hasText(cellValue.getStringValue())) {
				return cellValue.getStringValue();
			}
		}
	} catch (NotImplementedException e) {
		// If formula use Excel features not implemented in POI (like proper),
		// we can retrieve the cached value (which may no longer be correct, depending of what you do on your file).
		FormulaFeature feature = EnumUtils.getEnum(FormulaFeature.class, e.getCause().getMessage());
		if (ALLOWED_NOT_IMPLEMENTED_FORMULA_FEATURES.contains(feature)) {
			return getCellPrimitiveValue(cell, cell.getCachedFormulaResultType());
		} else {
			throw e;
		}
	}
	
	return null;
}
 
開發者ID:openwide-java,項目名稱:owsi-core-parent,代碼行數:32,代碼來源:WorkbookUtils.java

示例6: evaluateCellFormula

import org.apache.poi.ss.usermodel.FormulaEvaluator; //導入方法依賴的package包/類
/**
 * Evaluate the formula of the given cell.
 * 
 * @param workbook
 *            workbook (excel) for evaluating the cell formula
 * @param cell
 *            cell (excel)
 * 
 * @return the value of the excel-call as string (the formula will be executed)
 */
static String evaluateCellFormula(XSSFWorkbook workbook, XSSFCell cell) {
	FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
	CellValue cellValue = evaluator.evaluate(cell);

	switch (cellValue.getCellType()) {
	case Cell.CELL_TYPE_BOOLEAN:
		return String.valueOf(cellValue.getBooleanValue());
	case Cell.CELL_TYPE_NUMERIC:
		return String.valueOf(cellValue.getNumberValue());
	case Cell.CELL_TYPE_STRING:
		return cellValue.getStringValue();
	default:
		return null;
	}
}
 
開發者ID:fhm84,項目名稱:jexunit,代碼行數:26,代碼來源:ExcelLoader.java

示例7: execute

import org.apache.poi.ss.usermodel.FormulaEvaluator; //導入方法依賴的package包/類
public cfData execute( cfSession _session, List<cfData> parameters ) throws cfmRunTimeException {
	cfSpreadSheetData	spreadsheet = null;
	int rowNo, columnNo;
	
	/*
	 * Collect up the parameters
	 */
spreadsheet	= (cfSpreadSheetData)parameters.get(2);
rowNo				= parameters.get(1).getInt() - 1;
columnNo		= parameters.get(0).getInt() - 1;
		
if ( rowNo < 0 )
	throwException(_session, "row must be 1 or greater (" + rowNo + ")");
if ( columnNo < 0 )
	throwException(_session, "column must be 1 or greater (" + columnNo + ")");


/*
 * Find the cell in question 
 */
Sheet	sheet = spreadsheet.getActiveSheet();
Row row	= sheet.getRow( rowNo );
if ( row == null )
	row	= sheet.createRow( rowNo );

Cell cell	= row.getCell( columnNo );
if ( cell == null )
	cell = row.createCell( columnNo );

FormulaEvaluator evaluator = spreadsheet.getWorkBook().getCreationHelper().createFormulaEvaluator();

if ( cell.getCellType() == Cell.CELL_TYPE_BOOLEAN )
	return cfBooleanData.getcfBooleanData( cell.getBooleanCellValue() );
else if ( cell.getCellType() == Cell.CELL_TYPE_NUMERIC )
	return new cfNumberData( cell.getNumericCellValue() );
else if ( cell.getCellType() == Cell.CELL_TYPE_BLANK )
	return cfStringData.EMPTY_STRING;
else if ( cell.getCellType() == Cell.CELL_TYPE_STRING )
	return new cfStringData( cell.getStringCellValue() );
else if ( cell.getCellType() == Cell.CELL_TYPE_FORMULA ){
	CellValue cellValue = evaluator.evaluate(cell);
	
	switch (cellValue.getCellType()) {
		case Cell.CELL_TYPE_BOOLEAN:
			return cfBooleanData.getcfBooleanData(cellValue.getBooleanValue());
		case Cell.CELL_TYPE_NUMERIC:
			return new cfNumberData(cellValue.getNumberValue());
		case Cell.CELL_TYPE_STRING:
			return new cfStringData(cellValue.getStringValue());
		default:
			return cfStringData.EMPTY_STRING;
	}

}else
	return cfStringData.EMPTY_STRING;
}
 
開發者ID:OpenBD,項目名稱:openbd-core,代碼行數:57,代碼來源:SpreadsheetGetCellValue.java

示例8: generateCSVFromCell

import org.apache.poi.ss.usermodel.FormulaEvaluator; //導入方法依賴的package包/類
private String generateCSVFromCell(Cell cell, FormulaEvaluator evaluator)
{
    try
    {
        if ((cell != null) && (cell.getCellType() != Cell.CELL_TYPE_BLANK))
        {
            CellValue cellValue = evaluator.evaluate(cell);

            if (cellValue.getCellType() == Cell.CELL_TYPE_STRING)
                return cellValue.getStringValue();
            else if (cellValue.getCellType() == Cell.CELL_TYPE_NUMERIC)
            {
                if (DateUtil.isCellDateFormatted(cell))
                {
                    CellStyle cellStyle = cell.getCellStyle();

                    String     excelDateFormat = cellStyle.getDataFormatString();
                    String     javaDateFormat  = excelToJavaDataFormat(excelDateFormat);
                    DateFormat dateFormat      = new SimpleDateFormat(javaDateFormat);
                    return dateFormat.format(cell.getDateCellValue());
                }
                else
                    return Double.toString(cellValue.getNumberValue());
            }
            else if (cellValue.getCellType() == Cell.CELL_TYPE_BOOLEAN)
                return Boolean.toString(cellValue.getBooleanValue());
            else if (cellValue.getCellType() == Cell.CELL_TYPE_BLANK)
                return "";
            else
            {
                logger.log(Level.WARNING, "Problem process cell: Unknown CellValue Type = " + cellValue.getCellType());
                return "";
            }
        }
        else
            return "";
    }
    catch (Throwable throwable)
    {
        logger.log(Level.WARNING, "Problem process cell: Unknown Cell Type", throwable);
        return "";
    }
}
 
開發者ID:arjuna-technologies,項目名稱:Apache-POI_DataBroker_PlugIn,代碼行數:44,代碼來源:XSSFSheetToCSVDataProcessor.java


注:本文中的org.apache.poi.ss.usermodel.FormulaEvaluator.evaluate方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。