Find or Search Data

Finding Cells Containing Specified Data

Using Microsoft Excel

Microsoft Excel allows users to find cells in a worksheet that contains specified data. If you select Edit from the Find menu in Microsoft Excel, you will see a dialog where you can specify the search value.

Here, we are looking for the value “Oranges”. Aspose.Cells also allows developers to find cells in the worksheet containing specified values.

Using Aspose.Cells

Aspose.Cells provides a class, Workbook, that represents a Microsoft Excel file. The Workbook class contains a worksheets collection that allows access to each worksheet in the Excel file. A worksheet is represented by the Worksheet class. The Worksheet class provides a cells collection that represents all cells in the worksheet. The cells collection provides several methods for finding cells in a worksheet containing user-specified data. A few of these methods are discussed below in more detail.

Finding Cells Containing a Formula

Developers can find a specified formula in the worksheet by calling the cells collection’s find method. Typically, the find method accepts three parameters:

  • what: The object to search for. The type should be int,double,DateTime,string,bool.
  • previous_cell: Previous cell with the same object. This parameter can be set to null if searching from the start.
  • find_options: Options for finding the required object.

The examples below use worksheet data for practicing find methods:

from aspose.cells import FindOptions, LookInType, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Opening the Excel file
workbook = Workbook(sourceDir + "sampleFindingCellsContainingFormula.xlsx")
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Instantiate FindOptions Object
findOptions = FindOptions()
findOptions.look_in_type = LookInType.FORMULAS
# Finding the cell containing the specified formula
cell = worksheet.cells.find("=SUM(A5:A10)", None, findOptions)
# Printing the name of the cell found after searching worksheet
print("Name of the cell containing formula: " + cell.name)

Finding Data or Formulas using FindOptions

It is possible to find specified values using the cells collection’s find method with various FindOptions. Typically, the find method accepts the following parameters:

  • what:, the data or value to be searched for.
  • previous_cell, the last cell that contained the same value. This parameter can be set to null when searching from the start.
  • find_options, the find options.
from aspose.cells import CellArea, FindOptions, LookAtType, LookInType, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Instantiate the workbook object
workbook = Workbook(sourceDir + "sampleFindingDataOrFormulasUsingFindOptions.xlsx")
workbook.calculate_formula()
# Get Cells collection
cells = workbook.worksheets[0].cells
# Instantiate FindOptions Object
findOptions = FindOptions()
# Create a Cells Area
ca = CellArea()
ca.start_row = 8
ca.start_column = 2
ca.end_row = 17
ca.end_column = 13
# Set cells area for find options
findOptions.set_range(ca)
# Set searching properties
findOptions.search_backward = False
findOptions.seach_order_by_rows = True
# Set the lookintype, you may specify, values, formulas, comments etc.
findOptions.look_in_type = LookInType.VALUES
# Set the lookattype, you may specify Match entire content, endswith, starwith etc.
findOptions.look_at_type = LookAtType.ENTIRE_CONTENT
# Find the cell with value
cell = cells.find(341, None, findOptions)
if cell != None:
print("Name of the cell containing the value: " + cell.name)
else:
print("Record not found ")

Finding Cells Containing Specified String Value or Number

It is possible to find specified string values by calling the same find method found in the cells collection with various FindOptions.

Specify the FindOptions.look_in_type and FindOptions.look_at_type properties. The following example code illustrates how to use these properties to find cells with various number of strings at the beginning or at the center or at the end of the cell’s string.

from aspose.cells import FindOptions, LookAtType, LookInType, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Instantiate the workbook object
workbook = Workbook(dataDir + "book1.xls")
# Get Cells collection
cells = workbook.worksheets[0].cells
opts = FindOptions()
opts.look_in_type = LookInType.VALUES
opts.look_at_type = LookAtType.ENTIRE_CONTENT
# Find the cell with the input integer or double
cell1 = cells.find(205, None, opts)
if cell1 != None:
print("Name of the cell containing the value: " + cell1.name)
else:
print("Record not found ")
# Find the cell with the input string
cell2 = cells.find("Items A", None, opts)
if cell2 != None:
print("Name of the cell containing the value: " + cell2.name)
else:
print("Record not found ")
# Find the cell containing with the input string
opts.look_at_type = LookAtType.CONTAINS
cell3 = cells.find("Data", None, opts)
if cell3 != None:
print("Name of the cell containing the value: " + cell3.name)
else:
print("Record not found ")

Advance topics