이 튜토리얼에서는 **C++를 사용하여 XLSX 파일에 필터를 추가하는 방법을 배웁니다. C++에서 프로그래밍 방식으로 Microsoft Windows, Linux 등의 Excel 워크시트에 필터를 삽입할 수 있습니다.
C++를 사용하여 XLSX 파일에 필터를 추가하는 단계
- NuGet 패키지 관리자 플러그인으로 Aspose.Cells.Cpp 패키지 설치
- Aspose::Cells 네임스페이스에 대한 참조 추가
- Workbook 클래스 개체를 초기화하여 새 Excel 통합 문서를 인스턴스화합니다.
- 셀에 샘플 값 삽입
- 데이터 필터링을 위한 수식 및 범위 설정
- AddFilter 메서드를 사용하여 열에 필터 포함
- C++를 사용하여 데이터 필터링 후 출력 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")); | |
} | |
}; |
셀에 샘플 데이터와 값을 삽입하여 *C++*를 사용하여 Excel 파일에 필터를 생성할 수 있습니다. MS Excel이나 다른 응용 프로그램을 설치하지 않고도 Excel 파일의 데이터를 쉽게 필터링할 수 있습니다. 이전 예에서 Excel 파일에서 CSV 파일로의 변환을 설명하는 C++에서 Excel 파일을 CSV로 변환하는 방법에 대해 배웠습니다.