Cara Menambahkan Filter di File XLSX menggunakan C++

Dalam tutorial ini, kita akan mempelajari cara menambahkan filter pada file XLSX menggunakan C++. Anda dapat menyisipkan filter di lembar kerja Excel di Microsoft Windows, Linux, dll. secara terprogram di C++.

Langkah-langkah Menambahkan Filter di File XLSX menggunakan C++

  1. Instal paket Aspose.Cells.Cpp dengan plugin pengelola paket NuGet
  2. Tambahkan referensi ke Aspose::Cells namespace
  3. Inisialisasi objek Kelas Workbook untuk membuat instance buku kerja Excel baru
  4. Masukkan nilai sampel ke dalam sel
  5. Tetapkan rumus dan rentang untuk memfilter data
  6. Sertakan filter ke kolom dengan metode AddFilter
  7. Simpan file output Excel setelah memfilter data menggunakan C++

Dalam contoh berikut, Anda akan menjelajahi cara membuat filter di file Excel menggunakan C++. Inisialisasi lembar kerja kosong dan terapkan filter dalam file Excel menggunakan C++ hanya dengan mengikuti beberapa langkah.

Kode untuk Menambahkan Filter di File Excel di 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"));
}
};

Anda dapat membuat filter dalam file Excel menggunakan C++ dengan memasukkan data sampel dan nilai ke dalam sel. Anda dapat dengan mudah menyaring data dalam file Excel tanpa menginstal MS Excel atau aplikasi lainnya. Pada contoh sebelumnya, kita mempelajari Cara Mengonversi File Excel ke CSV di C++ yang menjelaskan konversi dari file Excel ke file CSV.

 Indonesian