Manipola tabella pivot
Possibili scenari di utilizzo
Oltre a creare nuove tabelle pivot, puoi manipolare le tabelle pivot nuove ed esistenti. È possibile modificare i dati nell’intervallo di origine della tabella pivot, quindi aggiornarli e calcolarli e ottenere i nuovi valori delle celle della tabella pivot. Si prega di utilizzareIPivotTable.RefreshData() eIPivotTable.CalculateData()metodi dopo aver modificato i valori nell’intervallo di origine della tabella pivot per aggiornare la tabella pivot.
Manipola tabella pivot
Il codice di esempio seguente carica il filefile excel di esempio e accede alla tabella pivot esistente all’interno del suo primo foglio di lavoro. Cambia il valore della cella B3 che si trova all’interno dell’intervallo di origine della tabella pivot e quindi aggiorna la tabella pivot. Prima di aggiornare la tabella pivot, accede al valore della cella della tabella pivot H8 che è 15 e dopo aver aggiornato la tabella pivot, il suo valore cambia in 6. Si prega di vedere ilfile excel di outputgenerato con questo codice e lo screenshot che mostra l’effetto del codice di esempio sul file excel di esempio. Si prega di vedere anche l’output della console di seguito che mostra il valore della cella della tabella pivot H8 prima e dopo l’aggiornamento della tabella pivot.
Codice d’esempio
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Source directory path | |
StringPtr dirPath = new String("..\\Data\\PivotTables\\"); | |
//Output directory path | |
StringPtr outPath = new String("..\\Data\\Output\\"); | |
//Path of input excel file | |
StringPtr sampleManipulatePivotTable = dirPath->StringAppend(new String("sampleManipulatePivotTable.xlsx")); | |
//Path of output excel file | |
StringPtr outputManipulatePivotTable = outPath->StringAppend(new String("outputManipulatePivotTable.xlsx")); | |
//Load the sample excel file | |
intrusive_ptr<IWorkbook> wb = Factory::CreateIWorkbook(sampleManipulatePivotTable); | |
//Access first worksheet | |
intrusive_ptr<IWorksheet> ws = wb->GetIWorksheets()->GetObjectByIndex(0); | |
//Change value of cell B3 which is inside the source data of pivot table | |
intrusive_ptr<String> str = new String("Cup"); | |
ws->GetICells()->GetObjectByIndex(new String("B3"))->PutValue(str); | |
//Get the value of cell H8 before refreshing pivot table | |
intrusive_ptr<String> val = ws->GetICells()->GetObjectByIndex(new String("H8"))->GetStringValue(); | |
StringPtr str1 = new String(L"Before refreshing Pivot Table value of cell H8: "); | |
Console::WriteLine(str1->StringAppend(val)); | |
//Access pivot table, refresh and calculate it | |
intrusive_ptr<IPivotTable> pt = ws->GetIPivotTables()->GetObjectByIndex(0); | |
pt->RefreshData(); | |
pt->CalculateData(); | |
//Get the value of cell H8 after refreshing pivot table | |
val = ws->GetICells()->GetObjectByIndex(new String("H8"))->GetStringValue(); | |
StringPtr str2 = new String(L"After refreshing Pivot Table value of cell H8: "); | |
Console::WriteLine(str2->StringAppend(val)); | |
//Save the output excel file | |
wb->Save(outputManipulatePivotTable); |
Uscita console
Di seguito è riportato l’output della console del codice di esempio precedente quando eseguito con il file fornitofile excel di esempio.
Before refreshing Pivot Table value of cell H8: 15
After refreshing Pivot Table value of cell H8: 6