Hitta eller sök data
Hitta Cells Innehåller specificerade data
Använder Microsoft Excel
Microsoft Excel tillåter användare att hitta celler i ett kalkylblad som innehåller specificerade data. Om du väljerRedigera frånHitta menyn i Microsoft Excel kommer du att se en dialogruta där du kan ange sökvärdet.
Här letar vi efter värdet “Apelsiner”. Aspose.Cells tillåter också utvecklare att hitta celler i kalkylbladet som innehåller specificerade värden.
Använder Aspose.Cells
Aspose.Cells tillhandahåller en klass,Arbetsbok , som representerar en Microsoft Excel-fil. DeArbetsbok klass innehåller enArbetsblad samling som ger åtkomst till varje kalkylblad i Excel-filen. Ett arbetsblad representeras avArbetsblad klass. DeArbetsblad klass ger enCells samling som representerar alla celler i kalkylbladet. DeCellssamling tillhandahåller flera metoder för att hitta celler i ett kalkylblad som innehåller användarspecificerad data. Några av dessa metoder diskuteras mer i detalj nedan.
Hitta Cells som innehåller en formel
Utvecklare kan hitta en specificerad formel i kalkylbladet genom att anropaCells samlingensHitta metod. Vanligtvis ärHittaMetoden accepterar tre parametrar:
- **Objekt:**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 ställas in på null om du söker från början.
- FindOptions: Alternativ för att hitta det önskade objektet.
I exemplen nedan används kalkylbladsdata för att öva på att hitta metoder:
// 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); |
Hitta data eller formler med FindOptions
Det är möjligt att hitta specificerade värden med hjälp avCells samlingensHitta metod med olikaHitta Alternativ . Vanligtvis ärHittametoden accepterar följande parametrar:
- Sökvärde, data eller värde som ska sökas efter.
- Föregående cell, den sista cellen som innehöll samma värde. Denna parameter kan ställas in på null när du söker från början.
- Hitta alternativ, sökalternativen.
// 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 "); | |
} |
Hitta Cells som innehåller specificerat strängvärde eller nummer
Det är möjligt att hitta specificerade strängvärden genom att anropa detsammaHitta metod som finns iCells samling med olikaHitta Alternativ.
SpecificeraFindOptions.LookInType ochFindOptions.LookAtType egenskaper. Följande exempelkod illustrerar hur man använder dessa egenskaper för att hitta celler med olika antal strängar vidbörjan eller vidCentrum eller vidslutet av cellens sträng.
// 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 "); | |
} |