Daten finden oder suchen
Suchen von Zellen, die bestimmte Daten enthalten
Verwendung von Microsoft Excel
Microsoft Excel ermöglicht es Benutzern, in einem Arbeitsblatt Zellen zu finden, die bestimmte Daten enthalten. Wenn Sie Bearbeiten im Suchen-Menü in Microsoft Excel auswählen, wird ein Dialogfeld angezeigt, in dem Sie den Suchwert angeben können.
Hier suchen wir nach dem Wert “Orangen”. Aspose.Cells ermöglicht es Entwicklern auch, in einem Arbeitsblatt Zellen mit bestimmten Werten zu finden.
Verwendung von Aspose.Cells
Aspose.Cells bietet eine Klasse, Workbook, die eine Microsoft Excel-Datei darstellt. Die Klasse Workbook enthält eine Sammlung von worksheets, die den Zugriff auf jedes Arbeitsblatt in der Excel-Datei ermöglicht. Ein Arbeitsblatt wird durch die Klasse Worksheet repräsentiert. Die Klasse Worksheet stellt eine Sammlung von cells dar, die alle Zellen im Arbeitsblatt repräsentiert. Die Sammlung cells bietet mehrere Methoden zum Finden von Zellen in einem Arbeitsblatt, die vom Benutzer spezifizierte Daten enthalten. Einige dieser Methoden werden im Folgenden genauer erläutert.
Suchen von Zellen, die eine Formel enthalten
Entwickler können eine bestimmte Formel im Arbeitsblatt finden, indem sie die Methode find der Sammlung cells aufrufen. Typischerweise akzeptiert die Methode find drei Parameter:
- what: Das zu suchende Objekt. Der Typ sollte int, double, DateTime, string, bool sein.
- previous_cell: Vorherige Zelle mit demselben Objekt. Dieser Parameter kann auf null gesetzt werden, wenn von Anfang an gesucht wird.
- find_options: Optionen zum Auffinden des erforderlichen Objekts.
Die folgenden Beispiele verwenden Arbeitsblattdaten, um die Find-Methoden zu üben:
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) |
Suchen von Daten oder Formeln mithilfe von FindOptions
Es ist möglich, angegebene Werte mithilfe der cells-Methode der find-Sammlung mit verschiedenen FindOptions zu finden. Normalerweise akzeptiert die Methode find die folgenden Parameter:
- was:, die zu suchenden Daten oder Werte.
- previous_cell, die letzte Zelle, die denselben Wert enthielt. Dieser Parameter kann auf Null gesetzt werden, wenn von Anfang an gesucht wird.
- find_options, die Suchoptionen.
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 ") |
Zellen finden, die den angegebenen Zeichenfolgenwert oder die angegebene Zahl enthalten
Es ist möglich, angegebene Zeichenfolgenwerte zu finden, indem dieselbe find-Methode in der cells-Sammlung mit verschiedenen FindOptions aufgerufen wird.
Geben Sie die FindOptions.look_in_type- und FindOptions.look_at_type-Eigenschaften an. Das folgende Beispielcode veranschaulicht die Verwendung dieser Eigenschaften zum Auffinden von Zellen mit verschiedener Anzahl von Zeichenfolgen am Anfang oder in der Mitte oder am Ende der Zellzeichenfolge.
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 ") |