イテレータの使用方法と場所

反復子の使用方法

セル反復子

セルの反復子にアクセスする方法はさまざまであり、アプリケーションの要件に基づいてこれらの方法のいずれかを使用することができます。 以下に、セルの反復子を返すメソッドが示されています。

  1. Cells.iterator
  2. Row.iterator
  3. Range.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 メソッドを使用すると、行の反復子にアクセスできます。 以下のコード例は、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 メソッドを使用すると、列の反復子にアクセスできます。 以下のコード例は、ColumnCollection クラスの反復子の実装を示しています。

// 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());
}
}

上記の2つのアプローチともに、コレクション内のすべてのセルをループしてセルの値を読み取るというロジックを使用していることがわかります。 以下で説明する理由により、これはいくつかの理由で問題があるかもしれません。

  1. MaxRow、MaxDataRow、MaxColumn、MaxDataColumn、および MaxDisplayRange などの API は、対応する統計情報を収集するための余分な時間を要求します。 データマトリックス(行×列)が大きい場合、これらの API を使用するとパフォーマンスにペナルティが課せられる可能性があります。
  2. ほとんどの場合、指定された範囲内のすべてのセルがインスタンス化されていません。そのような状況では、行列内のすべてのセルを確認することは、初期化されたセルのみを確認する場合と比べて効率的ではありません。
  3. Cells.get(rowIndex, columnIndex) をループ内でアクセスすると、範囲内のすべてのセルオブジェクトが初期化されるため、最終的に OutOfMemoryError が発生する可能性があります。
結論

上記の事実に基づいて、反復子を使用すべき可能性のあるシナリオは次のとおりです。

  1. セルコレクションの読み取り専用アクセスが必要な場合、つまり、セルを検査するだけの要求がある場合。
  2. 多数のセルを走査する必要がある場合。
  3. 初期化されたセル/行/列のみを走査する必要がある場合。