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

كيفية استخدام التكرارات

Cells التكرارات

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

  1. Cells.iterator
  2. مؤلف الصف
  3. المدى

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

يوضح المثال التالي من التعليمات البرمجية تطبيق فئة 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. يوضح المثال التالي من التعليمات البرمجية تطبيق 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());
}
الأعمدة مكرر

يمكن الوصول إلى Columns Iterator أثناء استخدام طريقة ColumnCollection.iterator. يوضح المثال التالي من التعليمات البرمجية تطبيق 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());
}
}

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

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

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

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