question

JimWilson-3906 avatar image
0 Votes"
JimWilson-3906 asked JimWilson-3906 answered

Invoking a sort using OpenXML

I have been producing excel files with open XML and I know how to set up the sort criteria. What I don't understand is how to invoke the actual sort. Any help will be appreciated.

dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@JimWilson-3906
Welcome to Q&A forum!
Since the tag office-excel-itpro focuses on general issues about Excel client, I will remove it.
And maybe you could add openspecs-office-fileformats tag for more reated help.
Thanks for your understanding!

0 Votes 0 ·
TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered

Could you please show some of your current code and let us move on based on your code?

I used ClosedXml to write some code, this library is a layer of encapsulation of OpenXML API, see if this is suitable for you.

         static void Main(string[] args)
         {
             DataTable dataTable = GetDataTable();
             ExportData();
         }
         static DataTable GetDataTable()
         {
             DataTable dataTable = new DataTable();
             dataTable.Columns.Add("ID", typeof(int));
             dataTable.Columns.Add("Name", typeof(string));
             dataTable.Columns.Add("Math", typeof(double));
             dataTable.Columns.Add("Science", typeof(double));
             dataTable.Columns.Add("History", typeof(double));
    
             dataTable.Rows.Add(1, "Timon", 75.2, 98, 74);
             dataTable.Rows.Add(2, "Tom", 85, 83, 54);
             dataTable.Rows.Add(3, "Jerry", 35.4, 77, 25);
             return dataTable;
         }
         private static void ExportData()
         {
             var wb = new XLWorkbook();
             DataTable dt = GetDataTable();
    
             var wsTable = wb.Worksheets.Add(dt,"Table");
             var rangeTable = wsTable.RangeUsed();
             var table = rangeTable.CopyTo(wsTable.Column(wsTable.LastColumnUsed().ColumnNumber() + 3)).CreateTable();
             table.Sort("History Asc");
    
             wb.SaveAs(@"C:\xxx\SortExamples1.xlsx");
         }

If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JimWilson-3906 avatar image
0 Votes"
JimWilson-3906 answered

@openspecs-office-fileformats

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.