Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
When you create a pivot table based on known data, you find that the data in it is not what you want. The data you want is the combination of these original data. For example, you need to add, subtract, multiply and divide the original data before you want the data. At this time, you need to build a calculated field and set the corresponding formula for calculation. Then perform some statistics and other operations on the calculated field.
Insert a calculated field in a PivotTable in Excel, follow these steps:


Add calculated field to Excel file using Aspose.Cells. Please see the following sample code. After executing the example code, a pivot table with calculated field is added to the worksheet.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Creating a Workbook object
Workbook workbook;
// Obtaining the reference of the newly added worksheet
Worksheet ws = workbook.GetWorksheets().Get(0);
Cells cells = ws.GetCells();
// Setting the values to the cells
Cell cell = cells.Get("A1");
cell.PutValue(u"Fruit");
cell = cells.Get("B1");
cell.PutValue(u"Count");
cell = cells.Get("C1");
cell.PutValue(u"Price");
cell = cells.Get("A2");
cell.PutValue(u"Apple");
cell = cells.Get("A3");
cell.PutValue(u"Mango");
cell = cells.Get("A4");
cell.PutValue(u"Blackberry");
cell = cells.Get("A5");
cell.PutValue(u"Cherry");
cell = cells.Get("B2");
cell.PutValue(5);
cell = cells.Get("B3");
cell.PutValue(3);
cell = cells.Get("B4");
cell.PutValue(6);
cell = cells.Get("B5");
cell.PutValue(4);
cell = cells.Get("C2");
cell.PutValue(5);
cell = cells.Get("C3");
cell.PutValue(20);
cell = cells.Get("C4");
cell.PutValue(30);
cell = cells.Get("C5");
cell.PutValue(60);
// Adding a PivotTable to the worksheet
int32_t i = ws.GetPivotTables().Add(u"=A1:C5", u"D10", u"PivotTable1");
// Accessing the instance of the newly added PivotTable
PivotTable pivotTable = ws.GetPivotTables().Get(i);
pivotTable.AddFieldToArea(PivotFieldType::Row, 0);
// Adding a calculated field to PivotTable and dragging it to data area
pivotTable.AddCalculatedField(u"total", u"=Count*Price", true);
pivotTable.RefreshData();
pivotTable.CalculateData();
workbook.Save(u"out.xlsx");
Aspose::Cells::Cleanup();
}
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.