翻譯|使用教程|編輯:李顯亮|2021-06-15 10:21:56.220|閱讀 294 次
概述:有時(shí)可能會(huì)發(fā)現(xiàn)自己處于需要以編程方式創(chuàng)建和操作數(shù)據(jù)透視表的場(chǎng)景中。為此,本文將教您如何使用 C++ 處理 Excel 文件中的數(shù)據(jù)透視表。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
數(shù)據(jù)透視表重新排列數(shù)據(jù)以有意義的方式表示它。它們提供不同的排序選項(xiàng),并通過(guò)將數(shù)據(jù)分組在一起來(lái)提供總和、平均值或其他統(tǒng)計(jì)數(shù)據(jù)。它是數(shù)據(jù)分析必不可少的工具,也是 MS Excel 的基本組成部分。有時(shí)可能會(huì)發(fā)現(xiàn)自己處于需要以編程方式創(chuàng)建和操作數(shù)據(jù)透視表的場(chǎng)景中。為此,本文將教您如何使用 C++ 處理 Excel 文件中的數(shù)據(jù)透視表。
Aspose.Cells for C++是一個(gè)本地 C++ 庫(kù),允許您創(chuàng)建、讀取和更新 Excel 文件,而無(wú)需安裝 Microsoft Excel。API 還支持使用 Excel 文件中的數(shù)據(jù)透視表。
在下面的示例中,我們將創(chuàng)建一個(gè)新的 Excel 文件,將示例數(shù)據(jù)插入其中并創(chuàng)建一個(gè)數(shù)據(jù)透視表。本示例中生成的文件將用作其他示例的源文件。以下是在 Excel 文件中創(chuàng)建數(shù)據(jù)透視表的步驟。
以下示例代碼顯示了如何使用 C++ 在 Excel 文件中創(chuàng)建數(shù)據(jù)透視表。
// 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")));
圖示:示例代碼創(chuàng)建的數(shù)據(jù)透視表的圖像
在下面的示例中,我們將按降序?qū)?shù)據(jù)透視表的第一列進(jìn)行排序。以下是對(duì)數(shù)據(jù)透視表中的數(shù)據(jù)進(jìn)行排序的步驟。
以下示例代碼演示了如何使用 C++ 對(duì) Excel 文件中的數(shù)據(jù)透視表進(jìn)行排序。
// 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);
圖示:示例代碼生成的排序后的數(shù)據(jù)透視表的圖像
使用 Aspose.Cells for C++ API,您還可以隱藏?cái)?shù)據(jù)透視表中的行。在以下示例中,我們將隱藏帶有“Orange”行標(biāo)簽的行。以下是在數(shù)據(jù)透視表中隱藏行的步驟。
以下示例代碼顯示了如何使用 C++ 隱藏?cái)?shù)據(jù)透視表中的行。
// 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);
圖示:帶有隱藏行的數(shù)據(jù)透視表的圖像
還可以使用 Aspose.Cells for C++ API 操作現(xiàn)有數(shù)據(jù)透視表的數(shù)據(jù)。在以下示例中,我們將單元格“A2”中的文本“Apple”替換為“Orange”,并反映數(shù)據(jù)透視表中的更改。以下是操作數(shù)據(jù)透視表數(shù)據(jù)的步驟。
以下示例代碼顯示了如何使用 C++ 更新數(shù)據(jù)透視表的數(shù)據(jù)。
// 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);
圖示:顯示更新數(shù)據(jù)的數(shù)據(jù)透視表
如果你想試用Aspose的全部完整功能,可聯(lián)系在線客服獲取30天臨時(shí)授權(quán)體驗(yàn)。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn