หัวข้อนี้ให้รายละเอียดที่จำเป็นเกี่ยวกับวิธีกรองข้อมูลในตาราง Excel โดยใช้ C# โค้ดตัวอย่างแสดงวิธีเพิ่มตัวกรองใน Excel โดยใช้ C# ในไฟล์ XLSX นอกจากนี้ คุณสามารถสร้างตัวกรองไดนามิกในสเปรดชีต Excel โดยใช้ C# ได้ในไม่กี่ขั้นตอนง่ายๆ
ขั้นตอนในการกรองข้อมูลในตาราง Excel โดยใช้ C#
- รับแพ็คเกจ Aspose.Cells for .NET จาก NuGet.org
- นำเข้าเนมสเปซ Aspose.Cells ไปยังโครงการ
- สร้างอินสแตนซ์และตั้งค่าวัตถุใบอนุญาตด้วยวิธี SetLicense
- สร้าง Workbook ตั้งแต่เริ่มต้น
- แทรกข้อมูลลงในแผ่นงานแรกในสมุดงาน
- กำหนดช่วง AutoFilter และเพิ่มการกรองแบบกำหนดเอง
- บันทึกข้อมูลที่กรองไปยังไฟล์ XLSX
การกรองข้อมูลแบบไดนามิกหมายถึงการแสดงเรกคอร์ดเหล่านั้นที่ครอบคลุมเกณฑ์บางอย่างเท่านั้น หากคุณต้องการรับระเบียนสำหรับผลไม้เฉพาะ (ในกรณีนี้คือองุ่น) เท่านั้น ตัวอย่างนี้จะสาธิตคุณลักษณะตัวกรองอัตโนมัติของ Excel และช่วยให้คุณเข้าใจวิธีการใช้ตัวกรองแบบกำหนดเองใน Excel ใน C#
โค้ดตัวอย่างเพื่อกรองข้อมูลในตาราง Excel โดยใช้ C#
using System; | |
using Aspose.Cells; | |
namespace FilterDataInExcelTable | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Create License object and set it at the start before using any other APIs | |
Aspose.Cells.License Aspose_Cells_lic = new Aspose.Cells.License(); | |
Aspose_Cells_lic.SetLicense("Aspose.Cells.lic"); | |
//Create an empty Excel workbook | |
Workbook FilteringDataWorkbook = new Workbook(); | |
//Get the worksheet at first indexed position in the workbook - default worksheet | |
Worksheet FilteringDataSheet = FilteringDataWorkbook.Worksheets[0]; | |
//Obtain the cells collection from the first sheet | |
Cells FilteringDataCells = FilteringDataSheet.Cells; | |
//Put data/values into the cells for the table | |
FilteringDataCells["A1"].PutValue("Fruits"); | |
FilteringDataCells["B1"].PutValue("Total"); | |
FilteringDataCells["A2"].PutValue("Blueberries"); | |
FilteringDataCells["B2"].PutValue(2500); | |
FilteringDataCells["A3"].PutValue("Apples"); | |
FilteringDataCells["B3"].PutValue(1100); | |
FilteringDataCells["A4"].PutValue("Mangoes"); | |
FilteringDataCells["B4"].PutValue(1500); | |
FilteringDataCells["A5"].PutValue("Grapes"); | |
FilteringDataCells["B5"].PutValue(1200); | |
FilteringDataCells["A6"].PutValue("Oranges"); | |
FilteringDataCells["B6"].PutValue(3000); | |
FilteringDataCells["D1"].PutValue("Count:"); | |
//Specify formula to E1 cell - this formula would give you count | |
FilteringDataCells["E1"].Formula = "=SUBTOTAL(2,B1:B6)"; | |
//Set the range to which the specified autofilters would be applied | |
FilteringDataSheet.AutoFilter.Range = "A1:B6"; | |
//Now add your desired filter to first column to select your desired data | |
FilteringDataSheet.AutoFilter.AddFilter(0, "Grapes"); | |
FilteringDataSheet.AutoFilter.Refresh(); | |
//Save Excel XLSX file | |
FilteringDataWorkbook.Save("FilteredData.xlsx"); | |
} | |
} | |
} |
ส่วนรหัส (ด้านบน) จำลองสมุดงานเปล่าแล้วแทรกข้อมูลลงในช่วงของเซลล์ในแผ่นงานแรก รหัส C# ตั้งค่าช่วงตัวกรองอัตโนมัติของ Excel; มันเพิ่มหรือใช้ตัวกรองที่กำหนดเองในสเปรดชีต excel สุดท้าย จะบันทึกข้อมูลที่กรองแล้วไปยังไฟล์ XLSX บนดิสก์ คุณอาจสนใจหัวข้อ วิธีสร้างแผนภูมิวงกลม Excel ใน C#