Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法在生成.xlsx时触发透视表计算_Java_Excel_Pivot Table_Dynamically Generated - Fatal编程技术网

Java 无法在生成.xlsx时触发透视表计算

Java 无法在生成.xlsx时触发透视表计算,java,excel,pivot-table,dynamically-generated,Java,Excel,Pivot Table,Dynamically Generated,我有一个问题需要一些帮助。 我将首先描述我的问题,然后,如果需要,您可以阅读下面的代码并查看实现细节 简短描述: 我生成一个Excel工作簿,其中包含两张工作表: 表1:通用数据 表2:通用数据的透视表 由于Apache提供的一些POI被证明存在错误,我通过访问的底层XML结构创建了Pivot表。xlsx文件。在这里,我指出了透视表字段和操作(在本例中为计数) 我现在正在设计自动JUnit测试来验证这一点,这就是我遇到麻烦的地方 问题: 生成包含文档的XLSX时,在客户机中打开透视表后,透视表仅

我有一个问题需要一些帮助。 我将首先描述我的问题,然后,如果需要,您可以阅读下面的代码并查看实现细节

简短描述: 我生成一个Excel工作簿,其中包含两张工作表:

表1:通用数据

表2:通用数据的透视表

由于Apache提供的一些POI被证明存在错误,我通过访问的底层XML结构创建了Pivot表。xlsx文件。在这里,我指出了透视表字段和操作(在本例中为计数)

我现在正在设计自动JUnit测试来验证这一点,这就是我遇到麻烦的地方

问题: 生成包含文档的XLSX时,在客户机中打开透视表后,透视表仅填充值

在客户端中打开透视表之前,我想询问是否有一种方法可以通过编程触发透视表。 以下是xlsx文档的底层xml(pivotTable1.xml)的两部分:

在excel客户端打开之前:

<location firstDataCol="1" firstDataRow="1" firstHeaderRow="1" ref="A3:D7"/>
<pivotFields count="8">
<pivotField showAll="false" axis="axisPage">
<items count="8">
<item t="default"/>
<item t="default"/>
<item t="default"/>
    private void createSheets(XSSFWorkbook wb) {
    generalDataSheet = wb.createSheet(GENERAL_DATA_SHEET_NAME);
    pivotTableSheet = wb.createSheet(PIVOT_TABLE_SHEET_NAME);
}
// Pivot table constants:
// where the Table starts with the Report Filter field
public static final String PIVOT_TABLE_SOURCE_START = "A1";
// Where the 2nd part of the pivot table starts with the Sum Values field
public static final String PIVOT_TABLE_DATA_START = "A3:B3";
private static final String PIVOT_TABLE_NAME = " Pivot Table";

private static final int INTERFACE_NAME_CELL_POS = 0;
private static final int PROVIDER_NAME_CELL_POS = 4;
private static final int REQUESTER_NAME_CELL_POS = 6;

…
private void populatePivotTableSheet(List<MyDataSet> list) {
//Set position of the pivot table in sheet
    CellReference pivotTableCellPosition = new CellReference(PIVOT_TABLE_SOURCE_START); 
    //set source area for the pivot table
    AreaReference pivotTableSourceArea = getDefaultPivotTableSourceArea(list);
// create pivot table and set attributespivotTable = new PivotTableMyTools(pivotTableSourceArea, PIVOT_TABLE_NAME);
    pivotTable.createPivotTable(pivotTableSheet, pivotTableCellPosition);
    // set the size of the pivot Table - this is because of a bug in regular API
    pivotTable.setRefField(PIVOT_TABLE_DATA_START);
    pivotTable.addRowLabelsField(PROVIDER_NAME_CELL_POS);
    pivotTable.addColumnLabelsField(REQUESTER_NAME_CELL_POS);
    pivotTable.addReportFilterField(INTERFACE_NAME_CELL_POS);
pivotTable.addSumValuesField(DataConsolidateFunction.COUNT,PROVIDER_NAME_CELL_POS);
    }
