[英]How can I share Pivot Cache between more Apache POI Pivot?
当我尝试打开Apache POI excel文件时遇到问题,该文件包含大量数据(600.000 / 700000行),这些数据提供了在三个不同工作表中生成的三个枢轴(使用流SXSSFWorkbook)。
当我尝试打开创建的Excel di MSExcel时,出现此消息:“ excel无法使用可用的Excel资源完成此活动……尝试关闭某些内容……”。 因此,为了节省资源,我尝试在前两个支点之间共享缓存:好! 好多了! 但是,即使我尝试与第三个缓存共享缓存,MSExcel也会给我错误并尝试还原,但结果错误。
有什么解决方案可以在同一数据源中与两个以上的枢轴共享缓存? 还是我做错了什么?
这是代码:
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());
有问题? 非常感谢你!!!
解决了:
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());
}
谢谢你们
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.