ListObject.AutoFilter 属性
获取一个筛选的表。
命名空间: Microsoft.Office.Tools.Excel
程序集: Microsoft.Office.Tools.Excel(在 Microsoft.Office.Tools.Excel.dll 中)
语法
声明
ReadOnly Property AutoFilter As AutoFilter
AutoFilter AutoFilter { get; }
属性值
类型:Microsoft.Office.Interop.Excel.AutoFilter
一个 AutoFilter,其中包含筛选的表。
示例
下面的代码示例向工作表中添加一个 ListObject,该控件对应于 Excel 中的一个表。 接着,该示例使用两行任意数据填充该表,并在表上指定一个筛选器,筛选出第一列的值不等于字符串值“bb”的所有行。 接下来,该示例显示在该表中找到的活动筛选器的数目。
此示例针对的是文档级自定义项。
Private Sub FilterListObject()
' Create ListObject control (table) and set table style
Dim employeeTable As Microsoft.Office.Tools.Excel.ListObject = _
Me.Controls.AddListObject(Me.Range("A1"), "employeeTable")
' Populate table with some data
Dim rng As Excel.Range
rng = employeeTable.InsertRowRange
rng(ColumnIndex:=1).Value2 = "bb"
rng(ColumnIndex:=2).Value2 = "b1"
Dim row2 As Excel.ListRow = employeeTable.ListRows.AddEx( _
AlwaysInsert:=True)
rng = row2.Range
rng(ColumnIndex:=1).Value2 = "aa"
rng(ColumnIndex:=2).Value2 = "a1"
' Set a filter
employeeTable.Range.AutoFilter(1, "bb")
Dim activeFilterCount As Integer = 0
For Each filter As Excel.Filter In employeeTable.AutoFilter.Filters
If filter.On = True Then
activeFilterCount += 1
End If
Next
MessageBox.Show("There are " + activeFilterCount.ToString() _
+ " active filter(s) for table " + employeeTable.Name + ".")
End Sub
private void FilterListObject()
{
// Create ListObject control (table) and set table style
Microsoft.Office.Tools.Excel.ListObject employeeTable =
this.Controls.AddListObject(this.Range["A1"],
"employeeTable");
// Populate table with some data
Excel.Range rng;
rng = employeeTable.InsertRowRange;
((Excel.Range)(rng[1])).Value2 = "bb";
((Excel.Range)(rng[2])).Value2 = "b1";
Excel.ListRow row2 = employeeTable.ListRows.AddEx(true);
rng = row2.Range;
((Excel.Range)(rng[1])).Value2 = "aa";
((Excel.Range)(rng[2])).Value2 = "a1";
// Set a filter
employeeTable.Range.AutoFilter(1, "bb",
Excel.XlAutoFilterOperator.xlFilterValues);
int activeFilterCount = 0;
foreach (Excel.Filter filter in employeeTable.AutoFilter.Filters)
{
if (filter.On == true)
{
activeFilterCount += 1;
}
}
MessageBox.Show("There are " + activeFilterCount.ToString()
+ " active filter(s) for table " + employeeTable.Name + ".");
}
.NET Framework 安全性
- 对直接调用方的完全信任。此成员不能由部分信任的代码使用。有关更多信息,请参见通过部分受信任的代码使用库。