数据排序与C++
在 Microsoft Excel 中排序数据
要在 Microsoft Excel 中排序数据:
- 从“排序”菜单中选择“数据”。将显示“排序”对话框。
- 选择排序选项。
通常,排序是针对一个列表执行的 - 定义为一组连续的数据,数据以列显示。
使用 Aspose.Cells 进行数据排序
Aspose.Cells 提供了用于按升序或降序对数据进行排序的 DataSorter 类。该类具有一些重要成员,例如 Key1 … Key3 和 Order1 … Order3 等属性。这些成员用于定义排序键并指定键排序顺序。
在执行数据排序之前,您必须定义关键字并设置排序顺序。该类提供了 Sort 方法,用于根据工作表中的单元格数据执行数据排序。
Sort 方法接受以下参数:
此示例使用在Microsoft Excel中创建的模板文件"Book1.xls"。在执行下面的代码后,数据将被适当地排序。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input excel file
U16String inputFilePath = srcDir + u"book1.xls";
// Path of output excel file
U16String outputFilePath = outDir + u"output.out.xls";
// Create workbook
Workbook workbook(inputFilePath);
// Get the workbook datasorter object
DataSorter sorter = workbook.GetDataSorter();
// Set the first order for datasorter object
sorter.SetOrder1(SortOrder::Descending);
// Define the first key
sorter.SetKey1(0);
// Set the second order for datasorter object
sorter.SetOrder2(SortOrder::Ascending);
// Define the second key
sorter.SetKey2(1);
// Create a cells area (range)
CellArea ca = CellArea::CreateCellArea(0, 0, 13, 1);
// Sort data in the specified data range (A1:B14)
sorter.Sort(workbook.GetWorksheets().Get(0).GetCells(), ca);
// Save the excel file
workbook.Save(outputFilePath);
std::cout << "Data sorted successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
以背景颜色排序数据
Excel提供了根据背景颜色对数据进行排序的功能。使用Aspose.Cells的DataSorter,可以使用SortOnType.CellColor在AddKey()中对基于背景颜色的数据进行排序。AddKey()中包含指定颜色的所有单元格,按照SortOrder设置的方式放置在顶部或底部,其余单元格的顺序保持不变。
以下是可以下载以进行此功能测试的样本文件:
outputsampleBackGroundFile.xlsx
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input excel file
U16String inputFilePath = srcDir + u"CellsNet46500.xlsx";
// Path of output excel file
U16String outputFilePath = outDir + u"outputSortData_CustomSortList.xlsx";
// Create a workbook object and load template file
Workbook workbook(inputFilePath);
// Instantiate data sorter object
DataSorter sorter = workbook.GetDataSorter();
// Add key for second column for red color
sorter.AddColorKey(1, SortOnType::CellColor, SortOrder::Descending, Color::Red());
// Sort the data based on the key
sorter.Sort(workbook.GetWorksheets().Get(0).GetCells(), CellArea::CreateCellArea(u"A2", u"C6"));
// Save the output file
workbook.Save(outputFilePath);
std::cout << "Data sorted successfully!" << std::endl;
Aspose::Cells::Cleanup();
}