This project has moved and is read-only. For the latest updates, please go here.

Added sortcondition to filter, but filter is not active

Feb 23, 2017 at 5:15 PM

I wanted to sort my output using the autofilter in excel. I could not find the way to do so in the example files, so I took a look at the XML code of an excel file I saved after applying the sort method to it.

I noticed this in the XML:
<autoFilter ref="A2:L2">
    <sortState ref="A3:L597">
        <sortCondition ref="E2"/>
so I added it to C# the code like this:
var filterDef = new XElement("sortState");
filterDef.Add(new XAttribute("ref", "A3:L597"));
filterDef.Add(new XElement("sortCondition", new XAttribute("ref", "E2")));
autoFilterNode.InnerXml += filterDef.ToString();

build the excel file, using the C# code. When I opened the excel file, the sort seems to be applied when you see the autofilter drop down in the specific column. But the sorting was actually not done.

I checked the XML code of the code-written excel sheet and the autoFilter element attributes and sub elements are exactly the same.

Generated XML code via C#
<autoFilter ref="A2:L2">
    <sortState ref="A3:L597">
        <sortCondition ref="E2" />
I also did a ws.Calculate() and _package.Workbook.Calculate() prior to saving the file.
What do I need to do to get the sorting right?
Kind regards,