Kaip pridėti filtrą XLSX faile naudojant C++

Šioje pamokoje sužinosime, kaip pridėti filtrą į XLSX failą naudojant C++. Filtrą galite įterpti į Excel darbalapį Microsoft Windows, Linux ir kt. programiškai C++.

Veiksmai, kaip pridėti filtrą XLSX faile naudojant C++

  1. Įdiekite paketą Aspose.Cells.Cpp su NuGet paketų tvarkyklės papildiniu
  2. Pridėkite nuorodą į Aspose::Cells vardų erdvę
  3. Inicijuokite Workbook klasės objektą, kad sukurtumėte naują Excel darbaknygę
  4. Įveskite mėginių vertes į langelius
  5. Nustatykite duomenų filtravimo formulę ir diapazoną
  6. Įtraukite filtrą į stulpelį naudodami AddFilter metodą
  7. Išsaugokite išvesties Excel failą po duomenų filtravimo naudojant C++

Šiame pavyzdyje išnagrinėsite, kaip sukurti filtrą Excel faile naudojant C++. Inicijuokite tuščią darbalapį ir * taikykite filtrą Excel faile naudodami C++* tiesiog atlikdami kelis veiksmus.

Kodas, skirtas pridėti filtrą Excel faile 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"));
}
};

Galite sukurti filtrą Excel faile naudodami C++, įterpdami pavyzdinius duomenis ir reikšmes į langelius. Galite lengvai filtruoti duomenis Excel faile neįdiegę MS Excel ar kitos programos. Ankstesniame pavyzdyje išmokome Kaip konvertuoti Excel failą į CSV C++, kuris paaiškina konvertavimą iš Excel failo į CSV failą.

 Latviski