[英]Apache POI for excel get dependent cells
我正在编写一个使用apache POI读取excel文件的程序。 我正在获取所有值,但是我想知道哪些单元格依赖于其他单元格(使用该单元格的公式)。 我已经尝试过使用String formula = cell.getCellFormula()
,但这只会向我返回单元String formula = cell.getCellFormula()
引(例如H5)。 我还有其他方法可以做到吗? 这是我读取单元格的代码:
private void handleCell(int type,Cell cell)
{
switch (type)
{
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_FORMULA:
String form = cell.getCellFormula();
handleCell(cell.getCachedFormulaResultType(),cell);
break;
default :
}
}
看看org.apache.poi.ss.formula.FormulaParser
。 它有一个静态方法
public static Ptg[] parse(
java.lang.String formula,
FormulaParsingWorkbook workbook,
int formulaType,
int sheetIndex)
根据文档,它以RPN顺序将公式字符串解析为令牌列表。
可以使用public final byte getPtgClass()
来检查令牌(Ptg =“ parse public final byte getPtgClass()
”)的类型(REF / VALUE / ARRAY public final byte getPtgClass()
。
我没有测试过,但是可能是要走的路。 解析公式,然后检查每个Ptg条目的类型(REF?)并获取目标单元格。
看到:
https://poi.apache.org/apidocs/org/apache/poi/ss/formula/FormulaParser.html https://poi.apache.org/apidocs/org/apache/poi/ss/formula/ptg/Ptg html的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.