排序工作表数据
Contents
[
Hide
]
排序是处理数据时经常使用的重要例行任务。在本主题中,我们将通过一个简单的示例讨论如何在工作表中对数据进行排序。
对工作表数据进行排序
要使用Aspose.Cells.GridDesktop的API在工作表中对数据进行排序,请按以下步骤操作:
- 首先创建一个CellRange的全局对象,以便在类的范围内随时访问
- 为GridDesktop的SelectedCellRangeChanged事件创建事件处理程序。SelectedCellRangeChanged事件在用户更改已选择单元格范围时触发。例如,如果用户选择了包含要排序的数据的单元格,那么每次他选择的范围发生变化时,都会触发此事件。
- 事件处理程序提供CellRangeEventArgs参数,进一步提供单元格范围的更新(用户选择的)作为CellRange对象的形式。因此,在此事件处理程序中,我们将分配此CellRange对象(包含更新后的单元格范围)给全局CellRange对象,以便在代码的其他部分中也可以使用。为了确保我们不丢失单元格范围,我们将在一个条件中编写事件处理程序代码
- 现在我们可以编写一些代码来对工作表数据进行排序。首先访问所需的工作表
- 创建一个SortRange对象,用于存储要排序其数据的单元格范围。在SortRange构造函数中,我们可以指定工作表,起始行和列的索引,要排序的行数和列数,排序的方向(如从上到下或从左到右)等。
- 现在我们可以调用SortRange对象的Sort方法来执行数据排序。在Sort方法中,我们可以指定要排序的列或行的索引以及排序顺序(根据您的要求可以是升序或降序)
- 最后,我们可以调用GridDesktop的Invalidate方法来重绘单元格。
在下面的示例中,我们演示了如何按列对数据进行排序。
创建一个CellRange的全局对象和GridDesktop的SelectedCellRangeChanged事件。现在按照下面给出的代码编写:
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; | |
} | |
} |
现在我们为升序排序编写方法。您可以创建一个升序排序按钮,并在其Click事件中编写下面的代码。
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(); |
最后,我们编写一些代码来实现降序排序功能。创建一个降序排序按钮,并在其Click事件中编写下面的代码。
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(); |