كيف وأين تستخدم العدادين

كيفية استخدام العدادين

Cells العداد

هناك طرق مختلفة للوصول إلى Cells Enumerator ، ويمكن للمرء استخدام أي من هذه الطرق بناءً على متطلبات التطبيق. فيما يلي الطرق التي تُرجع عد الخلايا.

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

تقوم جميع الطرق المذكورة أعلاه بإرجاع العداد الذي يسمح بعبور مجموعة الخلايا التي تمت تهيئتها.

يوضح المثال التالي من التعليمات البرمجية تطبيق واجهة IEnumerator لمجموعة Cells.

// 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 أثناء استخدام ملفRowCollection.GetEnumerator طريقة. يوضح المثال التالي من التعليمات البرمجية تطبيق واجهة IEnumerator لـمجموعة 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);
}

عداد الأعمدة

يمكن الوصول إلى عدّاد الأعمدة أثناء استخدام ملفColumnCollection.GetEnumerator طريقة. يوضح المثال التالي من التعليمات البرمجية تطبيق واجهة IEnumerator لـجمع العمود.

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

أين تستخدم العدادين

من أجل مناقشة مزايا استخدام العدادين ، لنأخذ مثالاً في الوقت الفعلي.

سيناريو

مطلب التطبيق هو اجتياز جميع الخلايا في معطىورقة عمللقراءة قيمهم. يمكن أن يكون هناك عدة طرق لتنفيذ هذا الهدف. يتم عرض القليل أدناه.

باستخدام نطاق العرض

// 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

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

كما يمكنك ملاحظة أن كلا النهجين المذكورين أعلاه يستخدمان منطقًا متشابهًا إلى حد ما ، أي ؛ حلقة فوق جميع الخلايا في المجموعة لقراءة قيم الخلية. قد يكون هذا مشكلة لعدد من الأسباب كما هو موضح أدناه.

  1. واجهات برمجة التطبيقات مثلماكسرو, ماكسداتارو, MaxColumn, MaxDataColumn & MaxDisplayRangeتتطلب وقتًا إضافيًا لجمع الإحصائيات المقابلة. إذا كانت مصفوفة البيانات (الصفوف × الأعمدة) كبيرة ، فقد يؤدي استخدام واجهات برمجة التطبيقات هذه إلى فرض عقوبة على الأداء.
  2. في معظم الحالات ، لا يتم إنشاء مثيل لكافة الخلايا الموجودة في النطاق المحدد. في مثل هذه الحالات ، لا يكون التحقق من كل خلية في المصفوفة فعالاً مقارنة بفحص الخلايا التي تمت تهيئتها فقط.
  3. الوصول إلى خلية في حلقة مثل صف Cells ، سيؤدي العمود إلى إنشاء مثيل لجميع كائنات الخلية في النطاق ، مما قد يتسبب في النهاية في OutOfMemoryException.

استنتاج

بناءً على الحقائق المذكورة أعلاه ، فيما يلي السيناريوهات المحتملة حيث يجب استخدام العدادين.

  1. مطلوب وصول للقراءة فقط لمجموعة الخلايا ، أي ؛ الشرط هو فحص الخلايا فقط.
  2. يجب اجتياز عدد كبير من الخلايا.
  3. يتم اجتياز الخلايا / الصفوف / الأعمدة المهيأة فقط.