简体   繁体   English

如何在更多Apache POI Pivot之间共享Pivot缓存?

[英]How can I share Pivot Cache between more Apache POI Pivot?

I have problems when I try to open my Apache POI excel file which consists on a big amount of data (600.000 / 700000 lines) that feeds three pivots generated in three different sheets (using streaming SXSSFWorkbook). 当我尝试打开Apache POI excel文件时遇到问题,该文件包含大量数据(600.000 / 700000行),这些数据提供了在三个不同工作表中生成的三个枢轴(使用流SXSSFWorkbook)。

When I try to open the created Excel di MSExcel, this message comes out: "excel can not complete this activity with the available Excel resources ... try to close something ...". 当我尝试打开创建的Excel di MSExcel时,出现此消息:“ excel无法使用可用的Excel资源完成此活动……尝试关闭某些内容……”。 So, to save resources, I tried to share the cache between the first two pivots: OK! 因此,为了节省资源,我尝试在前两个支点之间共享缓存:好! It's better! 好多了! But when I try to share the cache even with the third one, MSExcel gives me errors and tries to restore but with wrong results. 但是,即使我尝试与第三个缓存共享缓存,MSExcel也会给我错误并尝试还原,但结果错误。

Any solution to share cache with more than two pivots with the same data source? 有什么解决方案可以在同一数据源中与两个以上的枢轴共享缓存? Or am I doing something wrong? 还是我做错了什么?

Here is the code: 这是代码:

XSSFWorkbook wb = new XSSFWorkbook();
XSSFPivotTable firstPivot = preparefirstPivot(...);
XSSFPivotTable secondPivot = preparesecondPivot(...);
XSSFPivotTable thirdPivot = preparethirdPivot(...);

/* START: shared cache */
/* I remove from workbook all PivotCaches except the firstPivot one */
long firstPivotCacheId = firstPivot.getCTPivotTableDefinition().getCacheId();
List<CTPivotCache> ctPivotCacheList = wb.getCTWorkbook().getPivotCaches().getPivotCacheList();
for (int i = 0; i < ctPivotCacheList.size(); i++) {
    CTPivotCache ctPivotCache = ctPivotCacheList.get(i);
    if(ctPivotCache.getCacheId() != firstPivotChaceId ) {
        wb.getCTWorkbook().getPivotCaches().removePivotCache(i);
    }
}
/* I share the firstPivot cache with the other pivots */
/* work! OK! */
secondPivot.getCTPivotTableDefinition().setCacheId(firstPivotCacheId);
secondPivot.setPivotCache(firstPivot.getPivotCache());
/* here does not work! why?!??!! */
thirdPivot.getCTPivotTableDefinition().setCacheId(firstPivotCacheId);
thirdPivot.setPivotCache(firstPivot.getPivotCache());

Something wrong? 有问题? Thank you very much!!! 非常感谢你!!!

Solved: 解决了:

XSSFWorkbook wb = new XSSFWorkbook();
XSSFPivotTable firstPivot = preparefirstPivot(...);
XSSFPivotTable secondPivot = preparesecondPivot(...);
XSSFPivotTable thirdPivot = preparethirdPivot(...);

/* START: shared cache */
                List<CTPivotCache> ctPivotCacheList = wb.getCTWorkbook().getPivotCaches().getPivotCacheList();
                if (ctPivotCacheList != null && ctPivotCacheList.size() > 1) {
                    /* useful only with more than one PivotCache */
                    List<Long> ctPivotCacheIdList = new ArrayList<Long>();
                    for (int i = 0; i < ctPivotCacheList.size(); i++) {
                        CTPivotCache ctPivotCache = ctPivotCacheList.get(i);
                        ctPivotCacheIdList.add(new Long(ctPivotCache.getCacheId()));
                    }

                    long firstPivotCacheId = firstPivot.getCTPivotTableDefinition().getCacheId();
                    for (int i = ctPivotCacheIdList.size() - 1; i >= 0; i--) {
                        if (ctPivotCacheIdList.get(i) != firstPivotCacheId) {
                            ctPivotCacheList.remove(i);
                        }
                    }

                    secondPivot.getCTPivotTableDefinition().setCacheId(firstPivotCacheId);
                    secondPivot.setPivotCache(firstPivot.getPivotCache());
                    thirdPivot.getCTPivotTableDefinition().setCacheId(firstPivotCacheId);
                    thirdPivot.setPivotCache(firstPivot.getPivotCache());
}

Thank you all 谢谢你们

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

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