需要关于Apache的POI API数组公式执行的帮助 [英] Need help regarding array formulas implementation in apache poi api

查看:2770
本文介绍了需要关于Apache的POI API数组公式执行的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了现有项目的分配选择更新。 previous开发商使用JCOM API将数据导出到Excel表。但是JCOM API不与64位系统上运行。我决定改变code和使用Apache POI API。我设法做了很多方法。我的问题是数组公式。我需要实现使用Apache POI数组公式。这些公式如下公布,任何帮助将更多的AP preciable。在此先感谢球员。

公式:

字符串一级方程式=SUM(R [-2] C / 1.200); //纯收入由总收入计算
字符串formula2 =SUM(R [-1] C-R [1] C);
串formula3 =SUM(R [ ​​- + Integer.toString(listTypeTotals.size()+ 1)+]Ç:R -2] C);

我试图设置单元格为formulatype,并通过公式字符串。

setCellFormulaStyle(表4,I + 2,formula2); 公共静态无效setCellFormulaStyle(HSSFSheet片,诠释行,诠释列,字符串值)
  {
    HSSFRow temprow = NULL;
    temprow = getRow_CreateRow(表,行);
    temprow.createCell(列).setCellFormula(值);
  }公共静态HSSFRow getRow_CreateRow(HSSFSheet片,诠释行)
{
  HSSFRow excelrow = NULL;
  excelrow = sheet.getRow(行);
  如果(excelrow == NULL)
  {
    excelrow = sheet.createRow(行);
    返回excelrow;
  }其他
  返回excelrow;
}

我收到以下异常

  org.apache.poi.ss.formula.FormulaParseException:指定的命名范围'R'不会在当前工作簿中存在。
