Interface ISortFields
Represents the sort field collections based on which sorting is performed.
Namespace: Syncfusion.XlsIO
Assembly: Syncfusion.XlsIO.Portable.dll
Syntax
public interface ISortFields : IEnumerable
Properties
Count
Represents the field count.
Declaration
int Count { get; }
Property Value
Type |
---|
System.Int32 |
Examples
To get the number of ISortField in the ISortFields collection Count property can be used.
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Create worksheet
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];
//Load data
worksheet["A1"].Text = "Products";
worksheet["A2"].Text = "Alfreds Futterkiste";
worksheet["A3"].Text = "Antonio Moreno Taqueria";
worksheet["A4"].Text = "Around the Horn";
worksheet["A5"].Text = "Bon app";
worksheet["A6"].Text = "Eastern Connection";
worksheet["A7"].Text = "Ernst Handel";
worksheet["B1"].Text = "Qtr1";
worksheet["B2"].Number = 744.6;
worksheet["B3"].Number = 5079.6;
worksheet["B4"].Number = 1267.5;
worksheet["B5"].Number = 1418;
worksheet["B6"].Number = 4728;
worksheet["B7"].Number = 943.89;
worksheet["C1"].Text = "Qtr2";
worksheet["C2"].Number = 162.56;
worksheet["C3"].Number = 1249.2;
worksheet["C4"].Number = 1062.5;
worksheet["C5"].Number = 756;
worksheet["C6"].Number = 4547.92;
worksheet["C7"].Number = 349.6;
//Create style for table number format
IStyle style1 = workbook.Styles.Add("CurrencyFormat");
style1.NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* \" - \"??_);_(@_)";
//Apply number format
worksheet["B2:C7"].CellStyleName = "CurrencyFormat";
//Set colors
worksheet["C2:C3"].CellStyle.Color = System.Drawing.Color.Red;
worksheet["C4:C5"].CellStyle.Color = System.Drawing.Color.Blue;
worksheet["C6:C7"].CellStyle.Color = System.Drawing.Color.Green;
//Create sorter
IDataSort sorter = workbook.CreateDataSorter();
//Set range
sorter.SortRange = worksheet["A1:C7"];
//Add sortfield
ISortField sortField = sorter.SortFields.Add(1, SortOn.Values, OrderBy.Ascending);
ISortField sortField2 = sorter.SortFields.Add(2, SortOn.CellColor, OrderBy.Descending);
//Perform sort
sorter.Sort();
//Check sortfield count
Console.WriteLine(sorter.SortFields.Count);
//Apply AutoFit
worksheet.UsedRange.AutofitColumns();
//Save and Dispose
workbook.SaveAs("Datasort.xlsx");
workbook.Close();
Console.ReadKey();
}
//Output will be
//2
Item[Int32]
Returns single ISortField item from the ISortFields collection.
Declaration
ISortField this[int index] { get; }
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | index |
Property Value
Type | Description |
---|---|
ISortField | ISortField with the given Key. |
Examples
The following code illustrates how ISortField can be accessed from ISortFields collection.
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Create worksheet
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];
//Load data
worksheet["A1"].Text = "Products";
worksheet["A2"].Text = "Alfreds Futterkiste";
worksheet["A3"].Text = "Antonio Moreno Taqueria";
worksheet["A4"].Text = "Around the Horn";
worksheet["A5"].Text = "Bon app";
worksheet["A6"].Text = "Eastern Connection";
worksheet["A7"].Text = "Ernst Handel";
worksheet["B1"].Text = "Qtr1";
worksheet["B2"].Number = 744.6;
worksheet["B3"].Number = 5079.6;
worksheet["B4"].Number = 1267.5;
worksheet["B5"].Number = 1418;
worksheet["B6"].Number = 4728;
worksheet["B7"].Number = 943.89;
worksheet["C1"].Text = "Qtr2";
worksheet["C2"].Number = 162.56;
worksheet["C3"].Number = 1249.2;
worksheet["C4"].Number = 1062.5;
worksheet["C5"].Number = 756;
worksheet["C6"].Number = 4547.92;
worksheet["C7"].Number = 349.6;
//Create style for table number format
IStyle style1 = workbook.Styles.Add("CurrencyFormat");
style1.NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* \" - \"??_);_(@_)";
//Apply number format
worksheet["B2:C8"].CellStyleName = "CurrencyFormat";
//Set colors
worksheet["C2:C3"].CellStyle.Color = System.Drawing.Color.Red;
worksheet["C4:C5"].CellStyle.Color = System.Drawing.Color.Blue;
worksheet["C6:C7"].CellStyle.Color = System.Drawing.Color.Green;
//Create sorter
IDataSort sorter = workbook.CreateDataSorter();
//Set range
sorter.SortRange = worksheet["A1:C7"];
//Add sortfield
ISortField sortField = sorter.SortFields.Add(1, SortOn.Values, OrderBy.Ascending);
ISortField sortField2 = sorter.SortFields.Add(2, SortOn.CellColor, OrderBy.Descending);
//Set sortfiled color
sorter.SortFields[1].Color = System.Drawing.Color.Green;
//Set priority
sorter.SortFields[1].SetPriority(0);
//Perform sort
sorter.Sort();
//Apply AutoFit
worksheet.UsedRange.AutofitColumns();
//Save and Dispose
workbook.SaveAs("Datasort.xlsx");
workbook.Close();
}
Methods
Add(Int32, SortOn, OrderBy)
Adds the SortField in the collection.
Declaration
ISortField Add(int key, SortOn sortBasedOn, OrderBy orderBy)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | key | Column Index to sort the data. |
SortOn | sortBasedOn | To sort the data based on. |
OrderBy | orderBy | To order the sorted data. |
Returns
Type | Description |
---|---|
ISortField | Returns the Added ISortField. |
Examples
The Add method adds ISortField to the ISortFields collection and returns it. The following code illustrates how ISortField can be added to ISortFields collection using Add method.
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Create worksheet
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];
//Load data
worksheet["A1"].Text = "Products";
worksheet["A2"].Text = "Alfreds Futterkiste";
worksheet["A3"].Text = "Antonio Moreno Taqueria";
worksheet["A4"].Text = "Around the Horn";
worksheet["A5"].Text = "Bon app";
worksheet["A6"].Text = "Eastern Connection";
worksheet["A7"].Text = "Ernst Handel";
worksheet["B1"].Text = "Qtr1";
worksheet["B2"].Number = 744.6;
worksheet["B3"].Number = 5079.6;
worksheet["B4"].Number = 1267.5;
worksheet["B5"].Number = 1418;
worksheet["B6"].Number = 4728;
worksheet["B7"].Number = 943.89;
worksheet["C1"].Text = "Qtr2";
worksheet["C2"].Number = 162.56;
worksheet["C3"].Number = 1249.2;
worksheet["C4"].Number = 1062.5;
worksheet["C5"].Number = 756;
worksheet["C6"].Number = 4547.92;
worksheet["C7"].Number = 349.6;
//Create style for table number format
IStyle style1 = workbook.Styles.Add("CurrencyFormat");
style1.NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* \" - \"??_);_(@_)";
//Apply number format
worksheet["B2:C8"].CellStyleName = "CurrencyFormat";
//Create sorter
IDataSort sorter = workbook.CreateDataSorter();
//Set range
sorter.SortRange = worksheet["A1:C7"];
//Add sortfield
ISortField sortField = sorter.SortFields.Add(0, SortOn.Values, OrderBy.Ascending);
//Perform sort
sorter.Sort();
//Apply AutoFit
worksheet.UsedRange.AutofitColumns();
//Save and Dispose
workbook.SaveAs("Datasort.xlsx");
workbook.Close();
}
Remove(ISortField)
Removes the specified ISortField from the ISortFields collection.
Declaration
void Remove(ISortField sortField)
Parameters
Type | Name | Description |
---|---|---|
ISortField | sortField | Sort Field to remove from the collection. |
Examples
The following code illustrates how a ISortField can be removed from ISortFields collection by passing it as a parameter.
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Create worksheet
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];
//Load data
worksheet["A1"].Text = "Products";
worksheet["A2"].Text = "Alfreds Futterkiste";
worksheet["A3"].Text = "Antonio Moreno Taqueria";
worksheet["A4"].Text = "Around the Horn";
worksheet["A5"].Text = "Bon app";
worksheet["A6"].Text = "Eastern Connection";
worksheet["A7"].Text = "Ernst Handel";
worksheet["B1"].Text = "Qtr1";
worksheet["B2"].Number = 744.6;
worksheet["B3"].Number = 5079.6;
worksheet["B4"].Number = 1267.5;
worksheet["B5"].Number = 1418;
worksheet["B6"].Number = 4728;
worksheet["B7"].Number = 943.89;
worksheet["C1"].Text = "Qtr2";
worksheet["C2"].Number = 162.56;
worksheet["C3"].Number = 1249.2;
worksheet["C4"].Number = 1062.5;
worksheet["C5"].Number = 756;
worksheet["C6"].Number = 4547.92;
worksheet["C7"].Number = 349.6;
//Create style for table number format
IStyle style1 = workbook.Styles.Add("CurrencyFormat");
style1.NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* \" - \"??_);_(@_)";
//Apply number format
worksheet["B2:C8"].CellStyleName = "CurrencyFormat";
//Set colors
worksheet["C2:C3"].CellStyle.Color = System.Drawing.Color.Red;
worksheet["C4:C5"].CellStyle.Color = System.Drawing.Color.Blue;
worksheet["C6:C7"].CellStyle.Color = System.Drawing.Color.Green;
//Create sorter
IDataSort sorter = workbook.CreateDataSorter();
//Set range
sorter.SortRange = worksheet["A1:C7"];
//Add sortfield
ISortField sortField = sorter.SortFields.Add(1, SortOn.Values, OrderBy.Ascending);
ISortField sortField2 = sorter.SortFields.Add(2, SortOn.CellColor, OrderBy.Descending);
//Remove sortfield
sorter.SortFields.Remove(sortField2);
//Perform sort
sorter.Sort();
//Apply AutoFit
worksheet.UsedRange.AutofitColumns();
//Save and Dispose
workbook.SaveAs("Datasort.xlsx");
workbook.Close();
}
Remove(Int32)
Removes the ISortField in the ISortFields collection with the Key.
Declaration
void Remove(int key)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | key | Sort field Key to remove. |
Examples
The following code illustrates how a ISortField can be removed from ISortFields collection by specfiying it's Key.
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Create worksheet
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];
//Load data
worksheet["A1"].Text = "Products";
worksheet["A2"].Text = "Alfreds Futterkiste";
worksheet["A3"].Text = "Antonio Moreno Taqueria";
worksheet["A4"].Text = "Around the Horn";
worksheet["A5"].Text = "Bon app";
worksheet["A6"].Text = "Eastern Connection";
worksheet["A7"].Text = "Ernst Handel";
worksheet["B1"].Text = "Qtr1";
worksheet["B2"].Number = 744.6;
worksheet["B3"].Number = 5079.6;
worksheet["B4"].Number = 1267.5;
worksheet["B5"].Number = 1418;
worksheet["B6"].Number = 4728;
worksheet["B7"].Number = 943.89;
worksheet["C1"].Text = "Qtr2";
worksheet["C2"].Number = 162.56;
worksheet["C3"].Number = 1249.2;
worksheet["C4"].Number = 1062.5;
worksheet["C5"].Number = 756;
worksheet["C6"].Number = 4547.92;
worksheet["C7"].Number = 349.6;
//Create style for table number format
IStyle style1 = workbook.Styles.Add("CurrencyFormat");
style1.NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* \" - \"??_);_(@_)";
//Apply number format
worksheet["B2:C8"].CellStyleName = "CurrencyFormat";
//Set colors
worksheet["C2:C3"].CellStyle.Color = System.Drawing.Color.Red;
worksheet["C4:C5"].CellStyle.Color = System.Drawing.Color.Blue;
worksheet["C6:C7"].CellStyle.Color = System.Drawing.Color.Green;
//Create sorter
IDataSort sorter = workbook.CreateDataSorter();
//Set range
sorter.SortRange = worksheet["A1:C7"];
//Add sortfield
ISortField sortField = sorter.SortFields.Add(1, SortOn.Values, OrderBy.Ascending);
ISortField sortField2 = sorter.SortFields.Add(2, SortOn.CellColor, OrderBy.Descending);
//Remove sortfield
sorter.SortFields.Remove(2);
//Perform sort
sorter.Sort();
//Apply AutoFit
worksheet.UsedRange.AutofitColumns();
//Save and Dispose
workbook.SaveAs("Datasort.xlsx");
workbook.Close();
}