排序工作表数据
Contents
[
Hide
]
排序是我们在处理数据时最常使用的一项重要的日常任务。在本主题中,我们将借助一个简单示例讨论如何对工作表中的数据进行排序。
排序工作表数据
要使用 Aspose.Cells.GridDesktop 的 API 对工作表中的数据进行排序,请按照以下步骤操作:
- 首先创建一个全局对象单元格范围这样它就可以在你班级范围内的任何地方访问
- 为创建一个事件处理程序选定的单元格范围已更改的事件网格桌面. 选定的单元格范围已更改每次更改用户选择的单元格范围时都会触发事件。例如,如果用户选择单元格(包含要排序的数据),那么每当他的选择范围发生变化时,都会触发此事件。
- 事件处理程序提供CellRangeEventArgs进一步以 a 的形式提供单元格(由用户选择)的更新范围的参数单元格范围目的。所以,在这个事件处理程序中,我们将分配这个单元格范围对象(包含更新的单元格范围)到全局单元格范围对象,以便它也可以在代码的其他部分中使用。为了确保我们不会丢失单元格范围,我们将在条件中编写事件处理程序代码
- 现在我们可以编写一些代码来对工作表数据进行排序。首先,访问所需的工作表
- 创建一个排序范围将保留其数据要排序的单元格范围的对象。在排序范围构造函数,我们可以指定工作表,起始行和列的索引,要排序的行数和列数,排序方向(如从上到下或从左到右)等。
- 现在我们可以打电话种类的方法排序范围对象来执行数据排序。在种类方法,我们可以指定要排序的列或行的索引和排序顺序(可以是上升要么降序根据您的要求)
- 最后,我们可以调用无效的方法网格桌面重绘单元格。
在下面给出的示例中,我们演示了如何对列中的数据进行排序。
创建 CellRange 的全局对象和选定的单元格范围已更改GridDesktop 的事件。现在编写如下所示的代码:
This file contains hidden or 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 hidden or 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 hidden or 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(); |