创建数据透视表
Contents
[
Hide
]
介绍
请用IPivotTableCollection.Add()在工作表中创建数据透视表的方法。创建数据透视表后,您可以使用数据透视表班级。
创建数据透视表
以下示例代码显示了如何创建数据透视表并使用它。请检查输出excel文件使用此代码生成,以下屏幕截图显示输出excel文件在 Microsoft Excel 中。
示例代码
This file contains hidden or 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
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Output directory path | |
StringPtr outPath = new String("..\\Data\\Output\\"); | |
//Path of output excel file | |
StringPtr outputCreatePivotTable = outPath->StringAppend(new String("outputCreatePivotTable.xlsx")); | |
//Load the sample excel file | |
intrusive_ptr<IWorkbook> wb = Factory::CreateIWorkbook(); | |
//Access first worksheet | |
intrusive_ptr<IWorksheet> ws = wb->GetIWorksheets()->GetObjectByIndex(0); | |
//Add source data for pivot table | |
intrusive_ptr<String> str = new String("Fruit"); | |
ws->GetICells()->GetObjectByIndex(new String("A1"))->PutValue(str); | |
str = new String("Quantity"); | |
ws->GetICells()->GetObjectByIndex(new String("B1"))->PutValue(str); | |
str = new String("Price"); | |
ws->GetICells()->GetObjectByIndex(new String("C1"))->PutValue(str); | |
str = new String("Apple"); | |
ws->GetICells()->GetObjectByIndex(new String("A2"))->PutValue(str); | |
str = new String("Orange"); | |
ws->GetICells()->GetObjectByIndex(new String("A3"))->PutValue(str); | |
ws->GetICells()->GetObjectByIndex(new String("B2"))->PutValue(3); | |
ws->GetICells()->GetObjectByIndex(new String("B3"))->PutValue(4); | |
ws->GetICells()->GetObjectByIndex(new String("C2"))->PutValue(2); | |
ws->GetICells()->GetObjectByIndex(new String("C3"))->PutValue(1); | |
//Add pivot table | |
int idx = ws->GetIPivotTables()->Add(new String("A1:C3"), new String("E5"), new String("MyPivotTable")); | |
//Access created pivot table | |
intrusive_ptr<IPivotTable> pt = ws->GetIPivotTables()->GetObjectByIndex(idx); | |
//Manipulate pivot table rows, columns and data fields | |
pt->AddFieldToArea(PivotFieldType_Row, pt->GetIBaseFields()->GetObjectByIndex(0)); | |
pt->AddFieldToArea(PivotFieldType_Data, pt->GetIBaseFields()->GetObjectByIndex(1)); | |
pt->AddFieldToArea(PivotFieldType_Data, pt->GetIBaseFields()->GetObjectByIndex(2)); | |
pt->AddFieldToArea(PivotFieldType_Column, pt->GetIDataField()); | |
//Set pivot table style | |
pt->SetPivotTableStyleType(PivotTableStyleType_PivotTableStyleMedium9); | |
//Save the output excel file | |
wb->Save(outputCreatePivotTable); |