I've found various related code on the Internet but nothing seems to work. Maybe someone can point out my error:
// Get a Range of the data to sort
// (I also tried to use _worksheet.UsedRange, but it fails - why?)
var dataToSort = _worksheet.Range["A2", $"Z{lastLine}"];
// Sort range
dataToSort.Sort(_worksheet.Range["A2"]);
Funny enough it resorts the columns instead of the rows...
Actually I want to write something like this:
_worksheet.Sort(columnIndex: 1);
But I can't find a suitable overload.
My Sheet looks like this:
1 | | |
2 | Z | Data2 |
3 | A | Data1 |
I want it to look like this afterwards:
1 | | |
2 | A | Data1 |
3 | Z | Data2 |
(Using Excel 2019 and Excel Interop 15 for an xlsx file)
Ouch, for whatever reason the sort orientation default is xlSortRows
. I had to switch to xlSortColumns
and now it works with this simple oneliner:
_worksheet.UsedRange.Sort(_worksheet.Columns[1], Orientation: XlSortOrientation.xlSortColumns);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.