在org.apache.poi.ss.formula.FormulaParser.parseNonRange(FormulaParser.java:569)
在org.apache.poi.ss.formula.FormulaParser.parseRangeable(FormulaParser.java:517)
在org.apache.poi.ss.formula.FormulaParser.parseRangeEx pression(FormulaParser.java:268)
在org.apache.poi.ss.formula.FormulaParser.parseSimpleFactor(FormulaParser.java:1119)
在org.apache.poi.ss.formula.FormulaParser.percentFactor(FormulaParser.java:1079)
在org.apache.poi.ss.formula.FormulaParser.powerFactor(FormulaParser.java:1066)
在org.apache.poi.ss.formula.FormulaParser.Term(FormulaParser.java:1426)
在org.apache.poi.ss.formula.FormulaParser.additiveEx pression(FormulaParser.java:1526)
在org.apache.poi.ss.formula.FormulaParser.concatEx pression(FormulaParser.java:1510)
在org.apache.poi.ss.formula.FormulaParser.comparisonEx pression(FormulaParser.java:1467)
在org.apache.poi.ss.formula.FormulaParser.Arguments(FormulaParser.java:1051)
在org.apache.poi.ss.formula.FormulaParser.function(FormulaParser.java:936)
在org.apache.poi.ss.formula.FormulaParser.parseNonRange(FormulaParser.java:558)
在org.apache.poi.ss.formula.FormulaParser.parseRangeable(FormulaParser.java:429)
在org.apache.poi.ss.formula.FormulaParser.parseRangeEx pression(FormulaParser.java:268)
在org.apache.poi.ss.formula.FormulaParser.parseSimpleFactor(FormulaParser.java:1119)
在org.apache.poi.ss.formula.FormulaParser.percentFactor(FormulaParser.java:1079)
在org.apache.poi.ss.formula.FormulaParser.powerFactor(FormulaParser.java:1066)
在org.apache.poi.ss.formula.FormulaParser.Term(FormulaParser.java:1426)
在org.apache.poi.ss.formula.FormulaParser.additiveEx pression(FormulaParser.java:1526)
在org.apache.poi.ss.formula.FormulaParser.concatEx pression(FormulaParser.java:1510)
在org.apache.poi.ss.formula.FormulaParser.comparisonEx pression(FormulaParser.java:1467)
在org.apache.poi.ss.formula.FormulaParser.unionEx pression(FormulaParser.java:1447)
在org.apache.poi.ss.formula.FormulaParser.parse(FormulaParser.java:1568)
在org.apache.poi.ss.formula.FormulaParser.parse(FormulaParser.java:176)
在org.apache.poi.hssf.model.HSSFFormulaParser.parse(HSSFFormulaParser.java:72)
在org.apache.poi.hssf.usermodel.HSSFCell.setCellFormula(HSSFCell.java:594)`


解决方案

我不知道你怎么有使用的公式呢。但是,如果你的问题是关于基于行和列的动态单元格引用,或许我可以帮你。

有关的任​​何细胞,说B5,在运行时,

  cell.getReference();

会给你的单元格引用(如例子...它会返回你B5

  cell.getReference()的toString()的charAt(0);

会给你列引用(会给你B如果当前单元格B5)。现在,

  cell.getRowIndex();

  cell.getReference()的toString()的charAt(1)。

会给你行的索引。我已经使用了多次更新/创建工作簿我命名范围和计算公式。

小的变化,包括像处理 AZ89地址

有关AZ99我们可以利用这一点,我们可以用小黑客像的情况:

 字符串str = cell.getReference();
    对于(指数= 0;指数 - LT; str.length();指数++){
        如果((INT)str.charAt(指数)LT; 65){
            打破;
        }
    }
    字符串列= str.substring(0,指数);
    串行= str.substring(指数+1,str.length());

I got allocated updation of existing project. Previous developer used jcom api to export data to excel sheets. But jcom api don't work with 64 bit systems. I decided to change code and using apache poi api. I managed to done many methods. My problem is array formulas. I Need to implement array formulas using apache poi. Those formulas are posted below, any help will be more appreciable. Thanks in advance guys.

Formulas:

String  formula1 = "SUM(R[-2]C/1.200)";//net income  calculation from gross income
String  formula2 = "SUM(R[-1]C-R[1]C)";
String  formula3 = "SUM(R[-" + Integer.toString(listTypeTotals.size()+1) + "]C:R[-2]C)";

I tried to set that cell as formulatype and passing formula as string.

setCellFormulaStyle(sheet, 4, i+2, formula2);

 public static void setCellFormulaStyle(HSSFSheet sheet,int row, int column,String value)
  {
    HSSFRow temprow = null;
    temprow =getRow_CreateRow(sheet, row);
    temprow.createCell(column).setCellFormula(value);
  }

public static HSSFRow getRow_CreateRow(HSSFSheet sheet,int row)
{
  HSSFRow excelrow=null;
  excelrow = sheet.getRow(row);
  if(excelrow==null)
  {
    excelrow =sheet.createRow(row);
    return excelrow;
  }else
  return excelrow;
}

I am getting following exception

org.apache.poi.ss.formula.FormulaParseException: Specified named range 'R' does not exist in the current workbook.
at org.apache.poi.ss.formula.FormulaParser.parseNonRange(FormulaParser.java:569)
at org.apache.poi.ss.formula.FormulaParser.parseRangeable(FormulaParser.java:517)
at org.apache.poi.ss.formula.FormulaParser.parseRangeExpression(FormulaParser.java:268)
at org.apache.poi.ss.formula.FormulaParser.parseSimpleFactor(FormulaParser.java:1119)
at org.apache.poi.ss.formula.FormulaParser.percentFactor(FormulaParser.java:1079)
at org.apache.poi.ss.formula.FormulaParser.powerFactor(FormulaParser.java:1066)
at org.apache.poi.ss.formula.FormulaParser.Term(FormulaParser.java:1426)
at org.apache.poi.ss.formula.FormulaParser.additiveExpression(FormulaParser.java:1526)
at org.apache.poi.ss.formula.FormulaParser.concatExpression(FormulaParser.java:1510)
at org.apache.poi.ss.formula.FormulaParser.comparisonExpression(FormulaParser.java:1467)
at org.apache.poi.ss.formula.FormulaParser.Arguments(FormulaParser.java:1051)
at org.apache.poi.ss.formula.FormulaParser.function(FormulaParser.java:936)
at org.apache.poi.ss.formula.FormulaParser.parseNonRange(FormulaParser.java:558)
at org.apache.poi.ss.formula.FormulaParser.parseRangeable(FormulaParser.java:429)
at org.apache.poi.ss.formula.FormulaParser.parseRangeExpression(FormulaParser.java:268)
at org.apache.poi.ss.formula.FormulaParser.parseSimpleFactor(FormulaParser.java:1119)
at org.apache.poi.ss.formula.FormulaParser.percentFactor(FormulaParser.java:1079)
at org.apache.poi.ss.formula.FormulaParser.powerFactor(FormulaParser.java:1066)
at org.apache.poi.ss.formula.FormulaParser.Term(FormulaParser.java:1426)
at org.apache.poi.ss.formula.FormulaParser.additiveExpression(FormulaParser.java:1526)
at org.apache.poi.ss.formula.FormulaParser.concatExpression(FormulaParser.java:1510)
at org.apache.poi.ss.formula.FormulaParser.comparisonExpression(FormulaParser.java:1467)
at org.apache.poi.ss.formula.FormulaParser.unionExpression(FormulaParser.java:1447)
at org.apache.poi.ss.formula.FormulaParser.parse(FormulaParser.java:1568)
at org.apache.poi.ss.formula.FormulaParser.parse(FormulaParser.java:176)
at org.apache.poi.hssf.model.HSSFFormulaParser.parse(HSSFFormulaParser.java:72)
at org.apache.poi.hssf.usermodel.HSSFCell.setCellFormula(HSSFCell.java:594)`

解决方案

I am not sure how you have use the formula yet. However if your problem is regarding the dynamic cell reference based on rows and Columns, perhaps I can help you.

For any cell, say "B5", at runtime,

cell.getReference(); 

will give you cell reference (like in example... it will return you "B5"

cell.getReference().toString().charAt(0); 

will give you the Column Reference (will give you "B" if the current cell is B5). Now

cell.getRowIndex(); 

OR

 cell.getReference().toString().charAt(1);

will give you Row Index. I have used that multiple times to update/create the Named ranges and formula on my workbook.

Small Changes to include handling address like AZ89

For the cases of AZ99 we can use for this we can use small hacks like:

String str = cell.getReference();
    for(index = 0;index<str.length();index++){
        if((int)str.charAt(index)<65){
            break;
        }
    }
    String Col = str.substring(0, index);
    String Row = str.substring(index+1, str.length());

这篇关于需要关于Apache的POI API数组公式执行的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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