且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何以编程方式创建Excel“排序/筛选组合框"?

更新时间:2023-02-12 17:27:06

筛选器是通过Excel表中的范围或Excel界面上的筛选器"按钮本地创建的.正如Joshua所说,您可以尝试在VBA中编写脚本,但是从您的问题标签看来,您正在使用C#和Excel-Interop(??)

Filters are created natively by the range being in an Excel table, or maybe by Filter button on Excel interface. As Joshua said you can try and script this in VBA, but it seems by your question tags that you are using C# and Excel-Interop(??)

尝试在C#代码中使用范围对象句柄并应用.Autofilter方法.

Try using the range object handle in your C# code and applying .Autofilter method.

object result = (object)oRange.AutoFilter(1, System.Reflection.Missing.Value,ExApp.XlAutoFilterOperator.xlAnd,System.Reflection.Missing.Value, true);

ExApp上方是Interop.Excel命名空间的别名.

where above ExApp is my alias for the Interop.Excel namespace.

关于开发人员"标签,请在Google上为您的Excel版本查看自定义功能区"选项.右键单击功能区,或者File-> Options--etc

As for Developer tab, look at the Customize Ribbon option on google for your Excel version. Rightclick ribbon maybe, or File-->Options--etc

更新

好吧,要修改数据透视字段的排序和过滤器属性,您需要抓住数据透视字段的句柄,例如,如果月"过滤器位于单元格D5中,则可以执行以下操作:

Ok so to amend the sort and filter properties of a pivotfield, you need to grab the handle to the pivotField, e.g if your 'Month' Filter was in cell D5, you could do something like:

Range oRange = oSheet.get_Range("D5", "D5");
PivotField pf = oRange.PivotField;
pf.AutoSort((int)XlSortOrder.xlDescending, "Month");   //this sorts in reverse order            
pf.PivotItems(2).Visible = false;   //this makes the second item deselected in filter