Come e dove utilizzare gli enumeratori

Come utilizzare gli enumeratori

Enumeratore celle

Esistono vari modi per accedere all’enumeratore delle celle, e si può utilizzare uno qualsiasi di questi metodi in base ai requisiti dell’applicazione. Ecco i metodi che restituiscono l’enumeratore delle celle.

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

Tutti i metodi sopra menzionati restituiscono l’enumeratore che consente di attraversare la raccolta di celle che sono state inizializzate.

Nell’esempio di codice seguente viene dimostrata l’implementazione dell’interfaccia IEnumerator per una raccolta di celle.

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

Enumeratore di righe

L’enumeratore di righe può essere accessibile utilizzando il metodo RowCollection.GetEnumerator. Nell’esempio di codice seguente viene dimostrata l’implementazione dell’interfaccia IEnumerator per 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);
}

Enumeratore di colonne

L’enumeratore di colonne può essere accessibile utilizzando il metodo ColumnCollection.GetEnumerator. Nell’esempio di codice seguente viene dimostrata l’implementazione dell’interfaccia IEnumerator per 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);
}

Dove utilizzare gli enumeratori

Per discutere i vantaggi dell’uso degli enumeratori, prendiamo un esempio in tempo reale.

Scenario

Un requisito dell’applicazione è attraversare tutte le celle in un Worksheet dato per leggere i loro valori. Potrebbero esserci diversi modi per implementare questo obiettivo. Alcuni sono dimostrati di seguito.

Utilizzo della gamma di visualizzazione

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

Utilizzo di MaxDataRow e 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);
}
}
}

Come puoi osservare, entrambi gli approcci sopra menzionati utilizzano più o meno una logica simile, cioè; attraversa tutte le celle nella raccolta per leggere i valori delle celle. Questo potrebbe essere problematico per un certo numero di motivi come discusso di seguito.

  1. Le API come MaxRow, MaxDataRow, MaxColumn, MaxDataColumn & MaxDisplayRange richiedono tempo aggiuntivo per raccogliere le statistiche corrispondenti. Nel caso la matrice di dati (righe x colonne) sia ampia, l’uso di queste API potrebbe comportare una penalità sulle prestazioni.
  2. Nella maggior parte dei casi, non tutte le celle in un dato intervallo sono istanziate. In tali situazioni controllare ogni cella nella matrice non è così efficiente rispetto al controllo solo delle celle inizializzate.
  3. Accedere a una cella in un ciclo come celle riga, colonna farà istanziare tutti gli oggetti cella in un intervallo, che potrebbe alla fine causare OutOfMemoryException.

Conclusioni

Sulla base dei fatti sopra menzionati, di seguito sono riportati i possibili scenari in cui dovrebbero essere utilizzati gli enumeratori.

  1. È richiesto l’accesso in sola lettura alla collezione di celle, cioè; il requisito è di ispezionare solo le celle.
  2. Deve essere attraversato un gran numero di celle.
  3. Si devono attraversare solo le celle/righe/colonne inizializzate.