private AreaReference getDefaultPivotTableSourceArea(Object linkSetList) {

List< MyDataSet > list = (List< MyDataSet >) DataSetList;
// construct the target area of the Pivot table
// start cell is calculated as for ex: "General data!A2"
CellReference c1 = new CellReference(GENERAL_DATA_SHEET_NAME + "!" + PIVOT_TABLE_SOURCE_START);
String colName = CellReference.convertNumToColString(COLUMN_HEADERS.length - 1);
// end cell is calculated as for ex: "General data!H5"
CellReference c2 = new CellReference(GENERAL_DATA_SHEET_NAME + "!" + colName + (list.size() + 1));

return new AreaReference(c1, c2);
}
    public class PivotTableMyTools extends XSSFPivotTable implements IPivotTableMyTools {

    private XSSFSheet pivotTableSheet; // Sheet displaying information in pivot
    private AreaReference sourceDataArea;
    private XSSFPivotTable pivotTable;
    private int numberOfDataFields;
    private String pivotTableName;

public PivotTableMyTools(AreaReference sourceDataArea, String pivotTableName) {

        this.sourceDataArea = sourceDataArea;
        numberOfDataFields = 0;
        this.pivotTableName = pivotTableName;
    }

@Override
public void createPivotTable(XSSFSheet destinationSheet, CellReference pivotTableCellPosition) {

        pivotTableSheet = destinationSheet;
        pivotTable = pivotTableSheet.createPivotTable(sourceDataArea, pivotTableCellPosition);
        pivotTable.getCTPivotTableDefinition().setName(pivotTableName);
    }

// int fieldID is the ID of the field in the list of fields to be added to
// the report (column headers of the source data area)
@Override
public void addReportFilterField(int fieldID) {

    int lastColIndex = getSourceAreaLastColumnIndex();
    // create new pivot field with Column Specifications
    try {
        // throws index out of bounds
        checkColumnIndexOutOfBounds(fieldID, lastColIndex);
        // add pivot field to PivotTable, lastColindex also indicates the
        // number of columns
        addNewPivotField(fieldID, lastColIndex, STAxis.AXIS_PAGE);
        // Columns labels colField should be added.
        addNewCTPageField(fieldID);

    } catch (IndexOutOfBoundsException e) {
        Activator.logInfo("Column index is out of bounds");
        Activator.logError(e.getMessage());
    }

}



private void addNewCTPageField(int columnIndex) {

        CTPageFields pageFields;
        if (pivotTable.getCTPivotTableDefinition().getPageFields() != null) {
            pageFields = pivotTable.getCTPivotTableDefinition().getPageFields();
        } else {
            pageFields = pivotTable.getCTPivotTableDefinition().addNewPageFields();
        }
        // Set the fld and hier attributes
        CTPageField pageField = pageFields.addNewPageField();
        pageField.setFld(columnIndex);
        pageField.setHier(-1);
        // set the count attribute
        pageFields.setCount(pageFields.sizeOfPageFieldArray());

    }



@Override
    public void addRowLabelsField(int columnIndex) {

        pivotTable.addRowLabel(columnIndex);
    }

    @Override
    public void addColumnLabelsField(int columnIndex) {

        int lastColIndex = getSourceAreaLastColumnIndex();
        // create new pivot field with Column Specifications
        try {
            // throws index out of bounds
            checkColumnIndexOutOfBounds(columnIndex, lastColIndex);
            // add pivot field to PivotTable, lastColindex also indicates the
            // number of columns
            addNewPivotField(columnIndex, lastColIndex, STAxis.AXIS_COL);
            // Columns labels colField should be added.
            addNewCTColField(columnIndex);

        } catch (IndexOutOfBoundsException e) {
            Activator.logInfo("Column index is out of bounds");
            Activator.logError(e.getMessage());
        }
    }

    @Override
    public void addSumValuesField(DataConsolidateFunction function, int fieldID) {

        // pivotTable.addColumnLabel(DataConsolidateFunction.COUNT,
        // PROVIDER_NAME_CELL_POS, "Provider count");
        try {
            CTPivotField pivotField = getPivotField(fieldID);
            pivotField.setDataField(true);
        } catch (IndexOutOfBoundsException e) {
            Activator.logInfo("The selected column is out of current range");
            Activator.logError(e.getMessage());
        }

        addNewCTDataField(fieldID, "Count of Provider");

    }



private void addNewCTDataField(int fieldID, String fieldName) {

        numberOfDataFields++;
        CTDataFields dataFields = pivotTable.getCTPivotTableDefinition().addNewDataFields();
        CTDataField dataField = dataFields.addNewDataField();
        dataField.setName(fieldName);
        dataField.setFld(fieldID);
        dataField.setSubtotal(STDataConsolidateFunction.COUNT);
        dataField.setBaseField(0);
        dataField.setBaseItem(0);
        dataFields.setCount(numberOfDataFields);
    }

    private CTPivotField getPivotField(int fieldID) throws IndexOutOfBoundsException {

        CTPivotFields pivotFields = pivotTable.getCTPivotTableDefinition().getPivotFields();
        if (null == pivotFields)
            throw new IndexOutOfBoundsException();
        return pivotFields.getPivotFieldArray(4);
    }

    @Override
    public AreaReference getPivotTableSourceArea() {

        return sourceDataArea;
    }

    @Override
    public int getSourceAreaLastColumnIndex() {

        return (sourceDataArea.getLastCell().getCol() - sourceDataArea.getFirstCell().getCol());
    }

    @Override
    public void setRefField(String pivotTableFieldArea) {

        CTLocation location = pivotTable.getCTPivotTableDefinition().getLocation();
        location.setRef("A3:D7");
    }



/***************** private methods ***********************************/

    private void addNewCTColField(int columnIndex) {

        CTColFields colFields;
        if (pivotTable.getCTPivotTableDefinition().getColFields() != null) {
            colFields = pivotTable.getCTPivotTableDefinition().getColFields();
        } else {
            colFields = pivotTable.getCTPivotTableDefinition().addNewColFields();
        }
        colFields.addNewField().setX(columnIndex);
        colFields.setCount(colFields.sizeOfFieldArray());
    }

    private void addNewPivotField(int columnIndex, int numberOfItems, Enum axisValue) {

        IPivotFieldARTools pivotField = new PivotFieldARTools();
        pivotField.setAxis(axisValue);
        pivotField.createItemsList(numberOfItems);
        pivotField.addToPivotTable(columnIndex, pivotTable);
    }

    private void checkColumnIndexOutOfBounds(int columnIndex, int lastColIndex) throws IndexOutOfBoundsException {

        if (columnIndex > lastColIndex && columnIndex < 0) {
            throw new IndexOutOfBoundsException();
        }
    }
数据透视表的实现和使用详细信息:

<location firstDataCol="1" firstDataRow="1" firstHeaderRow="1" ref="A3:D7"/>
<pivotFields count="8">
<pivotField showAll="false" axis="axisPage">
<items count="8">
<item t="default"/>
<item t="default"/>
<item t="default"/>
    private void createSheets(XSSFWorkbook wb) {
    generalDataSheet = wb.createSheet(GENERAL_DATA_SHEET_NAME);
    pivotTableSheet = wb.createSheet(PIVOT_TABLE_SHEET_NAME);
}
// Pivot table constants:
// where the Table starts with the Report Filter field
public static final String PIVOT_TABLE_SOURCE_START = "A1";
// Where the 2nd part of the pivot table starts with the Sum Values field
public static final String PIVOT_TABLE_DATA_START = "A3:B3";
private static final String PIVOT_TABLE_NAME = " Pivot Table";

private static final int INTERFACE_NAME_CELL_POS = 0;
private static final int PROVIDER_NAME_CELL_POS = 4;
private static final int REQUESTER_NAME_CELL_POS = 6;

…
private void populatePivotTableSheet(List<MyDataSet> list) {
//Set position of the pivot table in sheet
    CellReference pivotTableCellPosition = new CellReference(PIVOT_TABLE_SOURCE_START); 
    //set source area for the pivot table
    AreaReference pivotTableSourceArea = getDefaultPivotTableSourceArea(list);
// create pivot table and set attributespivotTable = new PivotTableMyTools(pivotTableSourceArea, PIVOT_TABLE_NAME);
    pivotTable.createPivotTable(pivotTableSheet, pivotTableCellPosition);
    // set the size of the pivot Table - this is because of a bug in regular API
    pivotTable.setRefField(PIVOT_TABLE_DATA_START);
    pivotTable.addRowLabelsField(PROVIDER_NAME_CELL_POS);
    pivotTable.addColumnLabelsField(REQUESTER_NAME_CELL_POS);
    pivotTable.addReportFilterField(INTERFACE_NAME_CELL_POS);
pivotTable.addSumValuesField(DataConsolidateFunction.COUNT,PROVIDER_NAME_CELL_POS);
    }
private AreaReference getDefaultPivotTableSourceArea(Object linkSetList) {

List< MyDataSet > list = (List< MyDataSet >) DataSetList;
// construct the target area of the Pivot table
// start cell is calculated as for ex: "General data!A2"
CellReference c1 = new CellReference(GENERAL_DATA_SHEET_NAME + "!" + PIVOT_TABLE_SOURCE_START);
String colName = CellReference.convertNumToColString(COLUMN_HEADERS.length - 1);
// end cell is calculated as for ex: "General data!H5"
CellReference c2 = new CellReference(GENERAL_DATA_SHEET_NAME + "!" + colName + (list.size() + 1));

return new AreaReference(c1, c2);
}
    public class PivotTableMyTools extends XSSFPivotTable implements IPivotTableMyTools {

    private XSSFSheet pivotTableSheet; // Sheet displaying information in pivot
    private AreaReference sourceDataArea;
    private XSSFPivotTable pivotTable;
    private int numberOfDataFields;
    private String pivotTableName;

public PivotTableMyTools(AreaReference sourceDataArea, String pivotTableName) {

        this.sourceDataArea = sourceDataArea;
        numberOfDataFields = 0;
        this.pivotTableName = pivotTableName;
    }

@Override
public void createPivotTable(XSSFSheet destinationSheet, CellReference pivotTableCellPosition) {

        pivotTableSheet = destinationSheet;
        pivotTable = pivotTableSheet.createPivotTable(sourceDataArea, pivotTableCellPosition);
        pivotTable.getCTPivotTableDefinition().setName(pivotTableName);
    }

// int fieldID is the ID of the field in the list of fields to be added to
// the report (column headers of the source data area)
@Override
public void addReportFilterField(int fieldID) {

    int lastColIndex = getSourceAreaLastColumnIndex();
    // create new pivot field with Column Specifications
    try {
        // throws index out of bounds
        checkColumnIndexOutOfBounds(fieldID, lastColIndex);
        // add pivot field to PivotTable, lastColindex also indicates the
        // number of columns
        addNewPivotField(fieldID, lastColIndex, STAxis.AXIS_PAGE);
        // Columns labels colField should be added.
        addNewCTPageField(fieldID);

    } catch (IndexOutOfBoundsException e) {
        Activator.logInfo("Column index is out of bounds");
        Activator.logError(e.getMessage());
    }

}



private void addNewCTPageField(int columnIndex) {

        CTPageFields pageFields;
        if (pivotTable.getCTPivotTableDefinition().getPageFields() != null) {
            pageFields = pivotTable.getCTPivotTableDefinition().getPageFields();
        } else {
            pageFields = pivotTable.getCTPivotTableDefinition().addNewPageFields();
        }
        // Set the fld and hier attributes
        CTPageField pageField = pageFields.addNewPageField();
        pageField.setFld(columnIndex);
        pageField.setHier(-1);
        // set the count attribute
        pageFields.setCount(pageFields.sizeOfPageFieldArray());

    }



@Override
    public void addRowLabelsField(int columnIndex) {

        pivotTable.addRowLabel(columnIndex);
    }

    @Override
    public void addColumnLabelsField(int columnIndex) {

        int lastColIndex = getSourceAreaLastColumnIndex();
        // create new pivot field with Column Specifications
        try {
            // throws index out of bounds
            checkColumnIndexOutOfBounds(columnIndex, lastColIndex);
            // add pivot field to PivotTable, lastColindex also indicates the
            // number of columns
            addNewPivotField(columnIndex, lastColIndex, STAxis.AXIS_COL);
            // Columns labels colField should be added.
            addNewCTColField(columnIndex);

        } catch (IndexOutOfBoundsException e) {
            Activator.logInfo("Column index is out of bounds");
            Activator.logError(e.getMessage());
        }
    }

    @Override
    public void addSumValuesField(DataConsolidateFunction function, int fieldID) {

        // pivotTable.addColumnLabel(DataConsolidateFunction.COUNT,
        // PROVIDER_NAME_CELL_POS, "Provider count");
        try {
            CTPivotField pivotField = getPivotField(fieldID);
            pivotField.setDataField(true);
        } catch (IndexOutOfBoundsException e) {
            Activator.logInfo("The selected column is out of current range");
            Activator.logError(e.getMessage());
        }

        addNewCTDataField(fieldID, "Count of Provider");

    }



private void addNewCTDataField(int fieldID, String fieldName) {

        numberOfDataFields++;
        CTDataFields dataFields = pivotTable.getCTPivotTableDefinition().addNewDataFields();
        CTDataField dataField = dataFields.addNewDataField();
        dataField.setName(fieldName);
        dataField.setFld(fieldID);
        dataField.setSubtotal(STDataConsolidateFunction.COUNT);
        dataField.setBaseField(0);
        dataField.setBaseItem(0);
        dataFields.setCount(numberOfDataFields);
    }

    private CTPivotField getPivotField(int fieldID) throws IndexOutOfBoundsException {

        CTPivotFields pivotFields = pivotTable.getCTPivotTableDefinition().getPivotFields();
        if (null == pivotFields)
            throw new IndexOutOfBoundsException();
        return pivotFields.getPivotFieldArray(4);
    }

    @Override
    public AreaReference getPivotTableSourceArea() {

        return sourceDataArea;
    }

    @Override
    public int getSourceAreaLastColumnIndex() {

        return (sourceDataArea.getLastCell().getCol() - sourceDataArea.getFirstCell().getCol());
    }

    @Override
    public void setRefField(String pivotTableFieldArea) {

        CTLocation location = pivotTable.getCTPivotTableDefinition().getLocation();
        location.setRef("A3:D7");
    }



/***************** private methods ***********************************/

    private void addNewCTColField(int columnIndex) {

        CTColFields colFields;
        if (pivotTable.getCTPivotTableDefinition().getColFields() != null) {
            colFields = pivotTable.getCTPivotTableDefinition().getColFields();
        } else {
            colFields = pivotTable.getCTPivotTableDefinition().addNewColFields();
        }
        colFields.addNewField().setX(columnIndex);
        colFields.setCount(colFields.sizeOfFieldArray());
    }

    private void addNewPivotField(int columnIndex, int numberOfItems, Enum axisValue) {

        IPivotFieldARTools pivotField = new PivotFieldARTools();
        pivotField.setAxis(axisValue);
        pivotField.createItemsList(numberOfItems);
        pivotField.addToPivotTable(columnIndex, pivotTable);
    }

    private void checkColumnIndexOutOfBounds(int columnIndex, int lastColIndex) throws IndexOutOfBoundsException {

        if (columnIndex > lastColIndex && columnIndex < 0) {
            throw new IndexOutOfBoundsException();
        }
    }
//透视表常量:
//其中,表格以“报告过滤器”字段开头
公共静态最终字符串透视\u表\u源\u开始=“A1”;
//其中,数据透视表的第二部分以“总和值”字段开始
公共静态最终字符串透视\u表\u数据\u开始=“A3:B3”;
私有静态最终字符串PIVOT\u TABLE\u NAME=“PIVOT TABLE”;
私有静态最终int接口\u NAME\u CELL\u POS=0;
私有静态最终整数提供程序\u NAME\u CELL\u POS=4;
私有静态最终整数请求者\名称\单元\位置=6;
…
私有作废populatePivotTableSheet(列表){
//在图纸中设置透视表的位置
CellReference pivotTableCellPosition=新的CellReference(PIVOT\u TABLE\u SOURCE\u START);
//设置数据透视表的源区域
AreaReference pivotTableSourceArea=getDefaultPivotTableSourceArea(列表);
//创建数据透视表并设置AttributesPivoTable=new PivotTableMyTools(数据透视表资源区域,数据透视表名称);
创建数据透视表(数据透视表,数据透视表单元格位置);
//设置透视表的大小-这是因为常规API中存在错误
数据透视表.setRefField(数据透视表数据透视开始);
数据透视表.addRowLabelsField(提供者名称单元格位置);
数据透视表.addColumnLabelsField(请求者名称单元格位置);
数据透视表.addReportFilterField(接口名称单元格位置);
数据透视表.addSumValuesField(DataConsolidateFunction.COUNT,提供程序\名称\单元格\位置);
}
我得到数据透视表的源区域,如:

<location firstDataCol="1" firstDataRow="1" firstHeaderRow="1" ref="A3:D7"/>
<pivotFields count="8">
<pivotField showAll="false" axis="axisPage">
<items count="8">
<item t="default"/>
<item t="default"/>
<item t="default"/>
    private void createSheets(XSSFWorkbook wb) {
    generalDataSheet = wb.createSheet(GENERAL_DATA_SHEET_NAME);
    pivotTableSheet = wb.createSheet(PIVOT_TABLE_SHEET_NAME);
}
// Pivot table constants:
// where the Table starts with the Report Filter field
public static final String PIVOT_TABLE_SOURCE_START = "A1";
// Where the 2nd part of the pivot table starts with the Sum Values field
public static final String PIVOT_TABLE_DATA_START = "A3:B3";
private static final String PIVOT_TABLE_NAME = " Pivot Table";

private static final int INTERFACE_NAME_CELL_POS = 0;
private static final int PROVIDER_NAME_CELL_POS = 4;
private static final int REQUESTER_NAME_CELL_POS = 6;

…
private void populatePivotTableSheet(List<MyDataSet> list) {
//Set position of the pivot table in sheet
    CellReference pivotTableCellPosition = new CellReference(PIVOT_TABLE_SOURCE_START); 
    //set source area for the pivot table
    AreaReference pivotTableSourceArea = getDefaultPivotTableSourceArea(list);
// create pivot table and set attributespivotTable = new PivotTableMyTools(pivotTableSourceArea, PIVOT_TABLE_NAME);
    pivotTable.createPivotTable(pivotTableSheet, pivotTableCellPosition);
    // set the size of the pivot Table - this is because of a bug in regular API
    pivotTable.setRefField(PIVOT_TABLE_DATA_START);
    pivotTable.addRowLabelsField(PROVIDER_NAME_CELL_POS);
    pivotTable.addColumnLabelsField(REQUESTER_NAME_CELL_POS);
    pivotTable.addReportFilterField(INTERFACE_NAME_CELL_POS);
pivotTable.addSumValuesField(DataConsolidateFunction.COUNT,PROVIDER_NAME_CELL_POS);
    }
private AreaReference getDefaultPivotTableSourceArea(Object linkSetList) {

List< MyDataSet > list = (List< MyDataSet >) DataSetList;
// construct the target area of the Pivot table
// start cell is calculated as for ex: "General data!A2"
CellReference c1 = new CellReference(GENERAL_DATA_SHEET_NAME + "!" + PIVOT_TABLE_SOURCE_START);
String colName = CellReference.convertNumToColString(COLUMN_HEADERS.length - 1);
// end cell is calculated as for ex: "General data!H5"
CellReference c2 = new CellReference(GENERAL_DATA_SHEET_NAME + "!" + colName + (list.size() + 1));

return new AreaReference(c1, c2);
}
    public class PivotTableMyTools extends XSSFPivotTable implements IPivotTableMyTools {

    private XSSFSheet pivotTableSheet; // Sheet displaying information in pivot
    private AreaReference sourceDataArea;
    private XSSFPivotTable pivotTable;
    private int numberOfDataFields;
    private String pivotTableName;

public PivotTableMyTools(AreaReference sourceDataArea, String pivotTableName) {

        this.sourceDataArea = sourceDataArea;
        numberOfDataFields = 0;
        this.pivotTableName = pivotTableName;
    }

@Override
public void createPivotTable(XSSFSheet destinationSheet, CellReference pivotTableCellPosition) {

        pivotTableSheet = destinationSheet;
        pivotTable = pivotTableSheet.createPivotTable(sourceDataArea, pivotTableCellPosition);
        pivotTable.getCTPivotTableDefinition().setName(pivotTableName);
    }

// int fieldID is the ID of the field in the list of fields to be added to
// the report (column headers of the source data area)
@Override
public void addReportFilterField(int fieldID) {

    int lastColIndex = getSourceAreaLastColumnIndex();
    // create new pivot field with Column Specifications
    try {
        // throws index out of bounds
        checkColumnIndexOutOfBounds(fieldID, lastColIndex);
        // add pivot field to PivotTable, lastColindex also indicates the
        // number of columns
        addNewPivotField(fieldID, lastColIndex, STAxis.AXIS_PAGE);
        // Columns labels colField should be added.
        addNewCTPageField(fieldID);

    } catch (IndexOutOfBoundsException e) {
        Activator.logInfo("Column index is out of bounds");
        Activator.logError(e.getMessage());
    }

}



private void addNewCTPageField(int columnIndex) {

        CTPageFields pageFields;
        if (pivotTable.getCTPivotTableDefinition().getPageFields() != null) {
            pageFields = pivotTable.getCTPivotTableDefinition().getPageFields();
        } else {
            pageFields = pivotTable.getCTPivotTableDefinition().addNewPageFields();
        }
        // Set the fld and hier attributes
        CTPageField pageField = pageFields.addNewPageField();
        pageField.setFld(columnIndex);
        pageField.setHier(-1);
        // set the count attribute
        pageFields.setCount(pageFields.sizeOfPageFieldArray());

    }



@Override
    public void addRowLabelsField(int columnIndex) {

        pivotTable.addRowLabel(columnIndex);
    }

    @Override
    public void addColumnLabelsField(int columnIndex) {

        int lastColIndex = getSourceAreaLastColumnIndex();
        // create new pivot field with Column Specifications
        try {
            // throws index out of bounds
            checkColumnIndexOutOfBounds(columnIndex, lastColIndex);
            // add pivot field to PivotTable, lastColindex also indicates the
            // number of columns
            addNewPivotField(columnIndex, lastColIndex, STAxis.AXIS_COL);
            // Columns labels colField should be added.
            addNewCTColField(columnIndex);

        } catch (IndexOutOfBoundsException e) {
            Activator.logInfo("Column index is out of bounds");
            Activator.logError(e.getMessage());
        }
    }

    @Override
    public void addSumValuesField(DataConsolidateFunction function, int fieldID) {

        // pivotTable.addColumnLabel(DataConsolidateFunction.COUNT,
        // PROVIDER_NAME_CELL_POS, "Provider count");
        try {
            CTPivotField pivotField = getPivotField(fieldID);
            pivotField.setDataField(true);
        } catch (IndexOutOfBoundsException e) {
            Activator.logInfo("The selected column is out of current range");
            Activator.logError(e.getMessage());
        }

        addNewCTDataField(fieldID, "Count of Provider");

    }



private void addNewCTDataField(int fieldID, String fieldName) {

        numberOfDataFields++;
        CTDataFields dataFields = pivotTable.getCTPivotTableDefinition().addNewDataFields();
        CTDataField dataField = dataFields.addNewDataField();
        dataField.setName(fieldName);
        dataField.setFld(fieldID);
        dataField.setSubtotal(STDataConsolidateFunction.COUNT);
        dataField.setBaseField(0);
        dataField.setBaseItem(0);
        dataFields.setCount(numberOfDataFields);
    }

    private CTPivotField getPivotField(int fieldID) throws IndexOutOfBoundsException {

        CTPivotFields pivotFields = pivotTable.getCTPivotTableDefinition().getPivotFields();
        if (null == pivotFields)
            throw new IndexOutOfBoundsException();
        return pivotFields.getPivotFieldArray(4);
    }

    @Override
    public AreaReference getPivotTableSourceArea() {

        return sourceDataArea;
    }

    @Override
    public int getSourceAreaLastColumnIndex() {

        return (sourceDataArea.getLastCell().getCol() - sourceDataArea.getFirstCell().getCol());
    }

    @Override
    public void setRefField(String pivotTableFieldArea) {

        CTLocation location = pivotTable.getCTPivotTableDefinition().getLocation();
        location.setRef("A3:D7");
    }



/***************** private methods ***********************************/

    private void addNewCTColField(int columnIndex) {

        CTColFields colFields;
        if (pivotTable.getCTPivotTableDefinition().getColFields() != null) {
            colFields = pivotTable.getCTPivotTableDefinition().getColFields();
        } else {
            colFields = pivotTable.getCTPivotTableDefinition().addNewColFields();
        }
        colFields.addNewField().setX(columnIndex);
        colFields.setCount(colFields.sizeOfFieldArray());
    }

    private void addNewPivotField(int columnIndex, int numberOfItems, Enum axisValue) {

        IPivotFieldARTools pivotField = new PivotFieldARTools();
        pivotField.setAxis(axisValue);
        pivotField.createItemsList(numberOfItems);
        pivotField.addToPivotTable(columnIndex, pivotTable);
    }

    private void checkColumnIndexOutOfBounds(int columnIndex, int lastColIndex) throws IndexOutOfBoundsException {

        if (columnIndex > lastColIndex && columnIndex < 0) {
            throw new IndexOutOfBoundsException();
        }
    }
私有区域引用getDefaultPivotTableSourceArea(对象链接集列表){
ListList=(List)数据集列表;
//构造透视表的目标区域
//起始单元格的计算公式如下:“常规数据!A2”
CellReference c1=新的CellReference(通用数据表名称+“!”+数据透视表源开始);
String colName=CellReference.convertNumToColString(COLUMN_HEADERS.length-1);
//结束单元格的计算公式如下:“一般数据!H5”
CellReference c2=新的CellReference(通用数据表名称+“!”+colName+(list.size()+1));
返回新区域参考(c1、c2);
}
然后,我使用自己的透视表类覆盖一些方法:

<location firstDataCol="1" firstDataRow="1" firstHeaderRow="1" ref="A3:D7"/>
<pivotFields count="8">
<pivotField showAll="false" axis="axisPage">
<items count="8">
<item t="default"/>
<item t="default"/>
<item t="default"/>
    private void createSheets(XSSFWorkbook wb) {
    generalDataSheet = wb.createSheet(GENERAL_DATA_SHEET_NAME);
    pivotTableSheet = wb.createSheet(PIVOT_TABLE_SHEET_NAME);
}
// Pivot table constants:
// where the Table starts with the Report Filter field
public static final String PIVOT_TABLE_SOURCE_START = "A1";
// Where the 2nd part of the pivot table starts with the Sum Values field
public static final String PIVOT_TABLE_DATA_START = "A3:B3";
private static final String PIVOT_TABLE_NAME = " Pivot Table";

private static final int INTERFACE_NAME_CELL_POS = 0;
private static final int PROVIDER_NAME_CELL_POS = 4;
private static final int REQUESTER_NAME_CELL_POS = 6;

…
private void populatePivotTableSheet(List<MyDataSet> list) {
//Set position of the pivot table in sheet
    CellReference pivotTableCellPosition = new CellReference(PIVOT_TABLE_SOURCE_START); 
    //set source area for the pivot table
    AreaReference pivotTableSourceArea = getDefaultPivotTableSourceArea(list);
// create pivot table and set attributespivotTable = new PivotTableMyTools(pivotTableSourceArea, PIVOT_TABLE_NAME);
    pivotTable.createPivotTable(pivotTableSheet, pivotTableCellPosition);
    // set the size of the pivot Table - this is because of a bug in regular API
    pivotTable.setRefField(PIVOT_TABLE_DATA_START);
    pivotTable.addRowLabelsField(PROVIDER_NAME_CELL_POS);
    pivotTable.addColumnLabelsField(REQUESTER_NAME_CELL_POS);
    pivotTable.addReportFilterField(INTERFACE_NAME_CELL_POS);
pivotTable.addSumValuesField(DataConsolidateFunction.COUNT,PROVIDER_NAME_CELL_POS);
    }
private AreaReference getDefaultPivotTableSourceArea(Object linkSetList) {

List< MyDataSet > list = (List< MyDataSet >) DataSetList;
// construct the target area of the Pivot table
// start cell is calculated as for ex: "General data!A2"
CellReference c1 = new CellReference(GENERAL_DATA_SHEET_NAME + "!" + PIVOT_TABLE_SOURCE_START);
String colName = CellReference.convertNumToColString(COLUMN_HEADERS.length - 1);
// end cell is calculated as for ex: "General data!H5"
CellReference c2 = new CellReference(GENERAL_DATA_SHEET_NAME + "!" + colName + (list.size() + 1));

return new AreaReference(c1, c2);
}
    public class PivotTableMyTools extends XSSFPivotTable implements IPivotTableMyTools {

    private XSSFSheet pivotTableSheet; // Sheet displaying information in pivot
    private AreaReference sourceDataArea;
    private XSSFPivotTable pivotTable;
    private int numberOfDataFields;
    private String pivotTableName;

public PivotTableMyTools(AreaReference sourceDataArea, String pivotTableName) {

        this.sourceDataArea = sourceDataArea;
        numberOfDataFields = 0;
        this.pivotTableName = pivotTableName;
    }

@Override
public void createPivotTable(XSSFSheet destinationSheet, CellReference pivotTableCellPosition) {

        pivotTableSheet = destinationSheet;
        pivotTable = pivotTableSheet.createPivotTable(sourceDataArea, pivotTableCellPosition);
        pivotTable.getCTPivotTableDefinition().setName(pivotTableName);
    }

// int fieldID is the ID of the field in the list of fields to be added to
// the report (column headers of the source data area)
@Override
public void addReportFilterField(int fieldID) {

    int lastColIndex = getSourceAreaLastColumnIndex();
    // create new pivot field with Column Specifications
    try {
        // throws index out of bounds
        checkColumnIndexOutOfBounds(fieldID, lastColIndex);
        // add pivot field to PivotTable, lastColindex also indicates the
        // number of columns
        addNewPivotField(fieldID, lastColIndex, STAxis.AXIS_PAGE);
        // Columns labels colField should be added.
        addNewCTPageField(fieldID);

    } catch (IndexOutOfBoundsException e) {
        Activator.logInfo("Column index is out of bounds");
        Activator.logError(e.getMessage());
    }

}



private void addNewCTPageField(int columnIndex) {

        CTPageFields pageFields;
        if (pivotTable.getCTPivotTableDefinition().getPageFields() != null) {
            pageFields = pivotTable.getCTPivotTableDefinition().getPageFields();
        } else {
            pageFields = pivotTable.getCTPivotTableDefinition().addNewPageFields();
        }
        // Set the fld and hier attributes
        CTPageField pageField = pageFields.addNewPageField();
        pageField.setFld(columnIndex);
        pageField.setHier(-1);
        // set the count attribute
        pageFields.setCount(pageFields.sizeOfPageFieldArray());

    }



@Override
    public void addRowLabelsField(int columnIndex) {

        pivotTable.addRowLabel(columnIndex);
    }

    @Override
    public void addColumnLabelsField(int columnIndex) {

        int lastColIndex = getSourceAreaLastColumnIndex();
        // create new pivot field with Column Specifications
        try {
            // throws index out of bounds
            checkColumnIndexOutOfBounds(columnIndex, lastColIndex);
            // add pivot field to PivotTable, lastColindex also indicates the
            // number of columns
            addNewPivotField(columnIndex, lastColIndex, STAxis.AXIS_COL);
            // Columns labels colField should be added.
            addNewCTColField(columnIndex);

        } catch (IndexOutOfBoundsException e) {
            Activator.logInfo("Column index is out of bounds");
            Activator.logError(e.getMessage());
        }
    }

    @Override
    public void addSumValuesField(DataConsolidateFunction function, int fieldID) {

        // pivotTable.addColumnLabel(DataConsolidateFunction.COUNT,
        // PROVIDER_NAME_CELL_POS, "Provider count");
        try {
            CTPivotField pivotField = getPivotField(fieldID);
            pivotField.setDataField(true);
        } catch (IndexOutOfBoundsException e) {
            Activator.logInfo("The selected column is out of current range");
            Activator.logError(e.getMessage());
        }

        addNewCTDataField(fieldID, "Count of Provider");

    }



private void addNewCTDataField(int fieldID, String fieldName) {

        numberOfDataFields++;
        CTDataFields dataFields = pivotTable.getCTPivotTableDefinition().addNewDataFields();
        CTDataField dataField = dataFields.addNewDataField();
        dataField.setName(fieldName);
        dataField.setFld(fieldID);
        dataField.setSubtotal(STDataConsolidateFunction.COUNT);
        dataField.setBaseField(0);
        dataField.setBaseItem(0);
        dataFields.setCount(numberOfDataFields);
    }

    private CTPivotField getPivotField(int fieldID) throws IndexOutOfBoundsException {

        CTPivotFields pivotFields = pivotTable.getCTPivotTableDefinition().getPivotFields();
        if (null == pivotFields)
            throw new IndexOutOfBoundsException();
        return pivotFields.getPivotFieldArray(4);
    }

    @Override
    public AreaReference getPivotTableSourceArea() {

        return sourceDataArea;
    }

    @Override
    public int getSourceAreaLastColumnIndex() {

        return (sourceDataArea.getLastCell().getCol() - sourceDataArea.getFirstCell().getCol());
    }

    @Override
    public void setRefField(String pivotTableFieldArea) {

        CTLocation location = pivotTable.getCTPivotTableDefinition().getLocation();
        location.setRef("A3:D7");
    }



/***************** private methods ***********************************/

    private void addNewCTColField(int columnIndex) {

        CTColFields colFields;
        if (pivotTable.getCTPivotTableDefinition().getColFields() != null) {
            colFields = pivotTable.getCTPivotTableDefinition().getColFields();
        } else {
            colFields = pivotTable.getCTPivotTableDefinition().addNewColFields();
        }
        colFields.addNewField().setX(columnIndex);
        colFields.setCount(colFields.sizeOfFieldArray());
    }

    private void addNewPivotField(int columnIndex, int numberOfItems, Enum axisValue) {

        IPivotFieldARTools pivotField = new PivotFieldARTools();
        pivotField.setAxis(axisValue);
        pivotField.createItemsList(numberOfItems);
        pivotField.addToPivotTable(columnIndex, pivotTable);
    }

    private void checkColumnIndexOutOfBounds(int columnIndex, int lastColIndex) throws IndexOutOfBoundsException {

        if (columnIndex > lastColIndex && columnIndex < 0) {
            throw new IndexOutOfBoundsException();
        }
    }
公共类PivotTableMyTools扩展XSSFPivotTable实现IPivotableMyTools{
私有XSSFSheet pivotTableSheet;//在pivot中显示信息的工作表
私人区域参考源数据区域;
私有XSSF数据透视表;
私有int numberOfDataFields;
私有字符串数据透视表名称;
公共数据透视表MyTools(区域引用源数据区域,字符串数据透视表名称){
this.sourceDataArea=sourceDataArea;
numberOfDataFields=0;
this.pivotTableName=数据透视表名称;
}
@凌驾
公共void createPivotTable(XSSFSheet destinationSheet,CellReference数据透视表CellPosition){
数据透视表=目的表;
数据透视表=数据透视表。createPivotTable(sourceDataArea,pivotTableCellPosition);
数据透视表.getCTPivotTableDefinition().setName(数据透视表名称);
}
//int fieldID是要添加到的字段列表中的字段的ID
//报告(源数据区域的列标题)
@凌驾
public void addReportFilterField(int fieldID){
int lastColIndex=getSourceAreaLastColumnIndex();
//使用列规范创建新的透视字段
试一试{
//将索引抛出边界
checkColumnIndexOutOfBounds(字段ID、lastColIndex);
//将数据透视字段添加到数据透视表,lastColindex还指示
//列数
addNewPivotField(字段ID、lastColIndex、Statxis.AXIS_页);
//应添加colField的列标签。
addNewCTPageField(字段ID);
}catch(IndexOutOfBoundsException e){
Activator.logInfo(“列索引超出范围”);
Activator.logError(e.getMessage());
}
}
私有void addNewCTPageField(int columnIndex){
CTPageFields页面字段;
if(数据透视表.getCTPivotTableDefinition().getPageFields()!=null){
pageFields=数据透视表.getCTPivotTableDefinition().getPageFields();
}否则{
pageFields=数据透视表。getCTPivotTableDefinition().addNewPageFields();
}
//设置fld和hier属性
CTPageField pageField=pageFields.addNewPageField();
pageField.setFld(columnIndex);
pageField.setHier(-1);
//设置count属性
pageFields.setCount(pageFields.SizeOffPageFieldArray());
}
@凌驾
public void addRowLabelsField(int-columnIndex){
数据透视表.addRowLabel(columnIndex);
}
@凌驾
public void addColumnLabelField(int-columnIndex){
int lastColIndex=getSourceAreaLastColumnIndex();