В этом руководстве мы узнаем, как ** добавить фильтр в файл XLSX с помощью C++**. Вы можете вставить фильтр в рабочий лист Excel в Microsoft Windows, Linux и т. д. программно на C++.
Действия по добавлению фильтра в файл XLSX с помощью C++
- Установите пакет Aspose.Cells.Cpp с подключаемым модулем диспетчера пакетов NuGet.
- Добавьте ссылку на пространство имен Aspose::Cells
- Инициализируйте объект класса Workbook, чтобы создать экземпляр новой книги Excel.
- Вставьте образцы значений в ячейки
- Установите формулу и диапазон для фильтрации данных
- Включить фильтр в столбец с методом AddFilter
- Сохраните выходной файл Excel после фильтрации данных с помощью C++.
В следующем примере вы узнаете, как создать фильтр в файле Excel с помощью C++. Инициализируйте пустой рабочий лист и примените фильтр в файле Excel с помощью C++, просто выполнив несколько шагов.
Код для добавления фильтра в файл Excel на С++
#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")); | |
} | |
}; |
Вы можете создать фильтр в файле Excel с помощью C++, вставив образцы данных и значений в ячейки. Вы можете легко фильтровать данные в файле Excel без установки MS Excel или любого другого приложения. В предыдущем примере мы узнали Как преобразовать файл Excel в CSV на C++, что объясняет преобразование файла Excel в файл CSV.