Numaralandırıcıları Nasıl ve Nerede Kullanılır

Numaralandırıcıları Nasıl Kullanılır

Hücreler Numaralandırıcı

Hücreler Numaralandırıcısına erişmenin çeşitli yolları vardır ve uygulama gereksinimlerine bağlı olarak bu yöntemlerden herhangi biri kullanılabilir. İşte hücreler numaralandırıcısını döndüren yöntemler.

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

Yukarıda bahsedilen tüm yöntemler, başlatılmış hücre koleksiyonunu gezinmeyi sağlayan numaralandırıcıyı döndürür.

Aşağıdaki kod örneği, Hücreler koleksiyonu için IEnumerator arabirimi uygulamasını göstermektedir.

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

Satırlar Numaralandırıcı

RowCollection.GetEnumerator kullanılırken Satırlar Numaralandırıcısına erişilebilir. Aşağıdaki kod örneği, RowCollection için IEnumerator arabiriminin uygulamasını göstermektedir.

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

Sütunlar Numaralandırıcı

ColumnCollection.GetEnumerator kullanılırken Sütunlar Numaralandırıcısına erişilebilir. Aşağıdaki kod örneği, ColumnCollection için IEnumerator arabirimini uygulamanın uygulamasını göstermektedir.

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

Numaralandırıcıları Nerede Kullanılacağı

Numaralandırıcıları kullanmanın avantajlarını tartışmak için, gerçek bir zaman örneği ele alalım.

Senaryo

Bir uygulama gereksinimi, belirli bir Worksheet içindeki tüm hücreleri gezerek değerlerini okumaktır. Bu hedefi uygulamanın birkaç yolu olabilir. Birkaç örnek aşağıda gösterilmektedir.

Görüntü Aralığı Kullanarak

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

MaxDataRow & MaxDataColumn Kullanarak

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

Yukarıda bahsedilen her iki yaklaşımın da, tüm hücreleri döngü içinde dolaşarak hücre değerlerini okuma mantığına daha veya daha az benzer olduğunu gözlemleyebilirsiniz. Bu, aşağıda tartışılan birçok nedenden biri olabilir.

  1. MaxRow, MaxDataRow, MaxColumn, MaxDataColumn ve MaxDisplayRange gibi API’lar, ilgili istatistikleri toplamak için ekstra zaman gerektirir. Veri matrisi (satır x sütun) büyükse, bu API’ların kullanılması performans cezası uygulayabilir.
  2. Çoğu durumda, verilen bir aralıktaki tüm hücreler oluşturulmamıştır. Bu tür durumlarda, matristeki her hücreyi kontrol etmek, yalnızca başlatılmış hücreleri kontrol etmekten daha verimli değildir.
  3. Hücreye döngü içinde Cells satır, sütun olarak erişmek, bir aralıktaki tüm hücre nesnelerinin oluşturulmasına neden olabilir, bu da sonunda OutOfMemoryException’a neden olabilir.

Sonuç

Yukarıda belirtilen gerçeklere dayanarak, aşağıdakiler, numaralandırıcıların kullanılması gereken olası senaryolarıdır.

  1. Yalnızca hücre koleksiyonunun salt okunur erişimi gereklidir, yani; gereksinim yalnızca hücreleri incelemektir.
  2. Birçok hücrenin dolaşılması gereklidir.
  3. Yalnızca başlatılmış hücreler/satırlar/sütunlar dolaşılmalıdır.