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:
- Objekt: Data 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ökningen görs från början.
- FindOptions: Alternativ för att hitta den erforderliga datan.
Nedan används exempel på kalkylarksdata för att öva på hittametoder.
// 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 angivna värden med hjälp av Cells kollektionens Find metod med olika FindOptions. Vanligtvis accepterar Find metoden 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 till null när du söker från början.
- Hitta alternativ, hitta alternativ.
// 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 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.LookInType och FindOptions.LookAtType 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.
// 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 "); | |
} |