Excelファイルをロードする際にPivotキャッシュレコードを解析する

可能な使用シナリオ

Pivot Tableを作成する際に、Microsoft Excelは元のデータのコピーを取り、それをPivot Cacheに保存します。Pivot CacheはMicrosoft Excelのメモリ内に保持されます。それを見ることはできませんが、それがPivot Tableが構築されたりSlicerの選択が変更されたり行または列が移動されたりするときに参照するデータです。これにより、Microsoft ExcelはPivot Tableの変更に非常に敏感になりますが、ファイルのサイズが2倍になる可能性もあります。つまり、Pivot Cacheはソースデータの単なるコピーなので、ファイルサイズが潜在的に2倍になるのは理にかなっています。

Workbookオブジェクト内にExcelファイルをロードするときに、Pivot Cacheのレコードも一緒に読み込むかどうかをLoadOptions.ParsingPivotCachedRecordsプロパティを使用して決定できます。このプロパティのデフォルト値はfalseです。Pivot Cacheがかなり大きい場合、パフォーマンスが向上することがあります。しかし、Pivot Cacheのレコードも読み込む場合は、このプロパティをtrueに設定する必要があります。

Excelファイルをロードする際にPivotキャッシュレコードを解析する

以下のサンプルコードは、LoadOptions.ParsingPivotCachedRecordsプロパティの使用方法を説明します。それはPivot Cacheのレコードを解析しながらサンプルExcelファイルをロードし、ピボットテーブルをリフレッシュして出力Excelファイルとして保存しています。

サンプルコード

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
//Create load options
LoadOptions options = new LoadOptions();
//Set ParsingPivotCachedRecords true, default value is false
options.ParsingPivotCachedRecords = true;
//Load the sample Excel file containing pivot table cached records
Workbook wb = new Workbook("sampleParsingPivotCachedRecordsWhileLoadingExcelFile.xlsx", options);
//Access first worksheet
Worksheet ws = wb.Worksheets[0];
//Access first pivot table
PivotTable pt = ws.PivotTables[0];
//Set refresh data flag true
pt.RefreshDataFlag = true;
//Refresh and calculate pivot table
pt.RefreshData();
pt.CalculateData();
//Set refresh data flag false
pt.RefreshDataFlag = false;
//Save the output Excel file
wb.Save("outputParsingPivotCachedRecordsWhileLoadingExcelFile.xlsx");