数据排序
Contents
[
Hide
]
数据排序是微软 Excel 的众多有用功能之一。它允许用户对数据进行排序以便于扫描。Aspose.Cells for Python via .NET 允许开发人员按字母顺序或数字顺序对工作表数据进行排序,其方式与微软 Excel 一样。
在 Microsoft Excel 中排序数据
要在 Microsoft Excel 中排序数据:
- 从“排序”菜单中选择“数据”。将显示“排序”对话框。
- 选择排序选项。
通常,排序是针对一个列表执行的 - 定义为一组连续的数据,数据以列显示。
使用 Aspose.Cells for Python Excel 库对数据进行排序
Aspose.Cells for for Python via .NET 提供了 DataSorter 类用于对数据按升序或降序进行排序。该类具有一些重要成员,例如 Key1 … Key3 和 Order1 … Order3 等属性。这些成员用于定义排序关键字并指定关键字排序顺序。
在执行数据排序之前,您必须定义关键字并设置排序顺序。该类提供了 sort 方法,用于根据工作表中的单元格数据执行数据排序。
sort 方法接受以下参数:
- aspose.cells.Cells,基础工作表的单元格。
- aspose.cells.CellArea,单元格范围。在应用数据排序前定义单元格区域。
此示例使用在Microsoft Excel中创建的模板文件"Book1.xls"。在执行下面的代码后,数据将被适当地排序。
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
from aspose.cells import CellArea, SortOrder, Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Instantiate a new Workbook object. | |
# Load a template file. | |
workbook = Workbook(dataDir + "book1.xls") | |
# Get the workbook datasorter object. | |
sorter = workbook.data_sorter | |
# Set the first order for datasorter object. | |
sorter.order1 = SortOrder.DESCENDING | |
# Define the first key. | |
sorter.key1 = 0 | |
# Set the second order for datasorter object. | |
sorter.order2 = SortOrder.ASCENDING | |
# Define the second key. | |
sorter.key2 = 1 | |
# Create a cells area (range). | |
ca = CellArea() | |
# Specify the start row index. | |
ca.start_row = 0 | |
# Specify the start column index. | |
ca.start_column = 0 | |
# Specify the last row index. | |
ca.end_row = 13 | |
# Specify the last column index. | |
ca.end_column = 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") |
如果要进行由左至右的排序,请使用DataSorter.sort_left_to_right属性。
使用Aspose.Cells for Python Excel Library对带有背景颜色的数据进行排序
Excel提供了根据背景颜色对数据进行排序的功能。使用Aspose.Cells for for Python via .NET,可以使用DataSorter在SortOnType中使用CellColor来根据背景颜色对数据进行排序。在函数中包含指定颜色的所有单元格将根据SortOrder设置放在顶部或底部,其余单元格的顺序不会改变。
以下是可以下载以进行此功能测试的样本文件:
outputsampleBackGroundFile.xlsx
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
from aspose.cells import CellArea, SortOnType, SortOrder, Workbook | |
from aspose.pydrawing import Color | |
# 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(sourceDir + "CellsNet46500.xlsx") | |
# Instantiate data sorter object | |
sorter = workbook.data_sorter | |
# Add key for second column for red color | |
sorter.add_key(1, SortOnType.CELL_COLOR, SortOrder.DESCENDING, Color.red) | |
# Sort the data based on the key | |
sorter.sort(workbook.worksheets[0].cells, CellArea.create_cell_area("A2", "C6")) | |
# Save the output file | |
workbook.save(outputDir + "outputSortData_CustomSortList.xlsx") |