排序数据时指定排序警告
Contents
[
Hide
]
可能的使用场景
请考虑此文本数据,即 {11, 111, 22}。这个文本数据是这样排序的,因为就文本而言,111 在 22 之前。但是,如果你不想把这个数据排序为文本而是数字,那么它将变成 {11, 22, 111} 因为数字上 111 在后面22. Aspose.Cells提供DataSorter.SortAsNumber财产来处理这个问题。请设置此属性真的并且您的文本数据将被排序为数字数据。以下屏幕截图显示了 Microsoft Excel 在对看起来像数字数据的文本数据进行排序时显示的排序警告。
示例代码
下面的示例代码说明了DataSorter.SortAsNumber属性如前所述。请检查其示例 Excel 文件和输出Excel文件寻求更多帮助。
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-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(SpecifyingSortWarningWhileSortingData.class) + "data/"; | |
// Create workbook. | |
Workbook workbook = new Workbook(dataDir + "sampleSortAsNumber.xlsx"); | |
// Access first worksheet. | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Create your cell area. | |
CellArea ca = CellArea.createCellArea("A1", "A20"); | |
// Create your sorter. | |
DataSorter sorter = workbook.getDataSorter(); | |
// Find the index, since we want to sort by column A, so we should know | |
// the index for sorter. | |
int idx = CellsHelper.columnNameToIndex("A"); | |
// Add key in sorter, it will sort in Ascending order. | |
sorter.addKey(idx, SortOrder.ASCENDING); | |
sorter.setSortAsNumber(true); | |
// Perform sort. | |
sorter.sort(worksheet.getCells(), ca); | |
// Save the output workbook. | |
workbook.save(dataDir + "outputSortAsNumber.xlsx"); |