I have refined sample code shared by Babar to implement your scenario/ case precisely. I have used a sample Excel file (attached) which contains some data set as per your scenario. I have written the following sample code to accomplish your task a bit. Please refer to my sample code and you may add and write your own sample code accordingly for your data in your spreadsheet for your needs. I have also attached the output Excel file for your reference.
Workbook workbook = new Workbook(“e:\test2\Bk_sort1.xlsx”);
//Get the first worksheet.
Worksheet worksheet = workbook.Worksheets[0];
//Get the cells.
Cells cells = worksheet.Cells;
//Identify keys for the columns based on headers’ titles
//We need to find the header/title cells and then evaluate the column index for data sorting.
Aspose.Cells.Cell prevcell = null;
Aspose.Cells.Cell foundCell = null;
int Empindex = 0;
string stringToFind = “Employeeid”; //You can change the string as required, also you may find other titles accordingly too.
FindOptions opts = new FindOptions();
//Create a Cells Area
CellArea ca = new CellArea();
ca.StartRow = 0;
ca.StartColumn = 0;
ca.EndRow = cells.MaxDataRow;
ca.EndColumn = cells.MaxDataColumn;
//Set cells area for find options
opts.SetRange(ca);
opts.LookAtType = LookAtType.Contains; //use as per your needs
opts.LookInType = LookInType.Values; //change if you want to search in formulas
foundCell = worksheet.Cells.Find(stringToFind, prevcell, opts);
if (foundCell != null)
{
Empindex = foundCell.Column;
}
DataSorter dataSorter = workbook.DataSorter;
dataSorter.Key1 = Empindex;
dataSorter.Order1 = Aspose.Cells.SortOrder.Ascending;
//Similarly you may find out other titles and get the column indexes accordingly.
CellArea area = new CellArea();
area.StartRow = foundCell.Row+1;
area.EndRow = cells.MaxDataRow;
area.StartColumn = Empindex;
area.EndColumn = cells.MaxDataColumn;
dataSorter.Sort(workbook.Worksheets[0].Cells, area);
workbook.Save(“e:\test2\out1sort1_new.xlsx”);
Hope, this helps a bit.
Thank you.