XSSF (POI) - добавление столбца «формулы» в сводную таблицу

Я использую POI 3.12-бета1:

<!-- 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>

Я пытаюсь создать вычисляемый столбец сводной таблицы, который определяется как:= 'Ended' / 'Generated' * 100.

Я продолжил и вручную отредактировал лист в Excel, чтобы заставить это работать, и когда я полностью изменил*.xlsx файл в ZIP-каталог и просмотрел его, я нашел следующий код в\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-программе и добавил следующий код для его автоматической генерации, но он не регистрирует столбец данных «15», и я получаю ошибку IndexOutOfBounds.

// 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");
...

StackTrace исключения IndexOutOfBoundsException, которое происходит в жирной строке выше:

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 для создания этого столбца?

РЕДАКТИРОВАТЬ:

Я пытался использовать оба:

CTPivotTableDefinition ctPivotTableDefinition = pivotTable.getCTPivotTableDefinition();

а также

CTCacheField ctCacheField = ctCacheFields.insertNewCacheField(15);

и в любом сценарии я получаю то же исключение при выполнении этой строки:

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

В качестве заметки я попытался закомментировать строку, в которую я добавляю метку нового столбца, и когда я это делаю, если я открываю книгу в Excel 2010, я получаю следующее сообщение об ошибке при запуске:

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

Спасибо!

Ответы на вопрос(1)

Ваш ответ на вопрос