البحث عن البيانات أو البحث

العثور على الخلايا التي تحتوي على بيانات محددة

توفر Aspose.Cells فئة، Workbook، التي تمثل ملف Excel. تحتوي فئة Workbook على WorksheetCollection، وهي مجموعة تسمح بالوصول إلى كل ورقة العمل في ملف Excel. تمثل ورقة العمل بواسطة فئة Worksheet.

توفر فئة Worksheet Cells، مجموعة تمثل جميع الخلايا في ورقة العمل. توفر مجموعة Cells العديد من الأساليب للعثور على الخلايا في ورقة العمل التي تحتوي على بيانات تحددها المستخدم. يتم مناقشة بعض هذه الأساليب أدناه بتفصيل أكثر.

تُرجع جميع أساليب البحث مراجع الخلايا التي تحتوي على قيمة البحث المحددة.

البحث عن تحتوي على صيغة

يمكن للمطورين العثور على صيغة محددة في ورقة العمل عن طريق استدعاء Cells مجموعة البحث’s find، ضبط FindOptions.setLookInType إلى LookInType.FORMULAS وتمريرها كمعلمة إلى الطريق find.

عادةً، تقبل طريق find معلمتين أو أكثر:

  • الكائن المطلوب البحث عنه: يمثل كائنًا مطلوب العثور عليه في ورقة العمل.
  • الخلية السابقة: تمثل الخلية السابقة بنفس الصيغة. يمكن تعيين هذا المعلمة على قيمة الفراغ عند البحث من البداية.
  • خيارات البحث: تمثل معايير البحث. في الأمثلة أدناه ، يتم استخدام بيانات ورق العمل التالية لممارسة طرق البحث:

بيانات ورق العمل العينة

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.getSharedDataDir(FindingCellsContainingFormula.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
// Finding the cell containing the specified formula
Cells cells = worksheet.getCells();
FindOptions findOptions = new FindOptions();
findOptions.setLookInType(LookInType.FORMULAS);
Cell cell = cells.find("=SUM(A5:A10)", null, findOptions);
// Printing the name of the cell found after searching worksheet
System.out.println("Name of the cell containing formula: " + cell.getName());

البحث عن السلاسل

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

البحث عن السلاسل التي تبدأ بأحرف معينة

للبحث عن الحرف الأول في سلسلة ، ادعوا مجموعة Cells للfind الأسلوب ، ثم ضبط FindOptions.setLookAtType إلى LookAtType.START_WITH وتمريرها كمعلمة إلى find الأسلوب.

// 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.getSharedDataDir(FindingCellsWithStringOrNumber.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
// Finding the cell containing the specified formula
Cells cells = worksheet.getCells();
// Instantiate FindOptions
FindOptions findOptions = new FindOptions();
// Finding the cell containing a string value that starts with "Or"
findOptions.setLookAtType(LookAtType.START_WITH);
Cell cell = cells.find("SH", null, findOptions);
// Printing the name of the cell found after searching worksheet
System.out.println("Name of the cell containing String: " + cell.getName());

البحث عن السلاسل التي تنتهي بأحرف محددة

Aspose.Cells يمكن أيضًا العثور على السلاسل التي تنتهي بأحرف محددة. للبحث عن الأحرف الأخيرة في سلسلة ، ادعوا مجموعة Cells للfind الأسلوب ، ثم ضبط FindOptions.setLookAtType إلى LookAtType.END_WITH وتمريرها كمعلمة إلى find الأسلوب.

// 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.getSharedDataDir(FindingCellsEndWithSpecificCharacters.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
// Finding the cell containing the specified formula
Cells cells = worksheet.getCells();
// Instantiate FindOptions
FindOptions findOptions = new FindOptions();
// Finding the cell containing a string value that ends with "es"
findOptions.setLookAtType(LookAtType.END_WITH);
Cell cell = cells.find("SH", null, findOptions);
// Printing the name of the cell found after searching worksheet
System.out.println("Name of the cell containing String: " + cell.getName());

البحث بتعابير منتظمة: ميزة RegEx

توفر التعبير المنتظم وسيلة موجزة ومرنة لمطابقة (تحديد واعتراف) سلاسل نصية ، مثل الأحرف الخاصة أو الكلمات أو الأنماط.

على سبيل المثال ، يطابق نمط التعبير المنتظم abc-*xyz السلاسل “abc-123-xyz” ، “abc-985-xyz” و “abc-pony-xyz”. العلامة * هي بطاقة وبالتالي يتطابق النمط مع أي سلاسل تبدأ بـ “abc” وتنتهي بـ “-xyz” ، بغض النظر عما إذا كانت الأحرف هي في الوسط.

Aspose.Cells تتيح لك البحث باستخدام التعابير المنتظمة.

// 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.getSharedDataDir(FindingwithRegularExpressions.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
// Finding the cell containing the specified formula
Cells cells = worksheet.getCells();
// Instantiate FindOptions
FindOptions findOptions = new FindOptions();
// Instantiate FindOptions
FindOptions opt = new FindOptions();
// Set the search key of find() method as standard RegEx
opt.setRegexKey(true);
opt.setLookAtType(LookAtType.ENTIRE_CONTENT);
cells.find("abc[\\s]*$", null, opt);

مواضيع متقدمة