
Excel-spreadsheets maken het mogelijk om een enorme hoeveelheid gegevens in tabelvorm op te slaan. In bepaalde gevallen moeten we mogelijk cellen doorzoeken die specifieke gegevenswaarden of tekenreeksen uit opgeslagen gegevens bevatten. Hoewel Excel ingebouwde functionaliteit biedt om alle spreadsheets te doorzoeken, moeten we de zoekbewerking mogelijk programmatisch uitvoeren in Java-toepassingen. In dit artikel leren we gegevens zoeken in Excel met behulp van Java. In dit artikel komen de volgende onderwerpen aan bod:
- [Java API om gegevens te zoeken in Excel][2]
- [Specifieke tekst zoeken in Excel met Java][3]
- [Zoek tekst die begint met specifieke letters][4]
- [Zoek tekst die eindigt op specifieke letters][5]
- [Zoek tekst met specifieke letters][6]
- [Zoeken met reguliere expressie in Excel met Java][7]
- [Hoofdlettergevoelig zoeken in Excel met Java][8]
- [Formule zoeken in Excel met Java][9]
Java API om gegevens in Excel te doorzoeken
We zullen de [Aspose.Cells for Java][10] API gebruiken om een zoekbewerking uit te voeren op het [XLSX][11]-bestand. Hiermee kunnen Excel-automatiseringsfuncties programmatisch worden uitgevoerd zonder dat een Microsoft Excel-toepassing nodig is. [Download][12] de JAR van de API of voeg de volgende pom.xml-configuratie toe aan een op Maven gebaseerde Java-toepassing.
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>22.1</version>
</dependency>
Zoek specifieke tekst in Excel met behulp van Java
We kunnen alle tekst-, getal- of datumwaarden in een Excel-bestand doorzoeken door de onderstaande stappen te volgen:
- Laad eerst een Excel-bestand met de klasse [Workbook][13].
- Ga vervolgens naar het eerste [werkblad][14] in het Excel-bestand.
- Pak dan [cells][15] van het geopende blad.
- Maak vervolgens een instantie van de klasse [FindOptions][16].
- Stel daarna [LookAtType][17] in als “ENTIRE_CONTENT”.
- Zoek ten slotte de tekst met behulp van de methode [Cells.find()][18]. Er zijn zoekstring en FindOptions nodig als argumenten.
Het volgende codevoorbeeld laat zien hoe u met Java een specifieke tekst in een Excel-bestand kunt vinden.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// Load an Excel file Workbook workbook = new Workbook("C:\\Files\\Cells\\sample.xlsx"); // Access the first worksheet in the Excel file Worksheet worksheet = workbook.getWorksheets().get(0); // Get all the cells in CellCollections Cells cells = worksheet.getCells(); // Initialize FindOptions FindOptions findOptions = new FindOptions(); // Find the cell containing a string value findOptions.setLookAtType(LookAtType.ENTIRE_CONTENT); Cell cell = cells.find("A Company", null, findOptions); // Show the cell name and its value System.out.println("Name of the cell containing String: " + cell.getName()); System.out.println("the cell value is: " + cell.getValue()); Zoek specifieke tekst in Excel met behulp van Java.
Zoek tekst die begint met specifieke letters
We kunnen elke tekstwaarde zoeken die begint met specifieke letters door de bovenstaande stappen te volgen. We hoeven echter alleen LookAtType in te stellen als “START_WITH”. Het volgende codevoorbeeld laat zien hoe u met Java een specifieke tekst kunt vinden die begint met de letter “H” in een Excel-bestand.
// Load an Excel file | |
Workbook workbook = new Workbook("C:\\Files\\Cells\\sample.xlsx"); | |
// Access the first worksheet in the Excel file | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Get all the cells in CellCollections | |
Cells cells = worksheet.getCells(); | |
// Initialize FindOptions | |
FindOptions findOptions = new FindOptions(); | |
// Find the cell containing a string value | |
findOptions.setLookAtType(LookAtType.START_WITH); | |
Cell cell = cells.find("H", null, findOptions); | |
// Show the cell name and its value | |
System.out.println("Name of the cell containing String: " + cell.getName()); | |
System.out.println("the cell value is: " + cell.getValue()); |

