Hitta eller sök data

Hitta Cells som innehåller specifika data

Aspose.Cells tillhandahåller en klass,Arbetsbok , som representerar en Excel-fil. DeArbetsbok klass innehållerArbetsbladssamling , en samling som ger åtkomst till vart och ett av kalkylbladen i Excel-filen. Ett arbetsblad representeras avArbetsbladklass.

DeArbetsblad klass gerCells , en samling som representerar alla celler i kalkylbladetCellssamling innehå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.

Alla sökmetoder returnerar cellreferenserna för alla celler som innehåller det angivna sökvärdet.

Hitta som innehåller en formel

Utvecklare kan hitta en specificerad formel i kalkylbladet genom att anropaCells samlingenshitta metoden, ställa inFindOptions.setLookInType tillLookInType.FORMULASoch skicka det som en parameter tillhitta metod.

Vanligtvis ärhitta metod accepterar två eller flera parametrar:

  • Objekt att söka: representerar ett objekt som behövs för att hitta i kalkylbladet.
  • Den föregående Cell: representerar föregående cell med samma formel. Denna parameter kan ställas in på null när du söker från början.
  • Sökalternativ: representerar sökkriterierna. I exemplen nedan används följande kalkylbladsdata för att öva på att hitta metoder:

Exempel på kalkylbladsdata

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

Söker efter strängar

Att söka efter celler som innehåller ett strängvärde är enkelt och flexibelt. Det finns olika sätt att söka, till exempel söka efter celler som innehåller strängar som börjar med ett visst tecken, eller en uppsättning tecken.

Söker efter strängar som börjar med specifika tecken

För att söka efter det första tecknet i en sträng, anropaCells samlingenshitta metod, ställ inFindOptions.setLookAtType tillLookAtType.START_WITHoch skicka den som en parameter tillhitta metod.

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

Söker efter strängar som slutar med specifika tecken

Aspose.Cells kan också hitta strängar som slutar med specifika tecken. För att söka efter de sista tecknen i en sträng, anropaCells samlingenshitta metod, ställ inFindOptions.setLookAtType tillLookAtType.END_WITHoch skicka den som en parameter tillhitta metod.

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

Söka med reguljära uttryck: RegEx-funktionen

Ett reguljärt uttryck ger ett kortfattat och flexibelt sätt att matcha (specificera och känna igen) textsträngar, såsom särskilda tecken, ord eller mönster.

Till exempel, det reguljära uttrycksmönstret abc-* ~~xyz~~ matchar strängarna “abc-123-xyz”, “abc-985-xyz” och “abc-pony-xyz”.* är ett jokertecken så mönstret matchar alla strängar som börjar med “abc” och slutar med “-xyz”, oavsett vilka tecken som finns i mitten.

Aspose.Cells låter dig söka med reguljära uttryck.

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

Förhandsämnen