实现Excel小计功能
Excel 通过菜单数据
-> 提供小计选项。 大纲
-> 小计
。它会自动创建子和以及折叠数据的可能性。下图演示了该操作如何转换工作表。
这正是我需要通过 POI 执行的操作。我知道如何在单元格中设置小计函数,这样我就可以自己计算中间和。但是如何在左边框上启用这种折叠功能呢?
我意识到有 groupRow()
方法,但这些嵌套组无法正常工作。如果我使用以下代码,我只会得到两组。一大(1-7)和(1-3)。组 (5-7) 丢失,并且更改调用顺序没有任何效果。
sheet.groupRow(1, 7);
sheet.groupRow(1, 3);
sheet.groupRow(5, 7);
Excel provides the Subtotal option from the Menu Data
-> Outline
-> Subtotal
. It creates automatically the sub-sums and the possibility to fold the data. The image below demonstrates how the action transforms the sheet.
And this is exactly what I need to do via POI. I know how to set a subtotal function into a cell so I could calculate the intermediate sums by myself. But how do I enable this folding on the left border?
I realised there is the groupRow()
method but those nested groups doesn't work like they should. If I use the following code I only get two groups. One large (1-7) and (1-3). The group (5-7) is missing and changing the order of the calls has no effect.
sheet.groupRow(1, 7);
sheet.groupRow(1, 3);
sheet.groupRow(5, 7);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我使用的是 POI 的一个相当旧的版本,但这就是我的做法:
我还需要多个嵌套组,因此我有一个存储缩进级别的行模型(它是一棵树,因此缩进是隐式的)。我与访问者一起遍历模型以获取组的开始行号和结束行号。然后调用 HSSFSheet。随后为每个组进行 groupRow 。如果我没记错的话,群组通话的顺序很重要。
I use a quite old verion of POI but this is how I did it:
I also needed multiple nested groups so I had a model for the rows where the indent level was stored as well (it was a tree so the indent was implicit). I traversed the model with a visitor to get the group start and end row numbers. Then called HSSFSheet.groupRow subsequently for each group. If I remember correctly, the order of the group calls is important.
我认为这正是您正在寻找的:
http://www.mysamplecode.com/2011/10/apache-poi-excel-row-group-collapse.html
如果您使用您可以使用
subtotal(9,)
而不是sum()
执行嵌套组,因为小计会忽略其范围内包含小计的单元格I think this is exactly what you are looking for:
http://www.mysamplecode.com/2011/10/apache-poi-excel-row-group-collapse.html
if you use
subtotal(9,<range>)
instead ofsum(<range>)
, you can perform nested groups as subtotal ignores cell with subtotal in its range使用以下库,您可以计算所需的小计。
该库是 apache poi 的包装器。
在源代码下方:
在github上的项目链接下面:
低于结果。
在此处输入图片说明
With following library you can to calculate the subtotals that you want
This library is wrapper of apache poi.
Below the source code:
Below the link of the project on github:
Below the result.
enter image description here