Trova o Cerca Dati

Trova celle che contengono dati specifici

Aspose.Cells fornisce una classe, Workbook, che rappresenta un file di Excel. La classe Workbook contiene WorksheetCollection, una collezione che consente l’accesso a ciascun foglio di lavoro nel file di Excel. Un foglio di lavoro è rappresentato dalla classe Worksheet.

La classe Worksheet fornisce Cells, una raccolta che rappresenta tutte le celle nel foglio di lavoro. La raccolta Cells fornisce diversi metodi per trovare celle in un foglio di lavoro che contengono dati specificati dall’utente. Alcuni di questi metodi sono discussi di seguito in modo più dettagliato.

Tutti i metodi di ricerca restituiscono i riferimenti delle celle che contengono il valore di ricerca specificato.

Ricerca che Contengono una Formula

I programmatori possono trovare una formula specifica nel foglio di lavoro chiamando il metodo find della collezione Cells, impostando FindOptions.setLookInType su LookInType.FORMULAS e passandolo come parametro al metodo find.

Tipicamente, il metodo find accetta due o più parametri:

  • Oggetto da cercare: rappresenta un oggetto da trovare nel foglio di lavoro.
  • Cella precedente: rappresenta la cella precedente con la stessa formula. Questo parametro può essere impostato su null durante la ricerca dall’inizio.
  • Opzioni di ricerca: rappresenta i criteri di ricerca. Negli esempi seguenti vengono utilizzati i dati del foglio di lavoro seguenti per praticare i metodi di ricerca:

Dati di esempio del foglio di lavoro

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());

Ricerca di Stringhe

La ricerca di celle che contengono un valore di stringa è semplice e flessibile. Ci sono diversi modi per cercare, ad esempio cercare celle che contengono stringhe che iniziano con un carattere particolare, o un insieme di caratteri.

Ricerca di Stringhe che Iniziano con Caratteri Specifici

Per cercare il primo carattere in una stringa, chiamare il metodo find della collezione Cells, impostare FindOptions.setLookAtType su LookAtType.START_WITH e passarlo come parametro al metodo 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());

Ricerca di Stringhe che Terminano con Caratteri Specifici

Aspose.Cells può trovare anche stringhe che terminano con caratteri specifici. Per cercare gli ultimi caratteri in una stringa, chiamare il metodo find della collezione Cells, impostare FindOptions.setLookAtType su LookAtType.END_WITH e passarlo come parametro al metodo 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());

Ricerca con Espressioni Regolari: la Funzionalità RegEx

Un’espressione regolare fornisce un modo conciso e flessibile per corrispondere (specificare e riconoscere) stringhe di testo, come caratteri, parole o modelli particolari.

Ad esempio, il modello di espressione regolare abc-*xyz corrisponde alle stringhe “abc-123-xyz”, “abc-985-xyz” e “abc-pony-xyz”. * è un carattere jolly quindi il modello corrisponde a qualsiasi stringa che inizia con “abc” e termina con “-xyz”, indipendentemente dai caratteri presenti nel mezzo.

Aspose.Cells ti consente di cercare con espressioni regolari.

// 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);

Argomenti avanzati