فرز البيانات
فرز البيانات في Microsoft Excel
لفرز البيانات في Microsoft Excel:
- يختاربيانات منفرز قائمة. سيتم عرض مربع حوار الترتيب.
- حدد خيار الفرز.
بشكل عام ، يتم إجراء الفرز على قائمة - يتم تعريفها على أنها مجموعة متجاورة من البيانات حيث يتم عرض البيانات في أعمدة.
فرز البيانات مع Aspose.Cells
يوفر Aspose.Cells ملفDataSorterفئة تستخدم لفرز البيانات بترتيب تصاعدي أو تنازلي. يحتوي الفصل على بعض الأعضاء المهمين ، على سبيل المثال ، خصائص مثل Key1 … Key3 و Order1 … Order3. يتم استخدام هؤلاء الأعضاء لتحديد المفاتيح التي تم فرزها وتحديد ترتيب فرز المفاتيح.
يجب عليك تحديد المفاتيح وتعيين ترتيب الفرز قبل تنفيذ فرز البيانات. يوفر الفصلفرزالطريقة المستخدمة لإجراء فرز البيانات استنادًا إلى بيانات الخلية في ورقة العمل.
الفرزتقبل الطريقة المعلمات التالية:
- Aspose.Cells.Cells، خلايا ورقة العمل الأساسية.
- Aspose.Cells.CellArea، نطاق الخلايا. حدد منطقة الخلية قبل تطبيق فرز البيانات.
يستخدم هذا المثال ملف القالب “Book1.xls” الذي تم إنشاؤه في Microsoft Excel. بعد تنفيذ الكود أدناه ، يتم فرز البيانات بشكل مناسب.
// 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); | |
// Instantiate a new Workbook object. | |
// Load a template file. | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Get the workbook datasorter object. | |
DataSorter sorter = workbook.DataSorter; | |
// Set the first order for datasorter object. | |
sorter.Order1 = Aspose.Cells.SortOrder.Descending; | |
// Define the first key. | |
sorter.Key1 = 0; | |
// Set the second order for datasorter object. | |
sorter.Order2 = Aspose.Cells.SortOrder.Ascending; | |
// Define the second key. | |
sorter.Key2 = 1; | |
// Create a cells area (range). | |
CellArea ca = new CellArea(); | |
// Specify the start row index. | |
ca.StartRow = 0; | |
// Specify the start column index. | |
ca.StartColumn = 0; | |
// Specify the last row index. | |
ca.EndRow = 13; | |
// Specify the last column index. | |
ca.EndColumn = 1; | |
// Sort data in the specified data range (A1:B14) | |
sorter.Sort(workbook.Worksheets[0].Cells, ca); | |
// Save the excel file. | |
workbook.Save(dataDir + "output.out.xls"); |
فرز البيانات مع لون الخلفية
يوفر Excel ميزات لفرز البيانات بناءً على لون الخلفية. يتم توفير نفس الميزة باستخدام Aspose.Cells باستخدام DataSorter حيثSortOnType يمكن استخدام لون الخلية في تنسيقAddKey () لفرز البيانات بناءً على لون الخلفية. جميع الخلايا التي تحتوي على لون محدد في ملفAddKey ()، يتم وضع الوظيفة في الأعلى أو الأسفل وفقًا لإعداد SortOrder ولا يتم تغيير ترتيب باقي الخلايا على الإطلاق.
فيما يلي نماذج الملفات التي يمكن تنزيلها لاختبار هذه الميزة:
outputsampleBackGroundFile.xlsx
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Create a workbook object and load template file | |
Workbook workbook = new Workbook(sourceDir + "CellsNet46500.xlsx"); | |
// Instantiate data sorter object | |
DataSorter sorter = workbook.DataSorter; | |
// Add key for second column for red color | |
sorter.AddKey(1, SortOnType.CellColor, SortOrder.Descending, Color.Red); | |
// Sort the data based on the key | |
sorter.Sort(workbook.Worksheets[0].Cells, CellArea.CreateCellArea("A2", "C6")); | |
// Save the output file | |
workbook.Save(outputDir + "outputSortData_CustomSortList.xlsx"); |