java的POI XSSF FormulaEvaluator [英] java POI XSSF FormulaEvaluator

查看:5067
本文介绍了java的POI XSSF FormulaEvaluator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我救我的新Excel文件我有一个问题。我想它,当它被保存在公式计算本身,但目前它只是返回在Excel文件中的字符串。其计算公式是正确的。我不知道到底是获得 FormulaEvaluator 工作

下面是我进入我的公式返回一个字符串:

<$p$p><$c$c>dataRow1.createCell((short)5).setCellValue(\"=VLOOKUP(A\"+rowCountVlookup+\",'C:\\\\Users\\\\Admin\\\\Documents\\\\JCreator LE \\\\ \\\\ MyProjects下的wordCount \\\\ \\\\类[Pricing.xlsx]工作表Sheet1'$ B $!3:$ E $ 41,4,FALSE)* E+ rowCountVlookup +);

任何帮助将是非常美联社preciated。


解决方案

我用这个code,以评估公式

  //我用工作簿的一个实例的Excel工作簿,我目前工作
工作簿wbook;私人CellValue formulaEvaluation(单元格){
    FormulaEvaluator formulaEval = wbook.getCreationHelper()createFormulaEvaluator()。
    返回formulaEval.evaluate(细胞);
}公共双obtieneObjetoNumericoCelda(单元格){
    双dblValue = NULL;
    如果(细胞!= NULL){
        开关(cell.getCellType()){
        案例Cell.CELL_TYPE_NUMERIC:
            dblValue = cell.getNumericCellValue();
            打破;
        案例Cell.CELL_TYPE_FORMULA:
            CellValue objCellValue = formulaEvaluation(单元);
            如果(objCellValue.getCellType()== Cell.CELL_TYPE_NUMERIC){
                dblValue = objCellValue.getNumberValue();
            }
            打破;
        }
    }
    返回dblValor;
}

I am having a problem when i save my new excel file. I want it that when it gets saved the formula calculates itself but at the moment it is just returning a string in the excel file. The formula is correct. I don't know exactly to get the FormulaEvaluator to work.

Here is where I enter my formula that returns a string:

dataRow1.createCell((short)5).setCellValue("=VLOOKUP(A"+rowCountVlookup+",'C:\\Users\\Admin\\Documents\\JCreator LE\\MyProjects\\WordCount\\classes\\[Pricing.xlsx]Sheet1'!$B$3:$E$41,4, FALSE)*E"+rowCountVlookup+"");

Any help would be much appreciated.

解决方案

I use this code to evaluate a formula

//I use an instance of the workbook for the Excel workbook I'm working at the moment
Workbook wbook;

private CellValue formulaEvaluation(Cell cell) {
    FormulaEvaluator formulaEval = wbook.getCreationHelper().createFormulaEvaluator();
    return formulaEval.evaluate(cell);
}

public Double obtieneObjetoNumericoCelda(Cell cell) {
    Double dblValue = null;
    if (cell != null) {
        switch(cell.getCellType()) {
        case Cell.CELL_TYPE_NUMERIC:
            dblValue = cell.getNumericCellValue();
            break;
        case Cell.CELL_TYPE_FORMULA:
            CellValue objCellValue = formulaEvaluation(cell);
            if (objCellValue.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                dblValue = objCellValue.getNumberValue();
            }
            break;
        }
    }
    return dblValor;
}

这篇关于java的POI XSSF FormulaEvaluator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