XSSF(POI) - 添加"式QUOT;列数据透视表 [英] XSSF (POI) - Adding "formula" column to pivot table

查看:1636
本文介绍了XSSF(POI) - 添加"式QUOT;列数据透视表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用POI 3.12-β1:

I am using POI 3.12-beta1:

<!-- Apache POI (for Excel) -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.12-beta1</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.12-beta1</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>ooxml-schemas</artifactId>
    <version>1.1</version>
</dependency>

我想创建其定义为一个计算的数据透视表列: ='端'/'生成'* 100

我继续手动编辑表在Excel中得到这个工作,当我扭转了 *。XLSX 文件成ZIP目录,并通过它看,我发现在 \\ XL \\ pivotCache \\ pivotCacheDefinition1.xml 以下code:

I went ahead and manually edited the sheet in Excel to get this to work, and when I reversed the *.xlsx file into a ZIP directory and looked through it, I found the following code in \xl\pivotCache\pivotCacheDefinition1.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<pivotCacheDefinition xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" r:id="rId1" refreshOnLoad="1" refreshedBy="vyasrav" refreshedDate="42110.580247453705" createdVersion="3" refreshedVersion="3" minRefreshableVersion="3" recordCount="352">
    <cacheSource type="worksheet">
        <worksheetSource ref="A1:O353" sheet="Data"/>
    </cacheSource>
    <cacheFields count="16">
        <!-- OMITTED -->
        <cacheField name="Avg Pct Processed" numFmtId="0" formula="'Ended' / 'Generated' * 100" databaseField="0"/>
    </cacheFields>
</pivotCacheDefinition>

于是我又回到我的Java程序,并增加了以下code自动生成,但它未在注册数据列15,我得到一个错误IndexOutOfBounds

So I went back to my java program and added the following code to generate it automatically, but it isn't registering data column "15" and I am getting an IndexOutOfBounds error.

// Add pivot (pivot table):
Sheet pivotSheet = workbook.createSheet("Pivot");
LOGGER.trace("Created sheet: '" + String.valueOf(pivotSheet) + "'.");

XSSFPivotTable pivotTable = ((XSSFSheet)pivotSheet).createPivotTable(new AreaReference(tableRange), new CellReference("A1"), dataSheet);
CTPivotTableDefinition ctPivotTableDefinition = pivotTable.getCTPivotTableDefinition();
CTPivotTableStyle ctPivotTableStyle = ctPivotTableDefinition.getPivotTableStyleInfo();
ctPivotTableStyle.setName("PivotStyleMedium4");

// Row Labels:
pivotTable.addRowLabel(...); // ...
...

// Add column 15 (this is a calculated column):
CTCacheFields ctCacheFields = pivotTable.getPivotCacheDefinition().getCTPivotCacheDefinition().getCacheFields();
CTCacheField ctCacheField = ctCacheFields.addNewCacheField();
ctCacheField.setName("Avg Pct Processed");
ctCacheField.setFormula("'Ended' / 'Generated' * 100");

// Column Labels:
pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 8, "Sum of Generated");
pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 12, "Sum of Ended");
pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 13, "Sum of Unended");
pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 15, "Average of Processed Percent");
...

这发生在粗体线以上是IndexOutOfBoundsException异常的堆栈跟踪:

The StackTrace of the IndexOutOfBoundsException which occurs on the bolded line above is:

Exception in thread "main" java.lang.IndexOutOfBoundsException
    at org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTPivotFieldsImpl.setPivotFieldArray(Unknown Source)
    at org.apache.poi.xssf.usermodel.XSSFPivotTable.addDataColumn(XSSFPivotTable.java:372)
    at org.apache.poi.xssf.usermodel.XSSFPivotTable.addColumnLabel(XSSFPivotTable.java:296)
    at com...

有谁知道我该如何使用POI生成此列?

Does anyone know how can I use POI to generate this column?

编辑:

我试过同时使用:

CTPivotTableDefinition ctPivotTableDefinition = pivotTable.getCTPivotTableDefinition();

CTCacheField ctCacheField = ctCacheFields.insertNewCacheField(15);

和在任何情况下,我得到相同的异常此行执行时:

and in either scenario, I get the same exception when this line executes:

pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 15, "Average of Processed Percent");

作为一个说明,我也尝试注释掉,我添加新的列标签行了,当我这样做,如果我打开在Excel 2010工作簿,我收到以下错误消息,当它启动:

As a note, I did try commenting out the line where I add the new column label, and when I do that, if I open up the workbook in Excel 2010, I get the following error message when it starts up:

Removed Feature: PivotTable report from /xl/pivotTables/pivotTable1.xml part (PivotTable view)
Removed Records: Workbook properties from /xl/workbook.xml part (Workbook)

谢谢!

推荐答案

我决定在接下来的方式你的问题:

I resolved your problem in the next way:

//... get or create pivotTable

//Use first column as row label
    pivotTable.addRowLabel(0);
// 1. Add Formula to cache
    addFormulaToCache(pivotTable);
// 2. Add PivotField for Formula column
    addPivotFieldForNewColumn(pivotTable);
// 3. Add all column labels before our function..
    pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 1);
    //Set the third column as filter
    pivotTable.addColumnLabel(DataConsolidateFunction.AVERAGE, 2);
// 4. Add formula column
    addFormulaColumn(pivotTable);

下面是执行方法:

private static void addFormulaToCache(XSSFPivotTable pivotTable) {
    CTCacheFields ctCacheFields = pivotTable.getPivotCacheDefinition().getCTPivotCacheDefinition().getCacheFields();
    CTCacheField ctCacheField = ctCacheFields.addNewCacheField();
    ctCacheField.setName("Field1"); // Any field name
    ctCacheField.setFormula("'Ended' / 'Generated' * 100");
    ctCacheField.setDatabaseField(false);
    ctCacheField.setNumFmtId(0);
    ctCacheFields.setCount(ctCacheFields.sizeOfCacheFieldArray()); //!!! update count of fields directly
}

private static void addPivotFieldForNewColumn(XSSFPivotTable pivotTable) {
    CTPivotField pivotField = pivotTable.getCTPivotTableDefinition().getPivotFields().addNewPivotField();
    pivotField.setDataField(true);
    pivotField.setDragToCol(false);
    pivotField.setDragToPage(false);
    pivotField.setDragToRow(false);
    pivotField.setShowAll(false);
    pivotField.setDefaultSubtotal(false);
}

private static void addFormulaColumn(XSSFPivotTable pivotTable) {
    CTDataFields dataFields;
    if(pivotTable.getCTPivotTableDefinition().getDataFields() != null) {
        dataFields = pivotTable.getCTPivotTableDefinition().getDataFields();
    } else {
        // can be null if we have not added any column labels yet
        dataFields = pivotTable.getCTPivotTableDefinition().addNewDataFields();
    }
    CTDataField dataField = dataFields.addNewDataField();
    dataField.setName("Avg Pct Processed");
    // set index of cached field with formula - it is the last one!!!
    dataField.setFld(pivotTable.getPivotCacheDefinition().getCTPivotCacheDefinition().getCacheFields().getCount()-1);
    dataField.setBaseItem(0);
    dataField.setBaseField(0);
}

这篇关于XSSF(POI) - 添加&QUOT;式QUOT;列数据透视表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