データの検索または検索
指定されたデータを含むセルの検索
Microsoft Excel の使用
Microsoft Excelでは、ワークシート内の含まれる指定されたデータを持つセルを検索できます。Microsoft Excelの検索メニューから編集を選択すると、検索値を指定できるダイアログが表示されます。
ここでは、「オレンジ」の値を検索しています。Aspose.Cellsでは、指定された値を含むワークシート内のセルを検索できます。
Aspose.Cellsの使用
Aspose.Cellsは、Microsoft Excelファイルを表すWorkbookクラスを提供します。Workbookクラスには、Excelファイル内の各ワークシートにアクセスできるWorksheetsコレクションが含まれています。ワークシートはWorksheetクラスで表されます。Worksheetクラスにはワークシート内のすべてのセルを表すCellsコレクションが含まれており、Cellsコレクションにはユーザー指定データを含むワークシート内のセルを検索するためのいくつかのメソッドが提供されています。これらのメソッドのいくつかについて詳しく説明します。
指定された数式を含むセルの検索
開発者は、CellsコレクションのFindメソッドを呼び出すことによって、ワークシート内で指定された数式を検索できます。通常、Findメソッドは3つのパラメータを受け入れます。
- オブジェクト: 検索するオブジェクト。型はint、double、DateTime、string、boolである必要があります。
- 前のセル: 同じオブジェクトを持つ前のセル。最初から検索する場合は、このパラメーターをnullに設定できます。
- FindOptions: 必要なオブジェクトを見つけるためのオプション。
以下の例では、検索メソッドの練習にワークシートデータを使用します:
// 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); |
FindOptionsを使用したデータまたは式の検索
異なるFindOptionsを使用して、CellsのFindメソッドを使用して指定した値を検索することが可能です。通常、Findメソッドは次のパラメータを受け入れます:
- 検索値、検索するデータまたは値。
- 前のセル、同じ値を含んでいた最後のセル。最初から検索する場合は、このパラメーターをnullに設定できます。
- 検索オプション、検索オプション。
// 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 "); | |
} |
指定された文字列値を見つけることが可能です。異なる{2}を持つ{1}コレクション内に見つかった{0}メソッドを呼び出すことで。
異なるFindOptionsを持つCellsコレクションの中で見つかったFindメソッドを呼び出すことで、指定された文字列値を見つけることが可能です。
FindOptions.LookInTypeとFindOptions.LookAtTypeプロパティを指定します。次の例コードは、これらのプロパティを使用してセル内の文字列の先頭、または中央、または末尾にさまざまな数の文字列を探す方法を示しています。
// 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 "); | |
} |