Is there a way to read the sort criteria from an Excel file using C#??

as an example, if you sorted a cell range by country, is there a way to read "country" using C#?


Sure there's way :)

Many - have a search on using excel from c# and reading values.. and controlling excel - theres a few ways you could go about it.

thank you for replies, but i made some search but i guess i missed the right direction on it, could anyone of you please point me to the right direction??

ok, it seems that noone knows the answer ,just guesses, i made lots of searches and after that i posted this questions.

Thank you all for your replies.

Youve shown us no effort, why should we do it for you?

Liz, i didnt ask you to do it for me, i was seeking help in answering this question not a help with how to do a search!!!!!!! i was a big contributor in MS forums, and i know how forums work and how to make searches.

anyway, just answering your guesses, you cant get the sort information stored in the dialog as its perserved by excel application itself, so all of you said "Yah and many" specially you Liz without even knowing if its possible or not. and RamyMahrous posted a google search link with "C# work with Excel" keywords!!!

at least post keywords like "C# +excel +get +sort +data". with "+" sign telling the google engine to include all of these keywords in its search.

anyhow, i wouldnt post a question on forums without having an intensive search prior that and would never guess the answer without knowing it.

im just posting the answer to let anyone who has the same question to know that its not possible and the workaround is to do the following:

1. Get the last sort by calling Worksheet.Sort.Rng (do remember to check
Worksheet.Sort.SortFields.Count if its > 0 prior that call, Worksheet.Sort will return the last sort done by the end user).

2. loop over the data and check if its in the correct order as you expect it or not.

Actually there are enormous amounts of excel interactions listed, you have many options wether you read the data to c# and put it back, or just ask Excel to do the work for you. What you havent done is actually plan what you want. There are examples of all of this which is why I tell you to search.

Oh and there you go, you found an answer.. so, I guess you managed to work it out by readint anyway.

It surprises me that anyone feels they can program, when they cant do research. Searching on google should be fairly intuative by now, if you didnt think to search for c# excel data sort.. such as the keywords listed, it just doesnt look like people try.. when you can pretty much copy and paste the question into google and get the answer.

I am so sorry RonnySaid, if I annoyed you; please accept my apologies.

RamyMahrous and Liz, im not mad, thank you for your replies. i do appreciate any kind of reply whatever it is as you guys had the time to read my question and thank you for that :)

Liz, i got the answer from the Bytes forum, the approach i took was already done before even posting the question, but i just wanted to know if there is an easier way to do it or if excel supports such kind of interaction, it appears to be that xlDialogSort preserve these data to itself according to the confirmation i got from the bytes forum.

you should start learning using Microsoft.Office.Interop.Excel library. add it from COM reference. I give you here a simple example. I just import a string in Excel in cell (1,1). If you do this, try to learn more using auto-complete and surfing the web.

//the library
using Microsoft.Office.Interop.Excel;

Microsoft.Office.Interop.Excel.ApplicationClass exApp = new ApplicationClass();
        Microsoft.Office.Interop.Excel.Workbook objBook;
        Microsoft.Office.Interop.Excel.Worksheet objSheet;

 object missing = System.Reflection.Missing.Value;

                exApp.Visible = true;
                objBook = exApp.Workbooks.Add(missing);

                objSheet = (Microsoft.Office.Interop.Excel.Worksheet)objBook.Sheets["Sheet1"];
 objSheet.Name = "myTable";

objSheet.get_Range("A1", "B1").EntireColumn.AutoFit();//my text is long so i AutoFit the columns.    
// in case of exception, we show it
 catch (Exception ex)

Hope this helps you get experience with Excel :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.