ピボットテーブルを作成する
Contents
[
Hide
]
紹介
ワークシート内でピボットテーブルを作成するには、PivotTableCollection.Add() メソッドを使用してください。ピボットテーブルが作成されたら、PivotTable クラスを使用して操作できます。
ピボットテーブルの作成
次のサンプルコードは、ピボットテーブルの作成および操作方法を示しています。このコードで生成された出力エクセルファイルと、Microsoft Excelでの出力エクセルファイルを示すスクリーンショットを確認してください。
サンプルコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Aspose::Cells::Startup(); | |
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Output directory path | |
U16String outPath(u"..\\Data\\Output\\"); | |
//Path of output excel file | |
U16String outputCreatePivotTable = outPath + u"outputCreatePivotTable.xlsx"; | |
//Load the sample excel file | |
Workbook wb; | |
//Access first worksheet | |
Worksheet ws = wb.GetWorksheets().Get(0); | |
//Add source data for pivot table | |
U16String str(u"Fruit"); | |
ws.GetCells().Get(u"A1").PutValue(str); | |
str = u"Quantity"; | |
ws.GetCells().Get(u"B1").PutValue(str); | |
str = u"Price"; | |
ws.GetCells().Get(u"C1").PutValue(str); | |
str = u"Apple"; | |
ws.GetCells().Get(u"A2").PutValue(str); | |
str = u"Orange"; | |
ws.GetCells().Get(u"A3").PutValue(str); | |
ws.GetCells().Get(u"B2").PutValue(3); | |
ws.GetCells().Get(u"B3").PutValue(4); | |
ws.GetCells().Get(u"C2").PutValue(2); | |
ws.GetCells().Get(u"C3").PutValue(1); | |
//Add pivot table | |
int idx = ws.GetPivotTables().Add(u"A1:C3", u"E5", u"MyPivotTable"); | |
//Access created pivot table | |
PivotTable pt = ws.GetPivotTables().Get(idx); | |
//Manipulate pivot table rows, columns and data fields | |
pt.AddFieldToArea(PivotFieldType::Row, pt.GetBaseFields().Get(0)); | |
pt.AddFieldToArea(PivotFieldType::Data, pt.GetBaseFields().Get(1)); | |
pt.AddFieldToArea(PivotFieldType::Data, pt.GetBaseFields().Get(2)); | |
pt.AddFieldToArea(PivotFieldType::Column, pt.GetDataField()); | |
//Set pivot table style | |
pt.SetPivotTableStyleType(PivotTableStyleType::PivotTableStyleMedium9); | |
//Save the output excel file | |
wb.Save(outputCreatePivotTable); | |
Aspose::Cells::Cleanup(); |