I am trying to produce an excel pivot table using apache-poi library.
When i try to add multiple aggregations, excel was grouping it into values(you can see the values attribute in right side of 2nd image in row labels) and placing values in columns. I want to generate an excel with values in the row labels by default.
But when i add multiple aggregations like sum and average(as shown in the below image), the values are shown in column labels. values can be dragged to row labels in excel but i need the values in rows by default.
But I am able to generate this in java
Code for these aggregations
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
//Create some data to build the pivot table on
setCellData(sheet);
XSSFPivotTable pivotTable = null;
try {
pivotTable = sheet.createPivotTable(new AreaReference("A1:I8", SpreadsheetVersion.EXCEL2007), new CellReference("M10"));
} catch (Exception ex) {
System.out.println("In sheet: " + ex);
}
//Configure the pivot table
//Use first column as row label
pivotTable.addRowLabel(0);
pivotTable.addRowLabel(1);
pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(2).setAxis(STAxis.AXIS_COL);
pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(2).addNewItems();
pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(2).getItems().addNewItem()
.setT(STItemType.DEFAULT);
pivotTable.getCTPivotTableDefinition().addNewColFields().addNewField().setX(1);
//this.addCalculatedColumnToPivotTable(pivotTable, "field1", "average of count ab", "'count'");
//Sum up the second column
pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 3);
//Set the third column as filter
pivotTable.addColumnLabel(DataConsolidateFunction.AVERAGE, 3);
But there is no function in pivot table to add these aggregations in row labels in apache-poi library.
pivotTable.addRowLabel(3);
as you want to have the values as row and not as column – XtremeBaumer