วิธีเพิ่มตัวกรองในไฟล์ XLSX โดยใช้ C ++

ในบทช่วยสอนนี้ เราจะเรียนรู้วิธี เพิ่มตัวกรองในไฟล์ XLSX โดยใช้ C+* คุณสามารถแทรกตัวกรองในแผ่นงาน Excel บน Microsoft Windows, Linux และอื่น ๆ โดยทางโปรแกรมใน C ++

ขั้นตอนการเพิ่มตัวกรองในไฟล์ XLSX โดยใช้ C++

  1. ติดตั้งแพ็คเกจ Aspose.Cells.Cpp ด้วยปลั๊กอิน NuGet package manager
  2. เพิ่มการอ้างอิงไปยังเนมสเปซ Aspose::Cells
  3. เริ่มต้นวัตถุคลาส Workbook เพื่อสร้างตัวอย่างสมุดงาน Excel ใหม่
  4. ใส่ค่าตัวอย่างลงในเซลล์
  5. กำหนดสูตรและช่วงสำหรับการกรองข้อมูล
  6. รวมตัวกรองในคอลัมน์ด้วยเมธอด AddFilter
  7. บันทึกไฟล์ Excel เอาต์พุตหลังจากกรองข้อมูลโดยใช้ C ++

ในตัวอย่างต่อไปนี้ คุณจะสำรวจวิธีการ *สร้างตัวกรองในไฟล์ Excel โดยใช้ C++ เริ่มต้นแผ่นงานเปล่าและ ใช้ตัวกรองในไฟล์ Excel โดยใช้ C++ เพียงทำตามขั้นตอนไม่กี่ขั้นตอน

รหัสเพื่อเพิ่มตัวกรองในไฟล์ Excel ใน 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"));
}
};

คุณสามารถ *สร้างตัวกรองในไฟล์ Excel โดยใช้ C++ โดยใส่ข้อมูลตัวอย่างและค่าลงในเซลล์ คุณสามารถกรองข้อมูลในไฟล์ Excel ได้อย่างง่ายดายโดยไม่ต้องติดตั้ง MS Excel หรือโปรแกรมอื่นๆ ในตัวอย่างก่อนหน้านี้ เราได้เรียนรู้ วิธีแปลงไฟล์ Excel เป็น CSV ใน C++ ที่อธิบายการแปลงจากไฟล์ Excel เป็นไฟล์ CSV

 ไทย