In deze zelfstudie leren we hoe u filter in XLSX-bestand kunt toevoegen met C++. U kunt een filter in het Excel-werkblad invoegen op Microsoft Windows, Linux, enz. Programmatisch in C ++.
Stappen om filter toe te voegen in XLSX-bestand met C++
- Installeer het Aspose.Cells.Cpp-pakket met de NuGet-pakketbeheerder-plug-in
- Voeg de verwijzing toe aan Aspose::Cells naamruimte
- Initialiseer het Workbook Class-object om een nieuwe Excel-werkmap te instantiëren
- Steek voorbeeldwaarden in de cellen in
- Formule en bereik instellen voor het filteren van gegevens
- Voeg een filter toe aan een kolom met de AddFilter-methode
- Sla het Excel-uitvoerbestand op na het filteren van gegevens met C++
In het volgende voorbeeld zult u onderzoeken hoe u filter in Excel-bestand kunt maken met behulp van C++. Initialiseer een leeg werkblad en * pas filter toe in Excel-bestand met C++* door simpelweg een paar stappen te volgen.
Code om filter toe te voegen in Excel-bestand in C ++
#pragma once | |
#include "Aspose.Cells.h" | |
class ExcelFilterData | |
{ | |
void FilterExcelData() | |
{ | |
// Set the license for Aspose.Cells API for filtering data | |
intrusive_ptr<License> CellFilterLicense = new License(); | |
CellFilterLicense->SetLicense(new String("Aspose.Total.lic")); | |
// Instantiate the Workbook object to create empty file to filter out the data | |
intrusive_ptr<IWorkbook> FilterWorkbook = Factory::CreateIWorkbook(); | |
// Access the first worksheet using 0 index for filtering data | |
intrusive_ptr<IWorksheet> FilterWorksheet = FilterWorkbook->GetIWorksheets()->GetObjectByIndex(0); | |
// Adding sample data and values to cells for filtering | |
FilterWorksheet->GetICells()->GetObjectByIndex(new String("A1"))->PutValue("Fruits"); | |
FilterWorksheet->GetICells()->GetObjectByIndex(new String("B1"))->PutValue("Total"); | |
FilterWorksheet->GetICells()->GetObjectByIndex(new String("A2"))->PutValue("Blueberries"); | |
FilterWorksheet->GetICells()->GetObjectByIndex(new String("B2"))->PutValue("2500"); | |
FilterWorksheet->GetICells()->GetObjectByIndex(new String("A3"))->PutValue("Apples"); | |
FilterWorksheet->GetICells()->GetObjectByIndex(new String("B3"))->PutValue("1100"); | |
FilterWorksheet->GetICells()->GetObjectByIndex(new String("A4"))->PutValue("Mangoes"); | |
FilterWorksheet->GetICells()->GetObjectByIndex(new String("B4"))->PutValue("1500"); | |
FilterWorksheet->GetICells()->GetObjectByIndex(new String("A5"))->PutValue("Grapes"); | |
FilterWorksheet->GetICells()->GetObjectByIndex(new String("B5"))->PutValue("1200"); | |
FilterWorksheet->GetICells()->GetObjectByIndex(new String("A6"))->PutValue("Oranges"); | |
FilterWorksheet->GetICells()->GetObjectByIndex(new String("A5"))->PutValue("3000"); | |
FilterWorksheet->GetICells()->GetObjectByIndex(new String("B5"))->PutValue("Count:"); | |
// Set formula for filtering data | |
FilterWorksheet->GetICells()->GetObjectByIndex(new String("E1"))->SetFormula(new String("=SUBTOTAL(2,B1:B6)")); | |
// Set the range for applying AutoFilter | |
FilterWorksheet->GetIAutoFilter()->SetRange(new String("A1:B6")); | |
// Add an AutoFilter to a specific column | |
FilterWorksheet->GetIAutoFilter()->AddFilter(0 , new String("Grapes")); | |
FilterWorksheet->GetIAutoFilter()->Refresh(); | |
// Save the output Excel file with filtered data | |
FilterWorkbook->Save(new String ("FilterOutput.xlsx")); | |
} | |
}; |
U kunt filter maken in Excel-bestand met C++ door voorbeeldgegevens en waarden in de cellen in te voegen. U kunt eenvoudig gegevens in een Excel-bestand filteren zonder MS Excel of een andere toepassing te installeren. In het vorige voorbeeld leerden we Hoe Excel-bestand naar CSV te converteren in C++ waarin de conversie van een Excel-bestand naar een CSV-bestand wordt uitgelegd.