无需MS Office创建Excel!使用C ++以编程方式在 Excel 文件中处理数据透视表

有时可能会发现自己处于需要以编程方式创建和操作数据透视表的场景中。为此,本文将教您如何使用 C++ 处理 Excel 文件中的数据透视表。

数据透视表重新排列数据以有意义的方式表示它。它们提供不同的排序选项,并通过将数据分组在一起来提供总和、平均值或其他统计数据。它是数据分析必不可少的工具,也是 MS Excel 的基本组成部分。有时可能会发现自己处于需要以编程方式创建和操作数据透视表的场景中。为此,本文将教您如何使用 C++ 处理 Excel 文件中的数据透视表。

  • 使用 C++ 在 Excel 文件中创建数据透视表
  • 使用 C++ 对 Excel 文件中的数据透视表进行排序
  • 使用 C++ 隐藏数据透视表中的行
  • 使用 C++ 操作数据透视表数据


使用 C++ 在 Excel 文件中创建数据透视表

在下面的示例中,我们将创建一个新的 Excel 文件,将示例数据插入其中并创建一个数据透视表。本示例中生成的文件将用作其他示例的源文件。以下是在 Excel 文件中创建数据透视表的步骤。

  • 首先,创建一个IWorkbook 类的实例来表示新的 Excel 文件。
  • 使用IWorkbook->GetIWorksheets()->GetObjectByIndex(Aspose::Cells::Systems::Int32 index) 方法访问要插入数据透视表的工作表 。
  • 为数据透视表添加示例数据。
  • 使用 IWorksheet->GetIPivotTables()->Add(intrusive_ptrsourceData, intrusive_ptrdestCellName, intrusive_ptrtableName) 的方法
  • 要访问数据透视表,请使用IWorksheet->GetIPivotTables()->GetObjectByIndex(Aspose::Cells::Systems::Int32 index)方法。
  • 操作字段并设置数据透视表的样式。
  • 最后,使用IWorkbook->Save (intrusive_ptrfileName) 方法保存 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 classintrusive_ptrworkbook = Factory::CreateIWorkbook();// Access the first worksheetintrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);// Add source data for pivot tableintrusive_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 tableint idx = worksheet->GetIPivotTables()->Add(new String("A1:C4"), new String("E5"), new String("MyPivotTable"));// Access created pivot tableintrusive_ptrpivotTable = worksheet->GetIPivotTables()->GetObjectByIndex(idx);// Manipulate pivot table rows, columns and data fieldspivotTable->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 stylepivotTable->SetPivotTableStyleType(PivotTableStyleType_PivotTableStyleMedium9);// Save the output excel fileworkbook->Save(outDir->StringAppend(new String("outputCreatePivotTable.xlsx")));

无需MS Office创建Excel!使用C ++以编程方式在 Excel 文件中处理数据透视表

图示:示例代码创建的数据透视表的图像

使用 C++ 对 Excel 文件中的数据透视表进行排序

在下面的示例中,我们将按降序对数据透视表的第一列进行排序。以下是对数据透视表中的数据进行排序的步骤。

  • 首先,使用IWorkbook 类加载示例 Excel 文件。
  • 使用IWorkbook->GetIWorksheets()->GetObjectByIndex(Aspose::Cells::Systems::Int32 index) 方法检索包含数据透视表的工作表 。
  • 使用IWorksheet->GetIPivotTables()->GetObjectByIndex(Aspose::Cells::Systems::Int32 index)方法访问数据透视表。
  • 使用IPivotField->SetAutoSort(bool value)和IPivotField->SetAscendSort(bool value)方法获取行字段并对数据透视表进行排序。
  • 刷新数据透视表的内容并分别使用IPivotTable->RefreshData()和IPivotTable->CalculateData()方法计算数据。
  • 最后,使用IWorkbook->Save (intrusive_ptrfileName) 方法保存 Excel 文件 。

以下示例代码演示了如何使用 C++ 对 Excel 文件中的数据透视表进行排序。

// Source directory path.StringPtr srcDir = new String("SourceDirectory\");// Output directory path.StringPtr outDir = new String("OutputDirectory\");// Path of the input excel fileStringPtr samplePivotTable = srcDir->StringAppend(new String("SamplePivotTable.xlsx"));// Path of the output excel fileStringPtr outputSortedPivotTable = outDir->StringAppend(new String("outputSortedPivotTable.xlsx"));// Load the sample excel fileintrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(samplePivotTable);// Access the first worksheetintrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);// Access the pivot tableintrusive_ptr<IPivotTable> pivotTable = worksheet->GetIPivotTables()->GetObjectByIndex(0);// Set pivot table sortingpivotTable->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 fileworkbook->Save(outputSortedPivotTable);

无需MS Office创建Excel!使用C ++以编程方式在 Excel 文件中处理数据透视表

图示:示例代码生成的排序后的数据透视表的图像

使用 C++ 隐藏数据透视表中的行

使用 Aspose.Cells for C++ API,您还可以隐藏数据透视表中的行。在以下示例中,我们将隐藏带有“Orange”行标签的行。以下是在数据透视表中隐藏行的步骤。

  • 首先,使用IWorkbook 类加载示例 Excel 文件。
  • 使用IWorkbook->GetIWorksheets()->GetObjectByIndex(Aspose::Cells::Systems::Int32 index) 方法检索包含数据透视表的工作表 。
  • 使用IWorksheet->GetIPivotTables()->GetObjectByIndex(Aspose::Cells::Systems::Int32 index)方法访问数据透视表。
  • 使用IPivotTable->GetIDataBodyRange()方法获取数据透视表数据主体范围。
  • 遍历数据透视表的行并隐藏满足条件的行。
  • 刷新数据透视表的内容并分别使用IPivotTable->RefreshData()和IPivotTable->CalculateData()方法计算数据。
  • 最后,使用IWorkbook->Save (intrusive_ptrfileName) 方法保存 Excel 文件 。

以下示例代码显示了如何使用 C++ 隐藏数据透视表中的行。

// Source directory path.StringPtr srcDir = new String("SourceDirectory\");// Output directory path.StringPtr outDir = new String("OutputDirectory\");// Path of the input excel fileStringPtr samplePivotTable = srcDir->StringAppend(new String("SamplePivotTable.xlsx"));// Path of the output excel fileStringPtr outputHiddenRowPivotTable = outDir->StringAppend(new String("outputHiddenRowPivotTable.xlsx"));// Load the sample excel fileintrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(samplePivotTable);// Access the first worksheetintrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);// Access the pivot tableintrusive_ptr<IPivotTable> pivotTable = worksheet->GetIPivotTables()->GetObjectByIndex(0);// Get pivot table body rangeintrusive_ptr<ICellArea> dataBodyRange = pivotTable->GetIDataBodyRange();// Pivot table starting rowint currentRow = 5;// Pivot table ending rowint 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 fileworkbook->Save(outputHiddenRowPivotTable);

无需MS Office创建Excel!使用C ++以编程方式在 Excel 文件中处理数据透视表

图示:带有隐藏行的数据透视表的图像

使用 C++ 操作数据透视表数据

还可以使用 Aspose.Cells for C++ API 操作现有数据透视表的数据。在以下示例中,我们将单元格“A2”中的文本“Apple”替换为“Orange”,并反映数据透视表中的更改。以下是操作数据透视表数据的步骤。

  • 首先,使用IWorkbook 类加载示例 Excel 文件。
  • 使用IWorkbook->GetIWorksheets()->GetObjectByIndex(Aspose::Cells::Systems::Int32 index) 方法检索包含数据透视表数据的工作表 。
  • 根据您的要求更新数据透视表的数据。
  • 为了访问数据透视表,使用IWorksheet->GetIPivotTables()->GetObjectByIndex(Aspose::Cells::Systems::Int32 index)方法。
  • 刷新数据透视表的内容并分别使用IPivotTable->RefreshData()和IPivotTable->CalculateData()方法计算数据。
  • 最后,使用IWorkbook->Save (intrusive_ptrfileName) 方法保存 Excel 文件 。

以下示例代码显示了如何使用 C++ 更新数据透视表的数据。

// Source directory path.StringPtr srcDir = new String("SourceDirectory\");// Output directory path.StringPtr outDir = new String("OutputDirectory\");// Path of the input excel fileStringPtr samplePivotTable = srcDir->StringAppend(new String("SamplePivotTable.xlsx"));// Path of the output excel fileStringPtr outputManipulatePivotTable = outDir->StringAppend(new String("outputManipulatePivotTable.xlsx"));// Load the sample excel fileintrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(samplePivotTable);// Access the first worksheetintrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);// Change value of cell A2 which is inside the source data of pivot tableintrusive_ptr<String> str = new String("Orange");worksheet->GetICells()->GetObjectByIndex(new String("A2"))->PutValue(str);// Access pivot table, refresh and calculate itintrusive_ptr<IPivotTable> pivotTable = worksheet->GetIPivotTables()->GetObjectByIndex(0);pivotTable->RefreshData();pivotTable->CalculateData();// Save the output excel fileworkbook->Save(outputManipulatePivotTable);

无需MS Office创建Excel!使用C ++以编程方式在 Excel 文件中处理数据透视表

图示:显示更新数据的数据透视表

如果你想试用Aspose的全部完整功能,可联系在线客服获取30天临时授权体验。


还想要更多吗可以点击阅读【Aspose最新资源在线文库】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询
标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年5月12日
下一篇 2021年5月12日

相关推荐

发表回复

登录后才能评论