翻譯|使用教程|編輯:李顯亮|2021-05-20 10:44:00.723|閱讀 254 次
概述:在某些情況下,篩選數(shù)據(jù)可能會有所幫助。在本文中,將學(xué)習(xí)如何使用C ++在Excel文件中添加或刪除自動篩選。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
在Excel電子表格中過濾信息是一項重要功能。它使您可以隱藏不相關(guān)的數(shù)據(jù),并僅顯示符合特定條件的數(shù)據(jù)。在某些情況下,篩選數(shù)據(jù)可能會有所幫助。例如,組織可能希望從銷售報告中過濾出性能不佳的產(chǎn)品,以分析和改進(jìn)其銷售策略。在本文中,將學(xué)習(xí)如何使用C ++在Excel文件中添加或刪除自動篩選。
Aspose.Cells for C++是本機(jī)C ++庫,可讓您創(chuàng)建,讀取和修改Excel文件,而無需安裝Microsoft Excel。
可以在一系列單元格上應(yīng)用自動篩選。自動過濾器使您可以排序和過濾給定單元格范圍內(nèi)的值。以下是在Excel文件中添加自動篩選的步驟。
下面的示例代碼顯示了如何使用C ++將自動篩選應(yīng)用于Excel文件中的一系列單元格。
// Source directory path. StringPtr srcDir = new String("SourceDirectory\\"); // Output directory path. StringPtr outDir = new String("OutputDirectory\\"); // Load the source Excel file intrusive_ptrworkbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("Book3.xlsx"))); // Access the first worksheet in the Excel file intrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); // Creating AutoFilter by giving the cell range worksheet->GetIAutoFilter()->SetRange(new String("A1:B1")); // Save the Excel file workbook->Save(outDir->StringAppend(new String("AutoFilter_out.xlsx")));
Excel文件可能包含基于日期的數(shù)據(jù)。可能會遇到需要根據(jù)不同日期過濾和分析數(shù)據(jù)的情況。因此,在這種情況下,日期過濾器將被證明是有用的。以下是在Excel文件中添加日期自動篩選的步驟。
下面的示例代碼演示了如何使用C ++在Excel文件中添加日期自動篩選。
// Source directory path. StringPtr srcDir = new String("SourceDirectory\\"); // Output directory path. StringPtr outDir = new String("OutputDirectory\\"); // Load the source Excel file intrusive_ptrworkbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("Book4.xlsx"))); // Access the first worksheet in the Excel file intrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); // Call AddDateFilter function to apply the filter worksheet->GetIAutoFilter()->AddDateFilter(0, DateTimeGroupingType_Month, 2018, 1, 1, 0, 0, 0); // Call the Refresh function to update the worksheet worksheet->GetIAutoFilter()->Refresh(); // Save the Excel file workbook->Save(outDir->StringAppend(new String("DateFilter_out.xlsx")));
在某些情況下,可能需要更通用的日期過濾器,例如月份,而不管年份或當(dāng)前月份之前的月份。對于這種情況,可以使用動態(tài)自動篩選來篩選數(shù)據(jù)。以下是使用動態(tài)日期自動篩選器篩選數(shù)據(jù)的步驟。
下面的示例代碼演示了如何使用C++在Excel文件中添加一個動態(tài)日期自動過濾器。
// Source directory path. StringPtr srcDir = new String("SourceDirectory\\"); // Output directory path. StringPtr outDir = new String("OutputDirectory\\"); // Load the source Excel file intrusive_ptrworkbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("Book4.xlsx"))); // Access the first worksheet in the Excel file intrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); // Call DynamicFilter function to apply the filter worksheet->GetIAutoFilter()->DynamicFilter(0, DynamicFilterType_Februray); // Call the Refresh function to update the worksheet worksheet->GetIAutoFilter()->Refresh(); // Save the Excel file workbook->Save(outDir->StringAppend(new String("DynamicDateAutoFilter_out.xlsx")));
如果您想應(yīng)用一個自定義的自動過濾器,Aspose.Cells for C++ API將為您提供幫助。使用該API,您可以根據(jù)您的具體要求在Excel文件中應(yīng)用一個自定義的自動過濾器。以下是在Excel文件中添加自定義自動過濾器的步驟。
下面的示例代碼顯示了如何使用C++在Excel文件中添加自定義自動過濾器。
// Source directory path. StringPtr srcDir = new String("SourceDirectory\\"); // Output directory path. StringPtr outDir = new String("OutputDirectory\\"); // Load the source Excel file intrusive_ptrworkbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("sampleCountryNames.xlsx"))); // Access the first worksheet in the Excel file intrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); // Call Custom function to apply the filter worksheet->GetIAutoFilter()->Custom(0, FilterOperatorType_Equal, new String("Brazil")); // Call the Refresh function to update the worksheet worksheet->GetIAutoFilter()->Refresh(); // Save the Excel file workbook->Save(outDir->StringAppend(new String("CustomFilter_out.xlsx")));
除了添加自動過濾器之外,還可以使用Aspose.Cells for C++ API來刪除它們。下面是刪除Excel文件中自動過濾器的步驟。
下面的示例代碼顯示了如何使用C++從Excel文件中移除AutoFilter。
// Source directory path. StringPtr srcDir = new String("SourceDirectory\\"); // Output directory path. StringPtr outDir = new String("OutputDirectory\\"); // Load the source Excel file intrusive_ptrworkbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("SampleAutoFilter.xlsx"))); // Access the first worksheet in the Excel file intrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); // Remove AutoFilters worksheet->RemoveAutoFilter(); // Save the Excel file workbook->Save(outDir->StringAppend(new String("RemoveAutoFilter_out.xlsx")));
如果你想試用Aspose的全部完整功能,可聯(lián)系在線客服獲取30天臨時授權(quán)體驗。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn