データの検索または検索
Microsoft Excel では、ユーザーは特定のデータを含むセルを検索できます。たとえば、編集その後探す検索ダイアログを開きます。ユーザーが値を入力してクリックわかったそれを検索します。 Excel で一致するフィールドが強調表示されます。
検索ダイアログを使用して特定の値を含むセルを検索する
この例では、検索値は「オレンジ」です。
Aspose.Cells を使用すると、開発者はワークシート内のセルを検索して、特定の値を含むセルを見つけることができます。
特定のデータを含む Cells の検索
Aspose.Cells はクラスを提供し、ワークブック、Excel ファイルを表します。のワークブッククラスが含まれていますワークシート コレクション、Excel ファイル内の各ワークシートへのアクセスを許可するコレクション。ワークシートは、ワークシートクラス。
のワークシートクラスが提供するCells、ワークシート内のすべてのセルを表すコレクション。Cellscollection には、ユーザー指定のデータを含むワークシート内のセルを検索するためのメソッドがいくつか用意されています。これらの方法のいくつかについて、以下で詳しく説明します。
すべての検索メソッドは、指定された検索値を含むセルのセル参照を返します。
数式を含む検索
開発者は、を呼び出してワークシート内の指定された数式を見つけることができます。Cellsコレクションの[探す](https://reference.aspose.com/cells/java/com.aspose.cells/cells#find(java.lang.Object,%20com.aspose.cells.Cell) メソッド、設定FindOptions.setLookInTypeにLookInType.FORMULASにパラメータとして渡します。[探す](https://reference.aspose.com/cells/java/com.aspose.cells/cells#find(java.lang.Object,%20com.aspose.cells.Cell)) 方法。
通常、探す メソッドは 2 つ以上のパラメーターを受け入れます。
- 検索するオブジェクト: ワークシートで検索する必要があるオブジェクトを表します。
- 前の Cell: は、同じ数式を持つ前のセルを表します。最初から検索する場合、このパラメーターを null に設定できます。
- 検索オプション: 検索条件を表します。以下の例では、次のワークシート データを使用してメソッドの検索を練習します。
サンプル ワークシート データ
// 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()); |
文字列の検索
文字列値を含むセルの検索は、簡単かつ柔軟です。検索にはさまざまな方法があります。たとえば、特定の文字または文字セットで始まる文字列を含むセルを検索します。
特定の文字で始まる文字列を検索する
文字列の最初の文字を検索するには、Cellsコレクションの探す メソッドで、FindOptions.setLookAtTypeにLookAtType.START_WITHにパラメータとして渡します。[探す](https://reference.aspose.com/cells/java/com.aspose.cells/cells#find(java.lang.Object,%20com.aspose.cells.Cell)) 方法。
// 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()); |
特定の文字で終わる文字列を検索する
Aspose.Cells は、特定の文字で終わる文字列も検索できます。文字列の最後の文字を検索するには、Cellsコレクションの探す メソッドで、FindOptions.setLookAtTypeにLookAtType.END_WITHにパラメータとして渡します。[探す](https://reference.aspose.com/cells/java/com.aspose.cells/cells#find(java.lang.Object,%20com.aspose.cells.Cell)) 方法。
// 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()); |
正規表現による検索: RegEx 機能
正規表現は、特定の文字、単語、またはパターンなどのテキストの文字列を照合 (指定および認識) するための簡潔で柔軟な手段を提供します。
たとえば、正規表現パターン abc-* ~~xyz~~ は、文字列「abc-123-xyz」、「abc-985-xyz」、および「abc-pony-xyz」に一致します。*はワイルドカードであるため、途中の文字に関係なく、パターンは「abc」で始まり「-xyz」で終わるすべての文字列に一致します。
Aspose.Cells は正規表現で検索できます。
// 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); |