如何使用 org.apache.poi.ss.formula.FormulaParser 来解析公式? [英] How to use org.apache.poi.ss.formula.FormulaParser to parse the formula?

查看:29
本文介绍了如何使用 org.apache.poi.ss.formula.FormulaParser 来解析公式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能给我使用 org.apache.poi.ss.formula.FormulaParser 的简单代码片段吗.

Can u give me the simple code snippt using org.apache.poi.ss.formula.FormulaParser.

FormulaParser 类具有 parse() 方法.但它返回 ptg[].我不知道 ptg 类在哪里...
请指导我使用formulaParse解析excel表格公式...

FormulaParser class having the method parse().But it returns ptg[].i dont know where is the ptg class...
Please guide me to use formulaParse to parse the excel sheet formula...

沙拉湾

推荐答案

Ptg[] 是一个 标记数组,表示单元格中的公式.

Ptg[] is a array of tokens which represents the formula in the cell.

假设在我的 Excel 表格中,我有一个带有公式的单元格

Suppose in my excel sheet I have as cell with a formula as

=D4+D6+D8-D11+D23+D29+D46-D49

通过一个 FormulaParser 运行这个给我一个我已经打印出来的数组,如下所示

Running this through a FormulaParser gives me an array which I have printed out as shown below

HSSFEvaluationWorkbook hssfew = HSSFEvaluationWorkbook.create(workBook);

Ptg[] ptg = FormulaParser.parse(cell1.getCellFormula(), hssfew, FormulaType.NAMEDRANGE, 0);

for (int i=0;i<ptg.length;i++){
    System.out.println (ptg[i]);
}

结果

org.apache.poi.hssf.record.formula.RefPtg [D4] 
org.apache.poi.hssf.record.formula.RefPtg [D6] 
class org.apache.poi.hssf.record.formula.AddPtg 
org.apache.poi.hssf.record.formula.RefPtg [D8] 
class org.apache.poi.hssf.record.formula.AddPtg 
org.apache.poi.hssf.record.formula.RefPtg [D11] 
class org.apache.poi.hssf.record.formula.SubtractPtg
org.apache.poi.hssf.record.formula.RefPtg [D23]
class org.apache.poi.hssf.record.formula.AddPtg 
org.apache.poi.hssf.record.formula.RefPtg [D29] 
class org.apache.poi.hssf.record.formula.AddPtg 
org.apache.poi.hssf.record.formula.RefPtg [D46] 
class org.apache.poi.hssf.record.formula.AddPtg 
org.apache.poi.hssf.record.formula.RefPtg [D49] 
class org.apache.poi.hssf.record.formula.SubtractPtg

如您所见,这将公式中的每个元素分解为相应的单元格位置或运算符+ 的 AddPtg 和 - 运算符的 SubtractPtg

As you can see this breaks down each element in the formula into the respective cell location or operator AddPtg for + and SubtractPtg for - operator

这是一个简单的例子,你可以尝试更复杂的东西

This is a simple example, you can try out more complex stuff

这篇关于如何使用 org.apache.poi.ss.formula.FormulaParser 来解析公式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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