查找或搜索数据
Contents
[
Hide
]
Microsoft Excel 允许用户在包含指定数据的工作表中查找单元格。
查找包含指定数据的 Cells
使用 Microsoft Excel
Microsoft Excel 允许用户在包含指定数据的工作表中查找单元格。如果你选择编辑来自寻找Microsoft Excel 中的菜单,您将看到一个对话框,您可以在其中指定搜索值。
在这里,我们正在寻找值“Oranges”。 Aspose.Cells 还允许开发人员在工作表中查找包含指定值的单元格。
使用 Aspose.Cells
Aspose.Cells提供了一个类,工作簿,代表一个 Microsoft Excel 文件。这工作簿类包含一个工作表允许访问 Excel 文件中每个工作表的集合。工作表由工作表班级。这工作表类提供了Cells代表工作表中所有单元格的集合。这Cellscollection 提供了多种方法来查找包含用户指定数据的工作表中的单元格。下面将更详细地讨论其中一些方法。
所有 Find 方法都返回包含要搜索的指定数据的单元格的引用。
查找包含公式的 Cells
开发者可以通过调用Cells收藏的寻找方法。通常,寻找方法接受三个参数:
- **目的:**要搜索的对象。类型应为 int、double、DateTime、string、bool。
- **上一个单元格:**具有相同对象的上一个单元格。如果从头开始搜索,这个参数可以设置为空。
- FindOptions:用于查找所需对象的选项。
下面的示例使用工作表数据来练习查找方法:
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
// 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 查找数据或公式
可以使用Cells收藏的寻找方法与各种查找选项.通常,寻找方法接受以下参数:
- 搜索值要搜索的数据或值。
- 上一个单元格,包含相同值的最后一个单元格。从头开始搜索时可以将此参数设置为空。
- 查找选项查找选项。
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
// 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 "); | |
} |
查找包含指定字符串值或数字的 Cells
可以通过调用相同的方法来查找指定的字符串值寻找中找到的方法Cells收集各种查找选项.
指定查找选项.LookInType和查找选项.LookAtType特性。下面的示例代码说明了如何使用这些属性来查找具有不同数量字符串的单元格开始或在中央或在结尾单元格的字符串。
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
// 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 "); | |
} |