POI / Excel:在“相对”关系中应用公式方式
我正在使用 Apache 的 POI 通过 Java 操作 Excel (.xls) 文件。
我正在尝试创建一个新单元格,其内容是公式的结果,就好像用户复制/粘贴了公式一样(我称之为“相对”方式,与“绝对”相反)。
为了让自己更清楚,这里有一个简单的例子:
- 单元格A1包含“1”,B1包含“2”,A2包含“3”,B2包含“4”。
- 单元格 A3 包含以下公式“=A1+B1”。
如果我将公式复制到 Excel 下的 A4 单元格,它会变成 "=A2+B2"
:Excel 正在动态调整公式的内容。
不幸的是我无法以编程方式得到相同的结果。我发现的唯一解决方案是将公式标记化并自己完成肮脏的工作,但我真的怀疑是否应该这样做。我无法在指南或 API 中找到我想要的内容。
有没有更简单的方法来解决这个问题?如果是这样,你能指出我正确的方向吗?
最好的问候,
尼尔斯
I'm using Apache's POI to manipulate Excel (.xls) files with Java.
I'm trying to create a new cell whom content is the result of a formula as if the user had copied/pasted the formula (what i call the "relative" way, as opposite to "absolute").
To make myself clearer, here is a simple example:
- Cell A1 contains "1",B1 contains "2", A2 contains "3", B2 contains "4".
- Cell A3 contains the following formula "=A1+B1".
If I copy the formula to the A4 cell under excel, it becomes "=A2+B2"
: excel is adapting the content of the formula dynamically.
Unfortunately I cannot get the same result programatically. The only solution I found is to tokenize the formula and do the dirty work myself, but I really doubt that this is supposed to be done that way. I was not able to find what I'm looking for in the guides or in the API.
Is there an easier way to solve this problem ? If it's the case, can you please point me in the right direction ?
Best regards,
Nils
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在我看来,user2622016 是对的,除了他的解决方案仅管理单元引用,而不是区域引用(它不适用于<例如,代码>=SUM(A1:B8))。
以下是我解决此问题的方法:
我仍然不知道它是否适用于所有单元格公式,但它对我有用,快速且可靠。
In my sense, user2622016 is right, except his solution manages only cell references, as opposed to area references (it won't work for
=SUM(A1:B8)
for instance).Here's how I fixed this :
I still don't know if I had it correct for all cell formulas, but it works for me, fast and reliable.
我也认为没有一个简单的方法可以做到这一点。
甚至 POI 网站上的 HSSF 和 XSSD 示例,例如 TimesheetDemo 进行公式构建手动。例如110号线附近
I too think that there isn't an easy way to do this.
Even the HSSF and XSSD examples on the POI site e.g. TimesheetDemo do the formula construction manually. e.g. around line 110
我查看了 FormulaEvaluator 类,发现了一些 POI 内部类可以为我们完成这项工作。
FormulaParser,它将 String 解析为“解析事物”的数组:
ptgs 现在是我们采用反向波兰表示法的公式。现在,根据需要遍历所有元素并一一修改引用:
并且您已准备好将“解析事物”渲染回字符串:
I looked inside FormulaEvaluator class and found some POI internal classes that can do the work for us.
FormulaParser, which parses String to array of "parse things":
ptgs is now our formula in reverse polish notation. Now go through all elements and modify references one by one as you wish:
And you're ready to render "parse things" back to String:
另一种相对复制公式的方法,使用 poi 3.12 进行测试
根据需要更新共享公式:
从 poi 3.12 开始,SharedFormula 不支持来自其他工作表的单元格引用/公式 (='Sheet1'!A1)。以下是 SharedFormula 的更新:
Another way to copy formula relatively, tested with poi 3.12
Update shared formula as needed:
As of poi 3.12, SharedFormula doesn't support cell reference/formula from other sheets (='Sheet1'!A1). Here's an update to SharedFormula:
我认为不存在。 POI 必须解析公式(考虑到 A1 与 $A$1 与 $A1 等),但我不相信它有这种能力。当我过去这样做时,我总是必须自己管理。抱歉 - 不是您希望的答案!
I don't think there is. POI would have to parse the formula (taking into account A1 vs. $A$1 vs. $A1 etc.) and I don't believe it has that capacity. When I've done this in the past I've always had to manage this myself. Sorry - not the answer you hoped for!
您可以尝试一些第三方 Excel 库,其中大多数都可以处理相对/绝对范围公式。
you can try some third party excel librarys,most of them can handle the relative/absolute range formulas.