Zoek tekst die eindigt op specifieke letters
We kunnen elke tekstwaarde zoeken die eindigt met specifieke letters door de bovenstaande stappen te volgen. We hoeven echter alleen LookAtType in te stellen als ‘END_WITH’. Het volgende codevoorbeeld laat zien hoe u met Java een specifieke tekst kunt vinden die eindigt op de letters “any” in een Excel-bestand.
// Load an Excel file | |
Workbook workbook = new Workbook("C:\\Files\\Cells\\sample.xlsx"); | |
// Access the first worksheet in the Excel file | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Get all the cells in CellCollections | |
Cells cells = worksheet.getCells(); | |
// Initialize FindOptions | |
FindOptions findOptions = new FindOptions(); | |
// Find the cell containing a string value | |
findOptions.setLookAtType(LookAtType.ENDS_WITH); | |
Cell cell = cells.find("any", null, findOptions); | |
// Show the cell name and its value | |
System.out.println("Name of the cell containing String: " + cell.getName()); | |
System.out.println("the cell value is: " + cell.getValue()); |
Zoek tekst met specifieke letters
We kunnen elke tekstwaarde doorzoeken die een specifieke subtekenreeks bevat door de bovenstaande stappen te volgen. We hoeven echter alleen LookAtType in te stellen als “CONTAINS”. Het volgende codevoorbeeld laat zien hoe u met Java een tekst kunt vinden die “comp” bevat in een Excel-bestand.
// Load an Excel file | |
Workbook workbook = new Workbook("C:\\Files\\Cells\\sample.xlsx"); | |
// Access the first worksheet in the Excel file | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Get all the cells in CellCollections | |
Cells cells = worksheet.getCells(); | |
// Initialize FindOptions | |
FindOptions findOptions = new FindOptions(); | |
// Find the cell containing a string value | |
findOptions.setLookAtType(LookAtType.CONTAINS); | |
Cell cell = cells.find("comp", null, findOptions); | |
// Show the cell name and its value | |
System.out.println("Name of the cell containing String: " + cell.getName()); | |
System.out.println("the cell value is: " + cell.getValue()); |
Zoeken met reguliere expressie in Excel met behulp van Java
We kunnen ook een celwaarde doorzoeken met behulp van reguliere expressies door de bovenstaande stappen te volgen. We hoeven echter alleen de Regex Key in te stellen op true en de LookAtType op “CONTAINS”. Het volgende codevoorbeeld laat zien hoe u tekst kunt vinden met behulp van een reguliere expressie in een Excel-bestand met Java.
// Load an Excel file | |
Workbook workbook = new Workbook("C:\\Files\\Cells\\sample.xlsx"); | |
// Access the first worksheet in the Excel file | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Get all the cells in CellCollections | |
Cells cells = worksheet.getCells(); | |
// Initialize FindOptions | |
FindOptions findOptions = new FindOptions(); | |
// Define it as Regex | |
findOptions.setRegexKey(true); | |
findOptions.setLookAtType(LookAtType.ENTIRE_CONTENT); | |
findOptions.setLookInType(LookInType.VALUES); | |
Cell cell = cells.find("[a-z]{1}[\\s]&[\\s][a-z]{1}", null, opt); | |
// Show the cell name and its value | |
System.out.println("Name of the cell containing String: " + cell.getName()); | |
System.out.println("the cell value is: " + cell.getValue()); |

