在刷新AutoFilter后获取所有隐藏行索引

可能的使用场景

当在工作表单元上应用自动筛选时,一些行会自动隐藏。但可能存在一种情况,有些行已由Excel最终用户手动隐藏,而不是由自动筛选进行隐藏。因此,很难知道哪些行是由自动筛选隐藏的,哪些是由Excel最终用户手动隐藏的。Aspose.Cells使用AutoFilter.refresh(bool hideRows)方法解决了这个问题。此方法返回由自动筛选隐藏而不是由Excel最终用户手动隐藏的所有行的行索引。

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

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

示例代码

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Load the sample Excel file
Workbook wb = new Workbook(srcDir + "sampleGetAllHiddenRowsIndicesAfterRefreshingAutoFilter.xlsx");
//Access first worksheet
Worksheet ws = wb.getWorksheets().get(0);
//Apply autofilter
ws.getAutoFilter().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.getAutoFilter().refresh(true);
System.out.println("Printing Rows Indices, Cell Names and Values Hidden By AutoFilter.");
System.out.println("--------------------------");
for(int i=0; i<rowIndices.length; i++)
{
int r = rowIndices[i];
Cell cell = ws.getCells().get(r, 0);
System.out.println(r + "\t" + cell.getName() + "\t" + cell.getStringValue());
}

控制台输出

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