Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Tue Oct 01, 2019 1:23 pm

Hi,

I have trouble sorting data in an Excel file.
My file has multiple columns and I want to sort the data based on one column.

I use code that can be found here :
https://www.e-iceblue.com/Tutorials/Spi ... -data.html

So far i have tried :
Code: Select all
workbook.DataSorter.Sort(worksheet["A:A"]);
or
Code: Select all
workbook.DataSorter.Sort(worksheet.Columns[0]);

But i keep having a corrupted output file.

With this line fisrt column is sorted but the sort is not extended to the other coumns :
Code: Select all
workbook.DataSorter.Sort(worksheet["A1:A6"]);


Can you please provide me code to sort all the data based on one column ?

Thnak for your advice.

Edit : With this code it seems that can sort all the columns but the first row is not sorted :
Code: Select all
workbook.DataSorter.Sort(worksheet.Range[1, 1, worksheet.Rows.Count(), worksheet.Columns.Count()]);

Is it the right way to do it ? How can i sort the first row also ?

LisiAero
 
Posts: 11
Joined: Tue Nov 13, 2018 12:04 pm

Wed Oct 02, 2019 2:28 am

Hi,

Thanks for your inquiry.
I used the following code to sort the first row, but got the "ArgumentOutOfRangeException" exception when sorting.The issue has been posted to our dev team for investigating and fixing. If it is fixed or there is any update, I will infrom you. Sorry for the inconvenience caused.
Code: Select all
workbook.DataSorter.SortLeftToRight = true;
workbook.DataSorter.Sort(sheet.Rows[0]);

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1187
Joined: Tue Sep 27, 2016 1:06 am

Wed Oct 02, 2019 7:39 am

Hi thanks for you answer.

It seems like the property SortLeftToRight is not implemented in the 7.12 version. On the project i am working i won't be able to update the dll anytime soon.

I will use this code afteradding a header in the file :
Code: Select all
workbook.DataSorter.Sort(worksheet.Range[1, 1, worksheet.Rows.Count(), worksheet.Columns.Count()]);

LisiAero
 
Posts: 11
Joined: Tue Nov 13, 2018 12:04 pm

Wed Oct 02, 2019 8:04 am

Hi,

Thanks for your feedback.
The 7.12 version is too old, now the latest version is Spire.Office Platinum (DLL Only) Version:4.9.2. Anyway, our Dev team will investigate the reported issue. I will keep you informed regarding any available update.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1187
Joined: Tue Sep 27, 2016 1:06 am

Wed Oct 09, 2019 2:28 am

Hi,

After further investigation, we found the following code could achieve your need to sort the first row. And please download the latest version Spire.Office Platinum (DLL Only) Version:4.9.2 to have a try, if there is still any issue, please provide your testing Excel file as well as your desired output for further investigation.
Code: Select all
 Workbook wb = new Workbook();
 wb.LoadFromFile("sample.xlsx");
 Worksheet sheet = wb.Worksheets[0];
 //Sort the first row
 wb.DataSorter.IsIncludeTitle = false;
 wb.DataSorter.Orientation = SortOrientationType.LeftToRight;
 wb.DataSorter.SortColumns.Add(0, SortComparsionType.Values, OrderBy.Ascending);
 wb.DataSorter.Sort(sheet.Rows[0]);
 //Sort the first column
 wb.DataSorter.Orientation = SortOrientationType.TopToBottom;
 wb.DataSorter.Sort(sheet.Columns[0]);
 wb.SaveToFile("result.xlsx", ExcelVersion.Version2013);

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1187
Joined: Tue Sep 27, 2016 1:06 am

Fri Oct 18, 2019 9:18 am

Hi,

Greetings from E-iceblue!
Did we resolve your issue?
Any feedback will be greatly appreciated.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1187
Joined: Tue Sep 27, 2016 1:06 am

Return to Spire.XLS