Zoeken met reguliere expressie in Excel met behulp van Java.
Hoofdlettergevoelig zoeken in Excel met behulp van Java
We kunnen een zoekactie uitvoeren en elke hoofdlettergevoelige tekenreeks vinden door de bovenstaande stappen te volgen. We hoeven echter alleen de eigenschap CaseSensitive in te stellen op true. Het volgende codevoorbeeld laat zien hoe je hoofdlettergevoelige tekst kunt vinden in een Excel-bestand met Java**.
// Load an Excel file | |
Workbook workbook = new Workbook("C:\\Files\\Cells\\sample.xlsx"); | |
// Access the first worksheet in the Excel file | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Get all the cells in CellCollections | |
Cells cells = worksheet.getCells(); | |
// Initialize FindOptions | |
FindOptions findOptions = new FindOptions(); | |
// Find the cell containing a formula | |
findOptions.setCaseSensitive(true); | |
findOptions.setLookAtType(LookAtType.CONTAINS); | |
Cell cell = cells.find("Ltd", null, findOptions); | |
// Show the cell name and its value | |
System.out.println("Name of the cell containing String: " + cell.getName()); | |
System.out.println("the cell value is: " + cell.getValue()); |
Formule zoeken in Excel met behulp van Java
We kunnen een cel met een formule in een Excel-bestand doorzoeken door de onderstaande stappen te volgen:
- Laad eerst een Excel-bestand met de klasse [Workbook][13].
- Vervolgens toegang tot het eerste werkblad in het Excel-bestand.
- Haal vervolgens de cellen van het geopende blad op.
- Maak vervolgens een instantie van de klasse FindOptions.
- Stel daarna [LookInType][22] in als “FORMULAS”.
- Zoek tot slot de tekst met behulp van de methode Cells.find(). Er zijn zoekstring en FindOptions nodig als argumenten.
Het volgende codevoorbeeld laat zien hoe u met Java een formulecel in een Excel-bestand kunt vinden.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// Load an Excel file Workbook workbook = new Workbook("C:\\Files\\Cells\\sample.xlsx"); // Access the first worksheet in the Excel file Worksheet worksheet = workbook.getWorksheets().get(0); // Get all the cells in CellCollections Cells cells = worksheet.getCells(); // Initialize FindOptions FindOptions findOptions = new FindOptions(); // Find the cell containing a formula findOptions.setLookInType(LookInType.FORMULAS); Cell cell = cells.find("=SUM(C2:C10)", null, findOptions); // Show the cell name and its value System.out.println("Name of the cell containing String: " + cell.getName()); System.out.println("the cell value is: " + cell.getValue()); Formule zoeken in Excel met behulp van Java.
Ontvang een gratis licentie
Probeer de API zonder evaluatiebeperkingen door [een gratis tijdelijke licentie][24] aan te vragen.
Conclusie
In dit artikel hebben we geleerd tekst of formules in Excel te vinden met Java. We hebben met name geleerd hoe we elke tekst in Excel kunnen doorzoeken die begint, eindigt of specifieke letters bevat programmatisch. We hebben ook gezien hoe te zoeken met reguliere expressies in Excel-bestanden. Bovendien kunt u meer leren over Aspose.Cells voor Java API met behulp van de [documentatie][25]. Neem bij onduidelijkheden gerust contact met ons op via het [forum][26].
Zie ook
- [Tekst zoeken en vervangen in Excel in Java][27] [1]: https://blog.conholdate.com/wp-content/uploads/sites/27/2022/02/search-data-in-excel-using-java.jpg [2]: #installation [3]: #Find-Specific-Text-in-Excel-using-Java [4]: #Find-Text-Starting-with-Specific-Letters [5]: #Search-Text-Ending-with-Specific-Letters [6]: #Find-Text-Containing-Specific-Letters [7]: #Search-with-Regular-Expression-in-Excel-using-Java [8]: #Case-Sensitive-Search-in-Excel-using-Java [9]: #Search-Formula-in-Excel-using-Java [10]: https://products.aspose.com/cells/java/ [11]: https://docs.fileformat.com/spreadsheet/xlsx/ [12]: https://releases.aspose.com/cells/java/ [13]: https://apireference.aspose.com/cells/java/com.aspose.cells/workbook [14]: https://reference.aspose.com/cells/java/com.aspose.cells/worksheet/ [15]: https://apireference.aspose.com/cells/java/com.aspose.cells/Cells [16]: https://apireference.aspose.com/cells/java/com.aspose.cells/FindOptions [17]: https://apireference.aspose.com/cells/java/com.aspose.cells/LookAtType [18]: https://apireference.aspose.com/cells/java/com.aspose.cells/cells#find(java.lang.Object,%20com.aspose.cells.Cell,%20com.aspose.cells.FindOptions) [19]: https://blog.conholdate.com/wp-content/uploads/sites/27/2022/02/Find-Specific-Text-in-Excel-using-Java.jpg [20]: https://blog.conholdate.com/wp-content/uploads/sites/27/2022/02/Find-Text-Starting-with-Specific-Letters.jpg [21]: https://blog.conholdate.com/wp-content/uploads/sites/27/2022/02/Search-with-Regular-Expression-in-Excel-using-Java.jpg [22]: https://apireference.aspose.com/cells/java/com.aspose.cells/LookInType [23]: https://blog.conholdate.com/wp-content/uploads/sites/27/2022/02/Search-Formula-in-Excel-using-Java.jpg [24]: https://purchase.conholdate.com/temporary-license [25]: https://docs.aspose.com/cells/java/ [26]: https://forum.aspose.com/c/cells/9 [27]: https://blog.aspose.com/2020/12/08/find-and-replace-text-in-spreadsheets-using-java/