بحث واستبدال البيانات في نطاق
Contents
[
Hide
]
في بعض الأحيان ، تحتاج إلى البحث عن بيانات محددة واستبدالها في نطاق ، مع تجاهل أي قيم خلية خارج النطاق المطلوب. Aspose.Cells يسمح لك بتحديد البحث بنطاق معين. يشرح هذا المقال كيف.
يوفر Aspose.Cells ملفFindOptions.setRange () لتحديد النطاق عند البحث عن البيانات.
افترض أنك تريد البحث عن السلسلة**“بحث”** واستبدله بـ**“يحل محل”** في النطاق**E3: H6**. في لقطة الشاشة أدناه ، يمكن رؤية سلسلة “search” في عدة خلايا ولكننا نريد استبدالها فقط في نطاق معين ، مظلل هنا باللون الأصفر.
ملف الإدخال
بعد تنفيذ الكود ، يبدو ملف الإخراج كما يلي. تم استبدال جميع سلاسل “البحث” ضمن النطاق بكلمة “استبدال”.
ملف إلاخراج
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(SearchReplaceDataInRange.class); | |
Workbook workbook = new Workbook(dataDir + "input.xlsx"); | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Specify the range where you want to search | |
// Here the range is E3:H6 | |
CellArea area = CellArea.createCellArea("E3", "H6"); | |
// Specify Find options | |
FindOptions opts = new FindOptions(); | |
opts.setLookInType(LookInType.VALUES); | |
opts.setLookAtType(LookAtType.ENTIRE_CONTENT); | |
opts.setRange(area); | |
Cell cell = null; | |
do { | |
// Search the cell with value search within range | |
cell = worksheet.getCells().find("search", cell, opts); | |
// If no such cell found, then break the loop | |
if (cell == null) | |
break; | |
// Replace the cell with value replace | |
cell.putValue("replace"); | |
} while (true); | |
// Save the workbook | |
workbook.save(dataDir + "output.xlsx"); |