C++を使用してXLSXファイルにフィルターを追加する方法

このチュートリアルでは、C++を使用してXLSXファイルにフィルターを追加する方法を学習します。 C ++では、プログラムでMicrosoft Windows、LinuxなどのExcelワークシートにフィルターを挿入できます。

C++を使用してXLSXファイルにフィルターを追加する手順

  1. NuGetパッケージマネージャープラグインを使用してAspose.Cells.Cppパッケージをインストールします
  2. Aspose::Cells名前空間への参照を追加します
  3. Workbookクラスオブジェクトを初期化して、新しいExcelブックをインスタンス化します
  4. セルにサンプル値を挿入します
  5. データをフィルタリングするための式と範囲を設定する
  6. AddFilterメソッドを使用して列にフィルターを含める
  7. 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に変換する方法を学習しました。

 日本語