Como adicionar filtro no arquivo XLSX usando C++

Neste tutorial, aprenderemos como adicionar filtro no arquivo XLSX usando C++. Você pode inserir o filtro na planilha do Excel no Microsoft Windows, Linux, etc. programaticamente em C++.

Etapas para adicionar filtro no arquivo XLSX usando C++

  1. Instale o pacote Aspose.Cells.Cpp com o plug-in do gerenciador de pacotes NuGet
  2. Adicione a referência ao namespace Aspose::Cells
  3. Inicialize o objeto Class Workbook para instanciar a nova pasta de trabalho do Excel
  4. Insira valores de amostra nas células
  5. Definir fórmula e intervalo para filtrar dados
  6. Incluir um filtro em uma coluna com o método AddFilter
  7. Salve o arquivo Excel de saída após filtrar os dados usando C++

No exemplo a seguir, você explorará como criar filtro no arquivo Excel usando C++. Inicialize uma planilha vazia e aplique o filtro no arquivo Excel usando C++ simplesmente seguindo alguns passos.

Código para adicionar filtro no arquivo do Excel em 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"));
}
};

Você pode criar um filtro no arquivo do Excel usando C++ inserindo dados e valores de amostra nas células. Você pode facilmente filtrar dados no arquivo Excel sem instalar o MS Excel ou qualquer outro aplicativo. No exemplo anterior, aprendemos Como converter arquivo do Excel para CSV em C++ que explica a conversão do arquivo Excel para um arquivo CSV.

 Português