简体   繁体   中英

In ClosedXML, how to sort a worksheet by a column?

In C# using asp/MVC the app generates an Excel .xlsx file based on data thats filled in to the specific columns, works great.

But the goal is to provide additional worksheets that use the same columns but sort on specific colums, such as Column "J"

  var wb = new XLWorkbook();
  var ws = wb.Worksheets.Add("Proj Info");
  var ws2 = wb.Worksheets.Add("Sort By Dates");

The worksheet ws has values filled in by variables or formulas, the data is correct, but cannot make it sort on a column

  ws.AutoFilter.Column("J");   //no, nothing changes
  ws.Column("J").Sort(); -> this shifts all the columns up but does not sort
  ws.Column("J").Sort(XLSortOrder.Ascending);  ->same, doesnt sort only shifts

Update: ws.Sort(9); worked in sorting, but the problem is that Column 10 has a Formula, and I need to sort on that Column.

   ws.Cell("J" + c).FormulaR1C1 = "=C$2-F" + c;

With this? it WILL NOT SORT. The ws.Sort(10); works when the cell contains a final value, but when its got the Formula? Is there any workaround to force the Excel page to sort after its implemented the formula's in each cell?

You are not sorting the table, but the values in column J.

Try this:

ws.Sort("Column10");

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM