Hitta eller söka data

Att hitta celler som innehåller specificerad data

Använda Microsoft Excel

Microsoft Excel tillåter användare att hitta celler i ett kalkylark som innehåller specificerade data. Om du väljer Redigera från Sök-menyn i Microsoft Excel kommer du att se en dialog där du kan specificera sökvärdet.

Här letar vi efter värdet “Apelsiner”. Aspose.Cells tillåter också utvecklare att hitta celler i kalkylarket som innehåller specificerade värden.

Använda Aspose.Cells

Aspose.Cells tillhandahåller en klass, Workbook, som representerar en Microsoft Excel-fil. Workbook-klassen innehåller en worksheets-samling som ger åtkomst till varje kalkylblad i Excel-filen. Ett kalkylblad representeras av Worksheet-klassen. Worksheet-klassen tillhandahåller en cells-samling som representerar alla celler i kalkylarket. cells-samlingen tillhandahåller flera metoder för att hitta celler i ett kalkylark som innehåller användarspecificerad data. Några av dessa metoder diskuteras nedan mer i detalj.

Att hitta celler som innehåller en formel

Utvecklare kan hitta en specificerad formel i kalkylarket genom att anropa cells-samlingens find-metod. Vanligtvis accepterar find-metoden tre parametrar:

  • vad: Objektet att söka efter. Typen ska vara int,double, DateTime, string, bool.
  • föregående_cell: Föregående cell med samma objekt. Denna parameter kan vara inställd på null om sökning från början.
  • find_options: Alternativ för att hitta det nödvändiga objektet.

Nedan används exempel på kalkylarksdata för att öva på hittametoder.

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)

Hitta data eller formler med FindOptions

Det är möjligt att hitta angivna värden med hjälp av cells kollektionens find metod med olika FindOptions. Vanligtvis accepterar find metoden följande parametrar:

  • vad:, datan eller värdet som ska sökas efter.
  • föregående_cell, den senaste cellen som innehöll samma värde. Denna parameter kan vara inställd på null när du söker från början.
  • find_options, hitta alternativ.
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 ")

Hitta celler som innehåller specifierade strängvärden eller nummer

Det är möjligt att hitta angivna strängvärden genom att anropa samma find metod som finns i cells kollektion med olika FindOptions.

Ange FindOptions.look_in_type och FindOptions.look_at_type egenskaperna. Följande exempelkod illustrerar hur man använder dessa egenskaper för att hitta celler med olika antal strängar i början eller i mitten eller i änden av cellens sträng.

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

Fortsatta ämnen