如何以及在何处使用迭代器

如何使用迭代器

Cells 迭代器

有多种方法可以访问单元格的迭代器,可以根据应用程序要求使用这些方法中的任何一种。以下是返回单元格迭代器的方法。

  1. Cells.iterator
  2. 行.迭代器
  3. 范围.迭代器

上述所有方法都返回允许遍历已初始化单元格集合的迭代器。

以下代码示例演示了单元格集合的 Iterator 类的实现。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
String dataDir = Utils.getDataDir(CellsIterator.class);
// Load a file in an instance of Workbook
Workbook book = new Workbook(dataDir + "sample.xlsx");
// Get the iterator from Cells collection
Iterator cellIterator = book.getWorksheets().get(0).getCells().iterator();
// Traverse cells in the collection
while (cellIterator.hasNext()) {
Cell cell = (Cell) cellIterator.next();
;
System.out.println(cell.getName() + " " + cell.getValue());
}
// Get iterator from an object of Row
Iterator rowIterator = book.getWorksheets().get(0).getCells().getRows().get(0).iterator();
// Traverse cells in the given row
while (rowIterator.hasNext()) {
Cell cell = (Cell) rowIterator.next();
System.out.println(cell.getName() + " " + cell.getValue());
}
// Get iterator from an object of Range
Iterator rangeIterator = book.getWorksheets().get(0).getCells().createRange("A1:B10").iterator();
// Traverse cells in the range
while (rangeIterator.hasNext()) {
Cell cell = (Cell) rangeIterator.next();
System.out.println(cell.getName() + " " + cell.getValue());
}
行迭代器

可以在使用 RowCollection.iterator 方法时访问行迭代器。下面的代码示例演示了 Iterator for RowCollection 类的实现。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
String dataDir = Utils.getDataDir(RowsIterator.class);
// Load a file in an instance of Workbook
Workbook book = new Workbook(dataDir + "sample.xlsx");
// Get the iterator for RowCollection
Iterator rowsIterator = book.getWorksheets().get(0).getCells().getRows().iterator();
// Traverse rows in the collection
while (rowsIterator.hasNext()) {
Row row = (Row) rowsIterator.next();
System.out.println(row.getIndex());
}
列迭代器

可以在使用 ColumnCollection.iterator 方法时访问 Columns Iterator。下面的代码示例演示了 ColumnCollection 类的 Iterator 的实现。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
String dataDir = Utils.getDataDir(ColumnsIterator.class);
// Load a file in an instance of Workbook
Workbook book = new Workbook(dataDir + "sample.xlsx");
// Get the iterator for ColumnsCollection
Iterator colsIterator = book.getWorksheets().get(0).getCells().getColumns().iterator();
// Traverse columns in the collection
while (colsIterator.hasNext()) {
Column col = (Column) colsIterator.next();
System.out.println(col.getIndex());
}

在哪里使用迭代器

为了讨论使用迭代器的优点,让我们举一个实时的例子。

设想

一个应用程序需求是遍历给定工作表中的所有单元格以读取它们的值。可以有几种方法来实现这个目标。下面展示了一些。

使用显示范围
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
String dataDir = Utils.getDataDir(UsingDisplayRange.class);
// Load a file in an instance of Workbook
Workbook book = new Workbook(dataDir + "sample.xlsx");
// Get Cells collection of first worksheet
Cells cells = book.getWorksheets().get(0).getCells();
// Get the MaxDisplayRange
Range displayRange = cells.getMaxDisplayRange();
// Loop over all cells in the MaxDisplayRange
for (int row = displayRange.getFirstRow(); row < displayRange.getRowCount(); row++) {
for (int col = displayRange.getFirstColumn(); col < displayRange.getColumnCount(); col++) {
// Read the Cell value
System.out.println(displayRange.get(row, col).getStringValue());
}
}
使用 MaxDataRow 和 MaxDataColumn
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
String dataDir = Utils.getDataDir(UsingMaxDataRowAndMaxDataColumn.class);
// Load a file in an instance of Workbook
Workbook book = new Workbook(dataDir + "sample.xlsx");
// Get Cells collection of first worksheet
Cells cells = book.getWorksheets().get(0).getCells();
// Loop over all cells
for (int row = 0; row < cells.getMaxDataRow(); row++) {
for (int col = 0; col < cells.getMaxDataColumn(); col++) {
// Read the Cell value
System.out.println(cells.get(row, col).getStringValue());
}
}

正如您所观察到的,上述两种方法都使用或多或少相似的逻辑,即;遍历集合中的所有单元格以读取单元格值。由于如下所述的多种原因,这可能会产生问题。

  1. MaxRow、MaxDataRow、MaxColumn、MaxDataColumn 和 MaxDisplayRange 等 API 需要额外的时间来收集相应的统计信息。如果数据矩阵(行 x 列)很大,使用这些 API 可能会造成性能损失。
  2. 在大多数情况下,并非给定范围内的所有单元格都被实例化。在这种情况下,与仅检查初始化单元格相比,检查矩阵中的每个单元格效率不高。
  3. 以 Cells.get(rowIndex, columnIndex) 的方式在循环中访问单元格会导致一个范围内的所有单元格对象都被实例化,最终可能会导致 OutOfMemoryError。
结论

基于上述事实,以下是应该使用迭代器的可能场景。

  1. 需要对单元格集合进行只读访问,即;要求是只检查细胞。
  2. 要遍历大量单元格。
  3. 只遍历初始化的单元格/行/列。