How and Where to Use Enumerators

How to use Enumerators

Cells Enumerator

There are various ways to access the Cells Enumerator, and one can use any of these methods based on the application requirements. Here are the methods that return the cells enumerator.

  1. Cells.GetEnumerator
  2. Row.GetEnumerator
  3. Range.GetEnumerator

All of the above-mentioned methods return the enumerator that allows traversing the collection of cells which have been initialized.

The following code example demonstrates the implementation of the IEnumerator interface for a Cells collection.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Load a file in an instance of Workbook
var book = new Workbook(dataDir + "sample.xlsx");
// Get the enumerator from Cells collection
IEnumerator cellEnumerator = book.Worksheets[0].Cells.GetEnumerator();
// Traverse cells in the collection
while (cellEnumerator.MoveNext())
{
var cell = cellEnumerator.Current as Aspose.Cells.Cell;
Console.WriteLine(cell.Name + " " + cell.Value);
}
// Get enumerator from an object of Row
IEnumerator rowEnumerator = book.Worksheets[0].Cells.Rows[0].GetEnumerator();
// Traverse cells in the given row
while (rowEnumerator.MoveNext())
{
var cell = rowEnumerator.Current as Aspose.Cells.Cell;
Console.WriteLine(cell.Name + " " + cell.Value);
}
// Get enumerator from an object of Range
IEnumerator rangeEnumerator = book.Worksheets[0].Cells.CreateRange("A1:B10").GetEnumerator();
// Traverse cells in the range
while (rangeEnumerator.MoveNext())
{
var cell = rangeEnumerator.Current as Aspose.Cells.Cell;
Console.WriteLine(cell.Name + " " + cell.Value);
}

Rows Enumerator

The Rows Enumerator can be accessed while using the RowCollection.GetEnumerator method. The following code example demonstrates the implementation of the IEnumerator interface for RowCollection.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Load a file in an instance of Workbook
var book = new Workbook(dataDir + "sample.xlsx");
// Get the enumerator for RowCollection
IEnumerator rowsEnumerator = book.Worksheets[0].Cells.Rows.GetEnumerator();
// Traverse rows in the collection
while (rowsEnumerator.MoveNext())
{
var row = rowsEnumerator.Current as Aspose.Cells.Row;
Console.WriteLine(row.Index);
}

Columns Enumerator

The Columns Enumerator can be accessed while using the ColumnCollection.GetEnumerator method. The following code example demonstrates the implementation of the IEnumerator interface for ColumnCollection.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Load a file in an instance of Workbook
var book = new Workbook(dataDir + "sample.xlsx");
// Get the enumerator for ColumnsCollection
IEnumerator colsEnumerator = book.Worksheets[0].Cells.Columns.GetEnumerator();
// Traverse columns in the collection
while (colsEnumerator.MoveNext())
{
var col = colsEnumerator.Current as Aspose.Cells.Column;
Console.WriteLine(col.Index);
}

Where to use Enumerators

In order to discuss the advantages of using enumerators, let’s take a real time example.

Scenario

An application requirement is to traverse all cells in a given Worksheet to read their values. There could be several ways to implement this goal. A few are demonstrated below.

Using Display Range

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Load a file in an instance of Workbook
var book = new Workbook(dataDir + "sample.xlsx");
// Get Cells collection of first worksheet
var cells = book.Worksheets[0].Cells;
// Get the MaxDisplayRange
var displayRange = cells.MaxDisplayRange;
// Loop over all cells in the MaxDisplayRange
for (int row = displayRange.FirstRow; row < displayRange.RowCount; row++)
{
for (int col = displayRange.FirstColumn; col < displayRange.ColumnCount; col++)
{
// Read the Cell value
Console.WriteLine(displayRange[row, col].StringValue);
}
}

Using MaxDataRow & MaxDataColumn

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Load a file in an instance of Workbook
var book = new Workbook(dataDir + "sample.xlsx");
// Get Cells collection of first worksheet
var cells2 = book.Worksheets[0].Cells;
int maxDataRow = cells2.MaxDataRow;
int maxDataColumn = cells2.MaxDataColumn;
// Loop over all cells
for (int row = 0; row <= maxDataRow; row++)
{
for (int col = 0; col <= maxDataColumn; col++)
{
// Read the Cell value
var currentCell = cells2.CheckCell(row, col);
if (currentCell != null)
{
Console.WriteLine(currentCell.StringValue);
}
}
}

As you can observe that both of the above-mentioned approaches use more or less similar logic, that is; loop over all cells in the collection to read the cell values. This could be problematic for a number of reasons as discussed below.

  1. APIs such as MaxRow, MaxDataRow, MaxColumn, MaxDataColumn & MaxDisplayRange require extra time to gather the corresponding statistics. In case the data matrix (rows x columns) is large, using these APIs could impose a performance penalty.
  2. In most of the cases, not all cells in a given range are instantiated. In such situations to check every cell in the matrix is not so efficient as compared to check only the initialized cells.
  3. Accessing a cell in a loop as Cells row, column will cause all cell objects in a range to be instantiated, which may eventually cause OutOfMemoryException.

Conclusion

Based on above-mentioned facts, the following are the possible scenarios where enumerators should be used.

  1. Read-only access of the cell collection is required, that is; the requirement is to only inspect the cells.
  2. A large number of cells are to be traversed.
  3. Only initialized cells/rows/columns to be traversed.