Come aggiungere un filtro nel file XLSX usando C++

In questo tutorial impareremo come aggiungere un filtro nel file XLSX usando C++. È possibile inserire il filtro nel foglio di lavoro di Excel su Microsoft Windows, Linux, ecc. a livello di codice in C++.

Passaggi per aggiungere un filtro nel file XLSX utilizzando C++

  1. Installa il pacchetto Aspose.Cells.Cpp con il plug-in di gestione dei pacchetti NuGet
  2. Aggiungi il riferimento allo spazio dei nomi Aspose::Cells
  3. Inizializza l’oggetto Class Workbook per creare un’istanza della nuova cartella di lavoro di Excel
  4. Inserisci i valori di esempio nelle celle
  5. Imposta formula e intervallo per filtrare i dati
  6. Includi un filtro in una colonna con il metodo AddFilter
  7. Salva il file Excel di output dopo aver filtrato i dati utilizzando C++

Nell’esempio seguente, esplorerai come creare un filtro in un file Excel utilizzando C++. Inizializza un foglio di lavoro vuoto e applica il filtro nel file Excel utilizzando C++ semplicemente seguendo alcuni passaggi.

Codice per aggiungere filtro nel file Excel 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"));
}
};

Puoi creare un filtro nel file Excel usando C++ inserendo dati e valori di esempio nelle celle. Puoi facilmente filtrare i dati nel file Excel senza installare MS Excel o qualsiasi altra applicazione. Nell’esempio precedente, abbiamo appreso Come convertire file Excel in CSV in C++ che spiega la conversione da file Excel a file CSV.

 Italiano