Trova o Cerca Dati
Ricerca delle celle contenenti dati specificati
Utilizzando Microsoft Excel
Microsoft Excel consente agli utenti di trovare celle in un foglio di lavoro che contengono dati specificati. Se si seleziona Modifica dal menu Trova in Microsoft Excel, verrà visualizzata una finestra di dialogo in cui è possibile specificare il valore di ricerca.
Qui stiamo cercando il valore “Arance”. Aspose.Cells consente anche agli sviluppatori di trovare celle nel foglio di lavoro contenenti valori specificati.
Usare Aspose.Cells
Aspose.Cells fornisce una classe, Workbook, che rappresenta un file Microsoft Excel. La classe Workbook contiene una raccolta di Worksheets che consente l’accesso a ciascun foglio di lavoro nel file Excel. Un foglio di lavoro è rappresentato dalla classe Worksheet. La classe Worksheet fornisce una raccolta di Cells che rappresenta tutte le celle nel foglio di lavoro. La raccolta Cells fornisce diversi metodi per trovare celle in un foglio di lavoro contenenti dati specificati. Alcuni di questi metodi sono discussi di seguito in modo più dettagliato.
Ricerca delle celle contenenti una formula
Gli sviluppatori possono trovare una formula specificata nel foglio di lavoro chiamando il metodo Find della collezione Cells. Tipicamente, il metodo Find accetta tre parametri:
- Oggetto: L’oggetto da cercare. Il tipo dovrebbe essere int, double, DateTime, string, bool.
- Celle precedenti: Cella precedente con lo stesso oggetto. Questo parametro può essere impostato su null se la ricerca parte dall’inizio.
- FindOptions: Opzioni per trovare l’oggetto richiesto.
Gli esempi seguenti utilizzano i dati del foglio di lavoro per praticare i metodi di ricerca:
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Opening the Excel file | |
Workbook workbook = new Workbook(sourceDir + "sampleFindingCellsContainingFormula.xlsx"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Instantiate FindOptions Object | |
FindOptions findOptions = new FindOptions(); | |
findOptions.LookInType = LookInType.Formulas; | |
// Finding the cell containing the specified formula | |
Cell cell = worksheet.Cells.Find("=SUM(A5:A10)", null, findOptions); | |
// Printing the name of the cell found after searching worksheet | |
System.Console.WriteLine("Name of the cell containing formula: " + cell.Name); |
Ricerca di dati o formule mediante FindOptions
È possibile trovare valori specificati utilizzando il metodo Find della collezione Cells con varie FindOptions. Tipicamente, il metodo Find accetta i seguenti parametri:
- Valore di ricerca, i dati o il valore da cercare.
- Celle precedenti, l’ultima cella che conteneva lo stesso valore. Questo parametro può essere impostato su null durante la ricerca dall’inizio.
- Opzioni di ricerca, le opzioni di ricerca.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Instantiate the workbook object | |
Workbook workbook = new Workbook(sourceDir + "sampleFindingDataOrFormulasUsingFindOptions.xlsx"); | |
workbook.CalculateFormula(); | |
// Get Cells collection | |
Cells cells = workbook.Worksheets[0].Cells; | |
// Instantiate FindOptions Object | |
FindOptions findOptions = new FindOptions(); | |
// Create a Cells Area | |
CellArea ca = new CellArea(); | |
ca.StartRow = 8; | |
ca.StartColumn = 2; | |
ca.EndRow = 17; | |
ca.EndColumn = 13; | |
// Set cells area for find options | |
findOptions.SetRange(ca); | |
// Set searching properties | |
findOptions.SearchBackward = false; | |
findOptions.SearchOrderByRows = true; | |
// Set the lookintype, you may specify, values, formulas, comments etc. | |
findOptions.LookInType = LookInType.Values; | |
// Set the lookattype, you may specify Match entire content, endswith, starwith etc. | |
findOptions.LookAtType = LookAtType.EntireContent; | |
// Find the cell with value | |
Cell cell = cells.Find(341, null, findOptions); | |
if (cell != null) | |
{ | |
Console.WriteLine("Name of the cell containing the value: " + cell.Name); | |
} | |
else | |
{ | |
Console.WriteLine("Record not found "); | |
} |
Ricerca delle celle contenenti un valore di stringa specificato o numero
È possibile trovare valori di stringa specificati chiamando lo stesso metodo Find trovato nella collezione Cells con varie FindOptions.
Specificare le proprietà FindOptions.LookInType e FindOptions.LookAtType. Il codice di esempio seguente illustra come utilizzare queste proprietà per trovare celle con un numero variabile di stringhe all'inizio o al centro o alla fine della stringa della cella.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiate the workbook object | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Get Cells collection | |
Cells cells = workbook.Worksheets[0].Cells; | |
FindOptions opts = new FindOptions(); | |
opts.LookInType = LookInType.Values; | |
opts.LookAtType = LookAtType.EntireContent; | |
// Find the cell with the input integer or double | |
Cell cell1 = cells.Find(205, null, opts); | |
if (cell1 != null) | |
{ | |
Console.WriteLine("Name of the cell containing the value: " + cell1.Name); | |
} | |
else | |
{ | |
Console.WriteLine("Record not found "); | |
} | |
// Find the cell with the input string | |
Aspose.Cells.Cell cell2 = cells.Find("Items A", null, opts); | |
if (cell2 != null) | |
{ | |
Console.WriteLine("Name of the cell containing the value: " + cell2.Name); | |
} | |
else | |
{ | |
Console.WriteLine("Record not found "); | |
} | |
// Find the cell containing with the input string | |
opts.LookAtType = LookAtType.Contains; | |
Cell cell3 = cells.Find("Data", null, opts); | |
if (cell3 != null) | |
{ | |
Console.WriteLine("Name of the cell containing the value: " + cell3.Name); | |
} | |
else | |
{ | |
Console.WriteLine("Record not found "); | |
} |