刷新自动筛选后获取所有隐藏行索引

可能的使用场景

当您在工作表单元格上应用自动过滤器时,某些行会自动隐藏。但可能某些行已被 Excel 最终用户手动隐藏,并且未被自动筛选器隐藏。因此,很难知道哪些行被自动筛选器隐藏,哪些行被 Excel 最终用户手动隐藏。 Aspose.Cells 使用 int[] 处理这个问题AutoFilter.Refresh(bool hideRows)方法。此方法返回自动过滤器隐藏的所有行的行索引,而不是 Excel 最终用户手动隐藏的行索引。

刷新自动筛选后获取所有隐藏行索引

请参阅以下加载的示例代码示例 Excel 文件其中包含一些由 Excel 最终用户手动隐藏的行。该代码应用自动过滤器并使用 int[] 刷新它AutoFilter.Refresh(bool hideRows)方法返回自动筛选器隐藏的所有行的行索引。然后它在控制台上打印隐藏行的索引以及单元格名称和值。

示例代码

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
//Load the sample Excel file
Workbook wb = new Workbook(sourceDir + "sampleGetAllHiddenRowsIndicesAfterRefreshingAutoFilter.xlsx");
//Access first worksheet
Worksheet ws = wb.Worksheets[0];
//Apply autofilter
ws.AutoFilter.AddFilter(0, "Orange");
//True means, it will refresh autofilter and return hidden rows.
//False means, it will not refresh autofilter but return same hidden rows.
int[] rowIndices = ws.AutoFilter.Refresh(true);
Console.WriteLine("Printing Rows Indices, Cell Names and Values Hidden By AutoFilter.");
Console.WriteLine("--------------------------");
for (int i = 0; i < rowIndices.Length; i++)
{
int r = rowIndices[i];
Cell cell = ws.Cells[r, 0];
Console.WriteLine(r + "\t" + cell.Name + "\t" + cell.StringValue);
}

控制台输出

Printing Rows Indices, Cell Names and Values Hidden By AutoFilter.

\--------------------------

1       A2      Apple

2       A3      Apple

3       A4      Apple

6       A7      Apple

7       A8      Apple

11      A12     Pear

12      A13     Pear