Thanks for the document to demonstrate your requirements.
I think Aspose.Cells can suit your needs well. Aspose.Cells provide specific APIs to create, manipulate or render PivotTables. Aspose.Cells component provides all the relative APIs set (members) regarding PivotTables for the task. I think as you need to add calculated item in the pivot table report, you may simply use PivotField…addCalculatedItem() method for your needs. I have created a sample code for your reference, you may refer to it and write your own codes accordingly.
//Instantiating an Workbook object
Workbook workbook = new Workbook();
//Obtaining the reference of the newly added worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
Cells cells = sheet.getCells();
//Setting the value to the cells
Cell cell = cells.get(“A1”);
cell.putValue(“Sport”);
cell = cells.get(“B1”);
cell.putValue(“Quarter”);
cell = cells.get(“C1”);
cell.putValue(“Sales”);
cell = cells.get(“D1”);
cell.putValue(“Sales1”);
cell = cells.get(“A2”);
cell.putValue(“Golf”);
cell = cells.get(“A3”);
cell.putValue(“Golf”);
cell = cells.get(“A4”);
cell.putValue(“Tennis”);
cell = cells.get(“A5”);
cell.putValue(“Tennis”);
cell = cells.get(“A6”);
cell.putValue(“Tennis”);
cell = cells.get(“A7”);
cell.putValue(“Tennis”);
cell = cells.get(“A8”);
cell.putValue(“Golf”);
cell = cells.get(“B2”);
cell.putValue(“Qtr3”);
cell = cells.get(“B3”);
cell.putValue(“Qtr4”);
cell = cells.get(“B4”);
cell.putValue(“Qtr3”);
cell = cells.get(“B5”);
cell.putValue(“Qtr4”);
cell = cells.get(“B6”);
cell.putValue(“Qtr3”);
cell = cells.get(“B7”);
cell.putValue(“Qtr4”);
cell = cells.get(“B8”);
cell.putValue(“Qtr3”);
cell = cells.get(“C2”);
cell.putValue(1500);
cell = cells.get(“C3”);
cell.putValue(2000);
cell = cells.get(“C4”);
cell.putValue(600);
cell = cells.get(“C5”);
cell.putValue(1500);
cell = cells.get(“C6”);
cell.putValue(4070);
cell = cells.get(“C7”);
cell.putValue(5000);
cell = cells.get(“C8”);
cell.putValue(6430);
PivotTableCollection pivotTables = sheet.getPivotTables();
//Adding a PivotTable to the worksheet
int index = pivotTables.add(“=A1:C8”, “E20”, “PivotTable1”);
//Accessing the instance of the newly added PivotTable
PivotTable pivotTable = pivotTables.get(index);
pivotTable.addFieldToArea(PivotFieldType.ROW, 0);
//Dragging the second field to the column area.
pivotTable.addFieldToArea(PivotFieldType.COLUMN, 1);
pivotTable.addFieldToArea(PivotFieldType.DATA, 2);
pivotTable.getColumnFields().get(0).addCalculatedItem(“AverageQtrs”, “=Average(‘Qtr3’,‘Qtr4’)”);
workbook.save(“outpivottableaddcalcitem1.xlsx”);
We also recommend you to kindly see the documents in the section for your reference:
Hope, this helps a bit.
Thank you.