在刷新AutoFilter后获取所有隐藏行索引 可能的使用场景
当您在工作表单元格上应用自动筛选时,一些行会自动隐藏。但是可能会出现这种情况,一些行已经被Excel终端用户手动隐藏,它们不是由自动筛选隐藏的。这使得难以知道哪些行是由自动筛选隐藏的,哪些是由Excel终端用户手动隐藏的。Aspose.Cells使用int[] AutoFilter.Refresh(bool hideRows) 方法来解决这个问题。该方法返回所有由自动筛选隐藏而不是由Excel终端用户手动隐藏的行的行索引。
在刷新自动筛选后获取所有隐藏行索引
请参阅以下示例代码,加载包含一些由Excel终端用户手动隐藏的行的示例Excel文件 。代码应用自动筛选并使用int[] AutoFilter.Refresh(bool hideRows) 方法来刷新它,该方法返回由自动筛选隐藏的所有行的行索引。然后将隐藏行的索引与单元格名称和值一起打印在控制台上。
示例代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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);
}
控制台输出
Copy 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