Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
There are times when you want to create Microsoft Excel reports with pivot tables that take data from different data sources (such as a database) that are not known at design time. This article provides an approach to dynamically change a pivot table’s data source.
Creating a new designer template.
Creating a designer template & defining a named range, DataSource

Creating a Pivot Table Based on this named range.
Creating a pivot table based on the named range, DataSource

Creating a pivot table based on a corresponding field

Right‑click the pivot table and select Table Options.
Setting the pivot table options

Now, you can save this file as your designer template file.
Executing the example code below changes the source data of the pivot table.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"Book1.xlsx";
// Create workbook
Workbook workbook(inputFilePath);
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Populate new data into the worksheet cells
worksheet.GetCells().Get(u"A9").PutValue(u"Golf");
worksheet.GetCells().Get(u"B9").PutValue(u"Qtr4");
worksheet.GetCells().Get(u"C9").PutValue(7000);
// Change named range "DataSource"
Range range = worksheet.GetCells().CreateRange(0, 0, 9, 3);
range.SetName(u"DataSource");
// Save the modified Excel file
U16String outputFilePath = srcDir + u"output.xls";
workbook.Save(outputFilePath);
Aspose::Cells::Cleanup();
std::cout << "File saved successfully!" << std::endl;
return 0;
}
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.