تصفية البيانات

عنصرية البيانات

التصفية التلقائية هي أسرع طريقة لتحديد العناصر الوحيدة التي ترغب في عرضها في قائمة. تتيح ميزة التصفية التلقائية للمستخدمين تصفية العناصر في القائمة وفقًا لمعايير محددة. تصفية بناءً على النصوص أو الأرقام أو التواريخ.

التصفية التلقائية في Microsoft Excel

لتفعيل ميزة التصفية التلقائية في Microsoft Excel:

  1. انقر على صف العنوان في ورقة العمل.
  2. من قائمة البيانات، حدد تصفية ثم تصفية تلقائية.

عند تطبيق التصفية التلقائية على ورقة عمل، يظهر التبديل (السهام السوداء) إلى يمين عناوين العمود.

  1. انقر على سهم التصفية لرؤية قائمة الخيارات التصفية.

بعض خيارات التصفية التلقائية هي:

الخيارات الوصف
All
Custom
Filter by Color
Date Filters
Number Filters
Text Filters
Blanks/Non Blanks

يقوم المستخدمون بتصفية بيانات ورقة العمل يدويًا في Microsoft Excel باستخدام هذه الخيارات.

تصفية أوتوماتيكية بواسطة Aspose.Cells

توفر Aspose.Cells فئة Workbook التي تمثل ملف Excel. تحتوي فئة Workbook على مجموعة من ورق العمل وتسمح بالوصول إلى كل ورق عمل في ملف Excel.

ورقة العمل ممثلة بفئة ورقة العمل، وتقدم فئة ورقة العمل مجموعة واسعة من الخصائص والأساليب لإدارة أوراق العمل. لإنشاء تصفية تلقائية، استخدم خاصية AutoFilter من فئة ورقة العمل. وتعتبر خاصية AutoFilter كائنًا لفئة AutoFilter توفر خاصية Range لتحديد نطاق الخلايا التي تتكون من صف العنوان. يتم تطبيق التصفية التلقائية على نطاق الخلايا الذي يشكل صف العناوين.

في كل ورقة عمل، يمكنك تحديد نطاق تصفية واحد فقط وهذا محدود بواسطة Microsoft Excel. لتصفية البيانات المخصصة، استخدم الأسلوب AutoFilter.Custom.

في المثال المعطى أدناه، قمنا بإنشاء نفس تصفية الأوتوماتيكية باستخدام Aspose.Cells كما قمنا بإنشائها باستخدام مايكروسوفت إكسل في القسم السابق.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Creating AutoFilter by giving the cells range of the heading row
worksheet.AutoFilter.Range = "A1:B1";
// Saving the modified Excel file
workbook.Save(dataDir + "output.out.xls");

أنواع مختلفة من التصفية

توفر Aspose.Cells خيارات متعددة لتطبيق مرشحات مختلفة مثل مرشح الألوان، مرشح التاريخ، مرشح الأرقام، مرشح النص، مرشح الخانات الفارغة والخانات الغير فارغة.

لون التعبئة

توفر Aspose.Cells إضافة الوظيفة إضافة مرشح لون التعبئة لتصفية البيانات استنادًا إلى خاصية لون التعبئة للخلايا. في المثال الوارد أدناه، يتم استخدام ملف قالب يحتوي على ألوان تعبئة مختلفة في العمود الأول من الورقة لاختبار وظيفة تصفية الألوان. يمكن تنزيل الملفات العينية من الروابط التالية.

  1. ColouredCells.xlsx
  2. FilteredColouredCells.xlsx
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
//Source directory
string sourceDir = RunExamples.Get_SourceDirectory();
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(sourceDir + "ColouredCells.xlsx");
// Instantiating a CellsColor object for foreground color
CellsColor clrForeground = workbook.CreateCellsColor();
clrForeground.Color = Color.Red;
// Instantiating a CellsColor object for background color
CellsColor clrBackground = workbook.CreateCellsColor();
clrBackground.Color = Color.White;
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Call AddFillColorFilter function to apply the filter
worksheet.AutoFilter.AddFillColorFilter(0, BackgroundType.Solid, clrForeground, clrBackground);
// Call refresh function to update the worksheet
worksheet.AutoFilter.Refresh();
// Saving the modified Excel file
workbook.Save(outputDir + "FilteredColouredCells.xlsx");
تاريخ

