計算項目を含むピボット テーブルの更新と計算

計算項目を含むピボット テーブルの更新と計算

次のサンプル コードは、ソースエクセルファイル「add」、「div」、「div2」などの 3 つの計算項目を持つピボット テーブルが含まれています。最初にセル D2 の値を 20 に変更し、Aspose.Cells API を使用してピボット テーブルを更新して計算し、ワークブックを PDF 形式で保存します。での結果出力 PDFAspose.Cells が、計算項目を含むピボット テーブルを更新して計算したことを示しています。 Microsoft Excel を使用してセル D2 に値 20 を手動で入力し、Alt+F5 ショートカット キーを使用してピボット テーブルを更新するか、ピボット テーブルの [更新] ボタンをクリックして確認できます。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Load source excel file containing a pivot table having calculated items
Workbook wb = new Workbook(dataDir + "sample.xlsx");
// Access first worksheet
Worksheet sheet = wb.Worksheets[0];
// Change the value of cell D2
sheet.Cells["D2"].PutValue(20);
// Refresh and calculate all the pivot tables inside this sheet
foreach (PivotTable pt in sheet.PivotTables)
{
pt.RefreshData();
pt.CalculateData();
}
// Save the workbook in output pdf
wb.Save(dataDir + "RefreshAndCalculateItems_out.pdf", SaveFormat.Pdf);