ピボット テーブルの操作

考えられる使用シナリオ

新しいピボット テーブルを作成するだけでなく、新しいピボット テーブルと既存のピボット テーブルを操作できます。ピボット テーブルのソース範囲のデータを変更し、それを更新して計算し、ピボット テーブル セルの新しい値を取得できます。使ってくださいIPivotTable.RefreshData()IPivotTable.CalculateData()ピボット テーブルのソース範囲の値を変更してピボット テーブルを更新した後のメソッド。

ピボット テーブルの操作

次のサンプル コードは、サンプルエクセルファイル最初のワークシート内の既存のピボット テーブルにアクセスします。ピボット テーブルのソース範囲内にあるセル B3 の値を変更し、ピボット テーブルを更新します。ピボット テーブルを更新する前に、ピボット テーブル セル H8 の値 15 にアクセスし、ピボット テーブルを更新した後、その値は 6 に変わります。出力エクセルファイルこのコードと、サンプル Excel ファイルに対するサンプル コードの効果を示すスクリーンショットで生成されます。ピボット テーブルを更新する前後のピボット テーブル セル H8 の値を示す以下のコンソール出力も参照してください。

todo:画像_代替_文章

サンプルコード

//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);

コンソール出力

以下は、上記のサンプル コードを提供されたコマンドで実行したときのコンソール出力です。サンプルエクセルファイル.

 Before refreshing Pivot Table value of cell H8: 15

After refreshing Pivot Table value of cell H8: 6