W tym samouczku nauczymy się dodawać filtr w pliku XLSX za pomocą C++. Możesz wstawić filtr w arkuszu programu Excel w systemie Microsoft Windows, Linux itp. programowo w C ++.
Kroki, aby dodać filtr w pliku XLSX przy użyciu C++
- Zainstaluj pakiet Aspose.Cells.Cpp z wtyczką menedżera pakietów NuGet
- Dodaj odwołanie do przestrzeni nazw Aspose::Cells
- Zainicjuj obiekt klasy Workbook, aby utworzyć instancję nowego skoroszytu programu Excel
- Wstaw przykładowe wartości do komórek
- Ustaw formułę i zakres filtrowania danych
- Dołącz filtr do kolumny za pomocą metody AddFilter
- Zapisz wyjściowy plik programu Excel po przefiltrowaniu danych przy użyciu języka C++
W poniższym przykładzie dowiesz się, jak utworzyć filtr w pliku programu Excel przy użyciu języka C++. Zainicjuj pusty arkusz i zastosuj filtr w pliku Excel przy użyciu C++, wykonując po prostu kilka kroków.
Kod do dodania filtra w pliku Excel w 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")); | |
} | |
}; |
Możesz utworzyć filtr w pliku Excel przy użyciu C++, wstawiając przykładowe dane i wartości do komórek. Możesz łatwo filtrować dane w pliku Excel bez instalowania MS Excel lub jakiejkolwiek innej aplikacji. W poprzednim przykładzie dowiedzieliśmy się, że Jak przekonwertować plik Excela na CSV w C++ wyjaśnia konwersję z pliku Excel do pliku CSV.