البحث واستبدال البيانات في نطاق

توفر Aspose.Cells الطريقة FindOptions.setRange() لتحديد نطاق عند البحث عن البيانات.

فرضًا، ترغب في البحث عن سلسلة “بحث” واستبدالها بـ “استبدال” في النطاق E3:H6. يمكن رؤية سلسلة “بحث” في العديد من الخلايا ولكن نريد استبدالها فقط في النطاق المعطى، هنا مظلل باللون الأصفر.

الملف الداخلي

todo:image_alt_text

بعد تنفيذ الشفرة، تبدو ملف الإخراج كما هو فيما يلي. تم استبدال جميع سلاسل “البحث” ضمن النطاق بـ “استبدال”.

ملف الإخراج

todo:image_alt_text

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

مقالات ذات صلة