使用 Apache POI 刷新数据透视表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1010673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 17:46:29  来源:igfitidea点击:

Refresh Pivot Table with Apache POI

javaapacheexcelpivot-tableapache-poi

提问by B Jammin

I'm currently working on a Java application that uses a template excel file that contains a pivot table.

我目前正在开发一个 Java 应用程序,该应用程序使用包含数据透视表的模板 excel 文件。

The template file also has a data sheet that seeds the pivot table. This data sheet is dynamically loaded in the java application through the Apache POI api.

模板文件还有一个数据表,它为数据透视表提供种子。该数据表通过Apache POI api 动态加载到java 应用程序中。

When I open the excel file I must refresh the Pivot table manually to get the data loaded correctly.

当我打开 excel 文件时,我必须手动刷新数据透视表才能正确加载数据。

Is there any way to refresh the Pivot table with the POI api so I don't have to manually do it?

有什么方法可以使用 POI api 刷新数据透视表,这样我就不必手动执行了吗?

回答by FrankyFred

You can simple activate an option that will refresh the pivot table every time the file is opened.

您可以简单地激活一个选项,该选项将在每次打开文件时刷新数据透视表。

This Microsoft documentationsays :

这个微软文档说:

In the PivotTable Options dialog box, on the Data tab, select the Refresh data when opening the file check box.

在“数据透视表选项”对话框的“数据”选项卡上,选中“打开文件时刷新数据”复选框。

回答by Solidtubez

It is possible. In the PivotCacheDefinition, there is an attribute refreshOnLoadthat can be set to true. The cache is then refreshed when the workbook is opened. More information here.

有可能的。在 中PivotCacheDefinition,有一个refreshOnLoad可以设置为 的属性true。然后在打开工作簿时刷新缓存。更多信息在这里

In POI this can be done by calling the method setRefreshOnLoad(boolean bool), that takes a boolean as parameter, on a CTPivotCacheDefinition.

在 POI 中,这可以通过调用setRefreshOnLoad(boolean bool)CTPivotCacheDefinition 上的方法来完成,该方法将布尔值作为参数。

EDIT: The Apache POI now provides the possibility to create pivot tables and the pivot table is refreshed on load as default. Class XSSFPivotTable

编辑:Apache POI 现在提供了创建数据透视表的可能性,数据透视表在加载时默认刷新。XSSFPivotTable 类

回答by Trevor Turton

This post says you can turn on an option on the pivot table that will cause it to refresh itself automatically every time the workbook is opened:

这篇文章说你可以打开数据透视表上的一个选项,它会在每次打开工作簿时自动刷新:

How can I refresh all the pivot tables in my excel workbook with a macro?

如何使用宏刷新 Excel 工作簿中的所有数据透视表?

Hopefully this will still be true after you have used Apache POI to refresh or extend the data rows on which the pivot tables are based.

希望在您使用 Apache POI 刷新或扩展数据透视表所基于的数据行后,这仍然适用。

回答by Yishai

The basic answer to this is no. POI is a document format reader and writer. Updating the Pivot table is an excel engine issue. Sure, another application could try to duplicate the Excel engine behavior here, but that is really going to get ugly. I recommend using Joel'sworkaround of accessing the excel COM objects over a webservice to get this kind of thing done.

对此的基本答案是否定的。POI 是一种文档格式读写器。更新数据透视表是一个 excel 引擎问题。当然,另一个应用程序可能会尝试在此处复制 Excel 引擎的行为,但这真的会变得很丑陋。我建议使用Joel 的解决方法,通过 Web 服务访问 excel COM 对象来完成此类操作。

回答by ninjaxelite

This might work:

这可能有效:

XSSFPivotTable pivotTable = pivotSheet.getPivotTables().get(0);     
pivotTable.getPivotCacheDefinition().getCTPivotCacheDefinition().setRefreshOnLoad(true);