简体   繁体   English

使用Java Apache POI编写Excel公式

[英]Write excel formula using Java Apache POI

I've been searching around a lot for this but couldn't find a solution. 我一直在为此寻找很多东西,但是找不到解决方案。 Hope someone can help me here. 希望有人可以在这里帮助我。 I am using Apache POI to create a simple tool. 我正在使用Apache POI创建一个简单的工具。 Formulas will be inputted by user, and the result will be written on file. 公式将由用户输入,并将结果写入文件中。 Im stuck at extending/filling the formula. 我在扩展/填充公式时陷入困境。

Suppose you have a simple excel formula: 假设您有一个简单的excel公式:

IF(A2=B2,True,False)

A drag downwards on excel would result in: 向下拖动excel会导致:

IF(A2=B2,True,False)
IF(A3=B3,True,False)
IF(A4=B4,True,False)
IF(A5=B5,True,False)
         . .
          .

Now I want to do the same in my program. 现在,我想在程序中执行相同的操作。 I will know which row to end. 我会知道哪一行结束。 i just can't get the row index to increment. 我只是无法使行索引递增。 I have already done it manually (writing formula in program), but now I need to use this when formula is passed by user. 我已经手动完成了(在程序中编写公式),但是当用户传递公式时,现在需要使用它。

I have something like this for the manual part: 对于手动部分,我有以下内容:

for (int ind = 2; ind < rownumb ; ind++)
{
sheet.getRow(i).createCell(12).setCellFormula("IF(K" + ind + "=L" + ind + ",FALSE,TRUE)");
}

Now the user will input: 现在,用户将输入:

 =IF(K=L, False, True) OR  =IF(K0=L0, False, True)

I want to add this formula auto-filling (incrementing) all the way till rownumb. 我想一直将此公式自动填充(递增)直到rownumb。 How can I do this? 我怎样才能做到这一点?

If there is no direct way, can someone suggest some other approach, however the requirement is that the formula will be passed by user. 如果没有直接方法,有人可以建议其他方法,但是要求该公式将由用户传递。

Thanks. 谢谢。

Excel does not store it's function "in a sheet's cell" but as a VBA function. Excel不会将其功能“存储在工作表的单元格中”,而是存储为VBA功能。 In order to be able to properly utilize excel function yourself you will need to emulate that. 为了自己能够正确利用excel函数,您将需要进行仿真。 Check this article about user defined functions , this might get you going. 查看有关用户定义函数的本文 ,这可能会让您成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM