البحث واستبدال البيانات في نطاق
Contents
[
Hide
]
في بعض الأحيان تحتاج إلى البحث عن واستبدال البيانات المحددة في نطاق وتجاهل قيم الخلية خارج النطاق المرغوب. يسمح Aspose.Cells لك بتقييد البحث إلى نطاق معين. يشرح هذا المقال كيفية ذلك.
يوفر Aspose.Cells الطريقة FindOptions.SetRange() لتحديد نطاق عند البحث في البيانات. يوضح الكود العيني أدناه البحث واستبدال البيانات في نطاق.
This file contains 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-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
string filePath = dataDir+ "input.xlsx"; | |
Workbook workbook = new Workbook(filePath); | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Specify the range where you want to search | |
// Here the range is E3:H6 | |
CellArea area = CellArea.CreateCellArea("E9", "H15"); | |
// Specify Find options | |
FindOptions opts = new FindOptions(); | |
opts.LookInType = LookInType.Values; | |
opts.LookAtType = LookAtType.EntireContent; | |
opts.SetRange(area); | |
Cell cell = null; | |
do | |
{ | |
// Search the cell with value search within range | |
cell = worksheet.Cells.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.out.xlsx"); |