Free Support Forum - aspose.com

DataSorter Aspose.cells

How do you define a datasorter in visual basic .net and how do you use it. I need to sort some info in a worksheet.

This is my code but I don't know hao to define a new datasorter.

Dim sorter As DataSorter

sorter.Order1 = Aspose.Cells.SortOrder.Ascending

sorter.Key1 = 1

Hi,

Here is a sample code for your reference:

[VB]
'Instantiate a new Workbook object.
Dim workbook As Workbook = New Workbook
'Load a template file.
workbook.Open("d:\test\Book1.xls")
'Get the workbook datasorter object.
Dim sorter As DataSorter = workbook.DataSorter
'Set the first order for datasorter object
sorter.Order1 = Aspose.Cells.SortOrder.Descending
'Define the first key.
sorter.Key1 = 0
'Set the second order for datasorter object.
sorter.Order2 = Aspose.Cells.SortOrder.Ascending
'Define the second key.
sorter.Key2 = 1
'Create a cells area (range).
Dim ca As CellArea = New CellArea
'Specify the start row index.
ca.StartRow = 0
'Specify the start column index.
ca.StartColumn = 0
'Specify the last row index.
ca.EndRow = 13
'Specify the last column index.
ca.EndColumn = 1
'Sort the data in the specified data range (A1:B14)
sorter.Sort(workbook.Worksheets(0).Cells, ca)
'Save the excel file.
workbook.Save("d:\test\sortdata.xls")


[C#]
//Instantiate a new Workbook object.
Workbook workbook = new Workbook();
//Load a template file.
workbook.Open("d:\\test\\Book1.xls");
//Get the workbook datasorter object.
DataSorter sorter = workbook.DataSorter;
//Set the first order for datasorter object.
sorter.Order1 = Aspose.Cells.SortOrder.Descending;
//Define the first key.
sorter.Key1 = 0;
//Set the second order for datasorter object.
sorter.Order2 = Aspose.Cells.SortOrder.Ascending;
//Define the second key.
sorter.Key2 = 1;
//Create a cells area (range).
CellArea ca = new CellArea();
//Specify the start row index.
ca.StartRow = 0;
//Specify the start column index.
ca.StartColumn = 0;
//Specify the last row index.
ca.EndRow = 13;
//Specify the last column index.
ca.EndColumn = 1;
//Sort data in the specified data range (A1:B14)
sorter.Sort(workbook.Worksheets[0].Cells, ca);
//Save the excel file.
workbook.Save("d:\\test\\sortdata.xls");

Thank you.

Than you for the help. It was vwry useful.