يمكن تنفيذ أنواع مختلفة من عمليات تصفية التاريخ مثل تصفية جميع الصفوف التي تحتوي على تواريخ في يناير 2018. يوضح الكود العيني التالي هذا التصفية باستخدام الدالة AddDateFilter. يتم إعطاء ملفات عينية أدناه.

  1. Date.xlsx
  2. FilteredDate.xlsx
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
//Source directory
string sourceDir = RunExamples.Get_SourceDirectory();
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(sourceDir + "Date.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Call AddDateFilter function to apply the filter
worksheet.AutoFilter.AddDateFilter(0, DateTimeGroupingType.Month, 2018, 1, 0, 0, 0, 0);
// Call refresh function to update the worksheet
worksheet.AutoFilter.Refresh();
// Saving the modified Excel file
workbook.Save(outputDir + "FilteredDate.xlsx");
تاريخ ديناميكي

في بعض الأحيان، تكون التصفيات الديناميكية مطلوبة بناءً على التاريخ مثل جميع الخلايا التي تحتوي على تواريخ في يناير بغض النظر عن السنة. في هذه الحالة، يتم استخدام دالة DynamicFilter كما هو موضح في الكود العيني التالي. يتم إعطاء ملفات عينية أدناه.

  1. Date.xlsx
  2. FilteredDynamicDate.xlsx
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
//Source directory
string sourceDir = RunExamples.Get_SourceDirectory();
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(sourceDir + "Date.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Call DynamicFilter function to apply the filter
worksheet.AutoFilter.DynamicFilter(0, DynamicFilterType.January);
// Call refresh function to update the worksheet
worksheet.AutoFilter.Refresh();
// Saving the modified Excel file
workbook.Save(outputDir + "FilteredDynamicDate.xlsx");
رقم

يمكن تطبيق مرشحات مخصصة باستخدام Aspose.Cells مثل اختيار الخلايا التي تحتوي على أرقام بين نطاق معين. يظهر المثال التالي استخدام الوظيفة المخصصة لتصفية الأرقام. يتم تقديم ملفات العينات أدناه.

  1. Number.xlsx
  2. FilteredNumber.xlsx
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
//Source directory
string sourceDir = RunExamples.Get_SourceDirectory();
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(sourceDir + "Number.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Call Custom function to apply the filter
worksheet.AutoFilter.Custom(0, FilterOperatorType.GreaterOrEqual, 5, true, FilterOperatorType.LessOrEqual, 10);
// Call refresh function to update the worksheet
worksheet.AutoFilter.Refresh();
// Saving the modified Excel file
workbook.Save(outputDir + "FilteredNumber.xlsx");
نص

إذا كانت العمود يحتوي على نص ويتعين تحديد الخلايا التي تحتوي على نص معين، فيمكن استخدام دالة Filter(). في المثال التالي، يحتوي ملف القالب على قائمة بالدول ويجب تحديد الصف الذي يحتوي على اسم دولة معين. يوضح الكود التالي تصفية النصوص. يتم تقديم ملفات عينة أدناه.

  1. Text.xlsx
  2. FilteredText.xlsx
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
//Source directory
string sourceDir = RunExamples.Get_SourceDirectory();
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(sourceDir + "Text.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Call Filter function to apply the filter
worksheet.AutoFilter.Filter(0, "Angola");
// Call refresh function to update the worksheet
worksheet.AutoFilter.Refresh();
// Saving the modified Excel file
workbook.Save(outputDir + "FilteredText.xlsx");
فراغات

إذا كان العمود يحتوي على نص بحيث تكون بعض الخلايا فارغة ويتطلب التصفية لتحديد الصفوف فقط حيث يتواجد الخلية الفارغة، فيمكن استخدام الدالة MatchBlanks() كما هو موضح أدناه. تتم تقديم ملفات عينة أدناه.

  1. ملف فارغ.xlsx
  2. ملف فارغ مصفى.xlsx
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
//Source directory
string sourceDir = RunExamples.Get_SourceDirectory();
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(sourceDir + "Blank.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Call MatchBlanks function to apply the filter
worksheet.AutoFilter.MatchBlanks(0);
// Call refresh function to update the worksheet
worksheet.AutoFilter.Refresh();
// Saving the modified Excel file
workbook.Save(outputDir + "FilteredBlank.xlsx");
غير فارغة

عندما يتعين تصفية الخلايا التي تحتوي على أي نص، استخدم دالة MatchNonBlanks كمصفية كما هو موضح أدناه. تتم تقديم ملفات عينة أدناه.

  1. ملف فارغ.xlsx
  2. ملف تصفية غير فارغ.xlsx
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
//Source directory
string sourceDir = RunExamples.Get_SourceDirectory();
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(sourceDir + "Blank.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Call MatchNonBlanks function to apply the filter
worksheet.AutoFilter.MatchNonBlanks(0);
// Call refresh function to update the worksheet
worksheet.AutoFilter.Refresh();
// Saving the modified Excel file
workbook.Save(outputDir + "FilteredNonBlank.xlsx");
تصفية مخصصة مع الاحتواء

توفر Excel عوامل تصفية مخصصة مثل تصفية الصفوف التي تحتوي على سلسلة نصية معينة. تتوفر هذه الميزة في Aspose.Cells وتُظهر أدناه عن طريق تصفية الأسماء في الملف النموذجي. الملفات النموذجية معروضة أدناه.

  1. sourseSampleCountryNames.xlsx
  2. outSourseSampleCountryNames.xlsx.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a Workbook object containing sample data
Workbook workbook = new Workbook("sourseSampleCountryNames.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Creating AutoFilter by giving the cells range
worksheet.AutoFilter.Range = "A1:A18";
// Initialize filter for rows containing string "Ba"
worksheet.AutoFilter.Custom(0, FilterOperatorType.Contains, "Ba");
//Refresh the filter to show/hide filtered rows
worksheet.AutoFilter.Refresh();
// Saving the modified Excel file
workbook.Save("outSourseSampleCountryNames.xlsx");
تصفية مخصصة مع عدم الاحتواء

توفر Excel عوامل تصفية مخصصة مثل تصفية الصفوف التي لا تحتوي على سلسلة نصية معينة. تتوفر هذه الميزة في Aspose.Cells وتُظهر أدناه عن طريق تصفية الأسماء في الملف النموذجي المعطى أدناه.

  1. sourseSampleCountryNames.xlsx.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a Workbook object containing sample data
Workbook workbook = new Workbook("sourseSampleCountryNames.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Creating AutoFilter by giving the cells range
worksheet.AutoFilter.Range = "A1:A18";
// Initialize filter for rows containing string "Ba"
worksheet.AutoFilter.Custom(0, FilterOperatorType.NotContains, "Be");
//Refresh the filter to show/hide filtered rows
worksheet.AutoFilter.Refresh();
// Saving the modified Excel file
workbook.Save("outSourseSampleCountryNames.xlsx");
تصفية مخصصة تبدأ ب

توفر Excel عوامل تصفية مخصصة مثل تصفية الصفوف التي تبدأ بسلسلة نصية معينة. تتوفر هذه الميزة في Aspose.Cells وتُظهر أدناه عن طريق تصفية الأسماء في الملف النموذجي المعطى أدناه.

  1. sourseSampleCountryNames.xlsx.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a Workbook object containing sample data
Workbook workbook = new Workbook(sourceDir + "sourseSampleCountryNames.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Creating AutoFilter by giving the cells range
worksheet.AutoFilter.Range = "A1:A18";
// Initialize filter for rows starting with string "Ba"
worksheet.AutoFilter.Custom(0, FilterOperatorType.BeginsWith, "Ba");
//Refresh the filter to show/hide filtered rows
worksheet.AutoFilter.Refresh();
// Saving the modified Excel file
workbook.Save(outputDir + "outSourseSampleCountryNames.xlsx");
تصفية مخصصة تنتهي ب

يوفر Excel تصفية مخصصة مثل تصفية الصفوف التي تنتهي بسلسلة معينة. يتوفر هذا الميزة في Aspose.Cells ويتم استعراضها أدناه من خلال تصفية الأسماء في ملف العينة المُعطى أدناه.

  1. sourseSampleCountryNames.xlsx.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a Workbook object containing sample data
Workbook workbook = new Workbook(sourceDir + "sourseSampleCountryNames.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Creating AutoFilter by giving the cells range
worksheet.AutoFilter.Range = "A1:A18";
// Initialize filter for rows end with string "ia"
worksheet.AutoFilter.Custom(0, FilterOperatorType.BeginsWith, "ia");
//Refresh the filter to show/hide filtered rows
worksheet.AutoFilter.Refresh();
// Saving the modified Excel file
workbook.Save(outputDir + "outSourseSampleCountryNames.xlsx");

مواضيع متقدمة