翻译|使用教程|编辑:李显亮|2021-06-15 10:21:56.220|阅读 301 次
概述:有时可能会发现自己处于需要以编程方式创建和操作数据透视表的场景中。为此,本文将教您如何使用 C++ 处理 Excel 文件中的数据透视表。
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
数据透视表重新排列数据以有意义的方式表示它。它们提供不同的排序选项,并通过将数据分组在一起来提供总和、平均值或其他统计数据。它是数据分析必不可少的工具,也是 MS Excel 的基本组成部分。有时可能会发现自己处于需要以编程方式创建和操作数据透视表的场景中。为此,本文将教您如何使用 C++ 处理 Excel 文件中的数据透视表。
Aspose.Cells for C++是一个本地 C++ 库,允许您创建、读取和更新 Excel 文件,而无需安装 Microsoft Excel。API 还支持使用 Excel 文件中的数据透视表。
在下面的示例中,我们将创建一个新的 Excel 文件,将示例数据插入其中并创建一个数据透视表。本示例中生成的文件将用作其他示例的源文件。以下是在 Excel 文件中创建数据透视表的步骤。
以下示例代码显示了如何使用 C++ 在 Excel 文件中创建数据透视表。
// Source directory path.
StringPtr srcDir = new String("SourceDirectory\\");
// Output directory path.
StringPtr outDir = new String("OutputDirectory\\");
// Create an instance of the IWorkbook class
intrusive_ptrworkbook = Factory::CreateIWorkbook();
// Access the first worksheet
intrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);
// Add source data for pivot table
intrusive_ptrstr = new String("Fruit");
worksheet->GetICells()->GetObjectByIndex(new String("A1"))->PutValue(str);
str = new String("Quantity");
worksheet->GetICells()->GetObjectByIndex(new String("B1"))->PutValue(str);
str = new String("Price");
worksheet->GetICells()->GetObjectByIndex(new String("C1"))->PutValue(str);
str = new String("Apple");
worksheet->GetICells()->GetObjectByIndex(new String("A2"))->PutValue(str);
str = new String("Orange");
worksheet->GetICells()->GetObjectByIndex(new String("A3"))->PutValue(str);
str = new String("Mango");
worksheet->GetICells()->GetObjectByIndex(new String("A4"))->PutValue(str);
worksheet->GetICells()->GetObjectByIndex(new String("B2"))->PutValue(3);
worksheet->GetICells()->GetObjectByIndex(new String("B3"))->PutValue(4);
worksheet->GetICells()->GetObjectByIndex(new String("B4"))->PutValue(4);
worksheet->GetICells()->GetObjectByIndex(new String("C2"))->PutValue(2);
worksheet->GetICells()->GetObjectByIndex(new String("C3"))->PutValue(1);
worksheet->GetICells()->GetObjectByIndex(new String("C4"))->PutValue(4);
// Add pivot table
int idx = worksheet->GetIPivotTables()->Add(new String("A1:C4"), new String("E5"), new String("MyPivotTable"));
// Access created pivot table
intrusive_ptrpivotTable = worksheet->GetIPivotTables()->GetObjectByIndex(idx);
// Manipulate pivot table rows, columns and data fields
pivotTable->AddFieldToArea(PivotFieldType_Row, pivotTable->GetIBaseFields()->GetObjectByIndex(0));
pivotTable->AddFieldToArea(PivotFieldType_Data, pivotTable->GetIBaseFields()->GetObjectByIndex(1));
pivotTable->AddFieldToArea(PivotFieldType_Data, pivotTable->GetIBaseFields()->GetObjectByIndex(2));
pivotTable->AddFieldToArea(PivotFieldType_Column, pivotTable->GetIDataField());
// Set the pivot table style
pivotTable->SetPivotTableStyleType(PivotTableStyleType_PivotTableStyleMedium9);
// Save the output excel file
workbook->Save(outDir->StringAppend(new String("outputCreatePivotTable.xlsx")));
图示:示例代码创建的数据透视表的图像
在下面的示例中,我们将按降序对数据透视表的第一列进行排序。以下是对数据透视表中的数据进行排序的步骤。
以下示例代码演示了如何使用 C++ 对 Excel 文件中的数据透视表进行排序。
// Source directory path.
StringPtr srcDir = new String("SourceDirectory\\");
// Output directory path.
StringPtr outDir = new String("OutputDirectory\\");
// Path of the input excel file
StringPtr samplePivotTable = srcDir->StringAppend(new String("SamplePivotTable.xlsx"));
// Path of the output excel file
StringPtr outputSortedPivotTable = outDir->StringAppend(new String("outputSortedPivotTable.xlsx"));
// Load the sample excel file
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(samplePivotTable);
// Access the first worksheet
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);
// Access the pivot table
intrusive_ptr<IPivotTable> pivotTable = worksheet->GetIPivotTables()->GetObjectByIndex(0);
// Set pivot table sorting
pivotTable->AddFieldToArea(PivotFieldType_Row, 0);
intrusive_ptr<IPivotField> pivotField = pivotTable->GetIRowFields()->GetObjectByIndex(0);
pivotField->SetAutoSort(true);
pivotField->SetAscendSort(false);
// Refresh and calculate the data in the pivot table.
pivotTable->RefreshData();
pivotTable->CalculateData();
// Save the output excel file
workbook->Save(outputSortedPivotTable);
图示:示例代码生成的排序后的数据透视表的图像
使用 Aspose.Cells for C++ API,您还可以隐藏数据透视表中的行。在以下示例中,我们将隐藏带有“Orange”行标签的行。以下是在数据透视表中隐藏行的步骤。
以下示例代码显示了如何使用 C++ 隐藏数据透视表中的行。
// Source directory path.
StringPtr srcDir = new String("SourceDirectory\\");
// Output directory path.
StringPtr outDir = new String("OutputDirectory\\");
// Path of the input excel file
StringPtr samplePivotTable = srcDir->StringAppend(new String("SamplePivotTable.xlsx"));
// Path of the output excel file
StringPtr outputHiddenRowPivotTable = outDir->StringAppend(new String("outputHiddenRowPivotTable.xlsx"));
// Load the sample excel file
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(samplePivotTable);
// Access the first worksheet
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);
// Access the pivot table
intrusive_ptr<IPivotTable> pivotTable = worksheet->GetIPivotTables()->GetObjectByIndex(0);
// Get pivot table body range
intrusive_ptr<ICellArea> dataBodyRange = pivotTable->GetIDataBodyRange();
// Pivot table starting row
int currentRow = 5;
// Pivot table ending row
int rowsUsed = dataBodyRange->GetendRow();
// Iterate through the rows, compare the cell value and hide the rows.
for (int i = currentRow; i < rowsUsed; i++) {
intrusive_ptr<ICell> cell = worksheet->GetICells()->GetICell(i, 4);
if (strcmp(cell->GetStringValue()->charValue(), "Orange") == 0) {
worksheet->GetICells()->HideRow(i);
}
}
// Refresh and calculate the data in the pivot table.
pivotTable->RefreshData();
pivotTable->CalculateData();
// Save the output excel file
workbook->Save(outputHiddenRowPivotTable);
图示:带有隐藏行的数据透视表的图像
还可以使用 Aspose.Cells for C++ API 操作现有数据透视表的数据。在以下示例中,我们将单元格“A2”中的文本“Apple”替换为“Orange”,并反映数据透视表中的更改。以下是操作数据透视表数据的步骤。
以下示例代码显示了如何使用 C++ 更新数据透视表的数据。
// Source directory path.
StringPtr srcDir = new String("SourceDirectory\\");
// Output directory path.
StringPtr outDir = new String("OutputDirectory\\");
// Path of the input excel file
StringPtr samplePivotTable = srcDir->StringAppend(new String("SamplePivotTable.xlsx"));
// Path of the output excel file
StringPtr outputManipulatePivotTable = outDir->StringAppend(new String("outputManipulatePivotTable.xlsx"));
// Load the sample excel file
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(samplePivotTable);
// Access the first worksheet
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);
// Change value of cell A2 which is inside the source data of pivot table
intrusive_ptr<String> str = new String("Orange");
worksheet->GetICells()->GetObjectByIndex(new String("A2"))->PutValue(str);
// Access pivot table, refresh and calculate it
intrusive_ptr<IPivotTable> pivotTable = worksheet->GetIPivotTables()->GetObjectByIndex(0);
pivotTable->RefreshData();
pivotTable->CalculateData();
// Save the output excel file
workbook->Save(outputManipulatePivotTable);
图示:显示更新数据的数据透视表
如果你想试用Aspose的全部完整功能,可联系在线客服获取30天临时授权体验。
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@ke049m.cn