فرز بيانات ورقة العمل
Contents
[
Hide
]
يعد الفرز مهمة روتينية مهمة نستخدمها في الغالب أثناء معالجة البيانات. في هذا الموضوع ، سنناقش بمساعدة مثال بسيط كيف يمكننا فرز البيانات في ورقة عمل.
فرز بيانات ورقة العمل
لفرز البيانات في ورقة عمل باستخدام API من Aspose.Cells.GridDesktop ، يرجى اتباع الخطوات التالية:
- بادئ ذي بدء ، قم بإنشاء كائن عالمي لـCellRange بحيث يمكن الوصول إليها في أي مكان في نطاق فصلك
- قم بإنشاء معالج حدث لـSelectedCellRangeChanged حدثالشبكة. SelectedCellRangeChanged يتم تشغيل الحدث في كل مرة يتم فيها تغيير نطاق الخلايا المحدد من قبل المستخدم. على سبيل المثال ، إذا اختار المستخدم خلايا (تحتوي على بيانات ليتم فرزها) ، فسيتم تشغيل هذا الحدث في كل مرة يتغير فيها نطاق التحديد.
- يوفر معالج الحدثCellRangeEventArgs الحجة التي توفر أيضًا نطاق تحديث الخلايا (المحدد من قبل المستخدم) في شكل ملفCellRange موضوع. لذلك ، في معالج الحدث هذا ، سنقوم بتعيين هذاCellRange كائن (يحتوي على نطاق محدث من الخلايا) إلى العموميةCellRangeبحيث يمكن استخدامه أيضًا في جزء آخر من الكود. للتأكد من أننا لم نفقد نطاق الخلايا ، سنكتب رمز معالج الحدث داخل شرط
- الآن يمكننا كتابة بعض التعليمات البرمجية لفرز بيانات ورقة العمل. بادئ ذي بدء ، قم بالوصول إلى ورقة العمل المطلوبة
- إنشاءSortRange كائن سيحتفظ بنطاق الخلايا المطلوب فرز بياناته. فيSortRange منشئ ، يمكننا تحديد ورقة العمل ، ومؤشرات صف البداية والعمود ، وعدد الصفوف والأعمدة للفرز ، واتجاه الفرز (مثل من أعلى إلى أسفل أو من اليسار إلى اليمين) إلخ.
- الآن يمكننا الاتصالفرز طريقةSortRange كائن لأداء فرز البيانات. فيفرز الطريقة ، يمكننا تحديد فهرس العمود أو الصف المراد فرزه وترتيب الفرز (يمكن أن يكونتصاعدي أوتنازلي وفقًا لمتطلباتك)
- أخيرا ، يمكننا الاتصالباطل طريقةالشبكة لإعادة رسم الخلايا.
في المثال الموضح أدناه ، أوضحنا كيفية فرز البيانات في عمود.
قم بإنشاء كائن عالمي من CellRange وSelectedCellRangeChangedحدث GridDesktop. اكتب الآن الرمز كما هو موضح أدناه:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Creating global variable of CellRange | |
CellRange range; | |
private void gridDesktop1_SelectedCellRangeChanged(object sender, Aspose.Cells.GridDesktop.CellRangeEventArgs e) | |
{ | |
// Checking if the range of cells is not empty | |
if ((e.CellRange.EndColumn - e.CellRange.StartColumn > 0) || | |
(e.CellRange.EndRow - e.CellRange.StartRow > 0)) | |
{ | |
// Assigning the updated CellRange to global variable | |
range = e.CellRange; | |
} | |
} |
الآن نكتب طريقة لترتيب تصاعدي . يمكنك إنشاء زر لـترتيب تصاعدي واكتب الكود أدناه داخلهانقر حدث.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing a worksheet that is currently active | |
Worksheet sheet = gridDesktop1.GetActiveWorksheet(); | |
// Creating SortRange object | |
SortRange sr = new SortRange(sheet, range.StartRow, | |
range.StartColumn, range.EndRow - range.StartRow + 1, | |
range.EndColumn - range.StartColumn + 1, | |
SortOrientation.SortTopToBottom, true); | |
// Sorting data in the specified column in ascending order | |
sr.Sort(range.StartColumn, Aspose.Cells.GridDesktop.SortOrder.Ascending); | |
// Redrawing cells of the Grid | |
gridDesktop1.Invalidate(); |
أخيرًا ، نكتب بعض التعليمات البرمجية لتحقيقهاترتيب تنازلي وظائف. إنشاءترتيب تنازلي زر واكتب الكود أدناه بداخلهانقر حدث.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing a worksheet that is currently active | |
Worksheet sheet = gridDesktop1.GetActiveWorksheet(); | |
// Creating SortRange object | |
SortRange sr = new SortRange(sheet, range.StartRow, range.StartColumn, | |
range.EndRow - range.StartRow + 1, | |
range.EndColumn - range.StartColumn + 1, | |
SortOrientation.SortTopToBottom, true); | |
// Sorting data in the specified column in descending order | |
sr.Sort(range.StartColumn, Aspose.Cells.GridDesktop.SortOrder.Descending); | |
// Redrawing cells of the Grid | |
gridDesktop1.Invalidate(); |