查找或搜索数据
查找包含指定数据的单元格
使用Microsoft Excel
Microsoft Excel 允许用户在工作表中查找包含指定数据的单元格。如果在 Microsoft Excel 中选择 查找 菜单中的 编辑,你会看到一个对话框,可以在其中指定搜索值。
在这里,我们正在查找值"橙子"。 Aspose.Cells 还允许开发人员查找工作表中包含指定值的单元格。
使用 Aspose.Cells for Node.js via C++
Aspose.Cells 提供一个类 Workbook,代表一个 Microsoft Excel 文件。Workbook 类包含一个 Workbook.getWorksheets() 集合,用于访问 Excel 文件中的每个工作表。工作表由 Worksheet 类表示。Worksheet 类提供一个 getCells() 集合,表示工作表中的所有单元格。Cells 集合提供多种方法,用于查找包含用户指定数据的单元格。以下部分更详细地介绍了这些方法中的一些。
查找包含公式的单元格
开发者可以通过调用 Cells 集合的 find 方法,在工作表中查找指定的公式。通常,find 方法接受三个参数:
- 对象: 要搜索的对象。类型应为 int、double、DateTime、string 或 bool。
- 前一个单元格: 具有相同对象的前一个单元格。如果从头开始搜索,可以将此参数设为 null。
- 查找选项: 查找所需对象的选项。
以下示例使用工作表数据来练习查找方法:
const AsposeCells = require("aspose.cells.node"); | |
const path = require("path"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Opening the Excel file | |
const workbook = new AsposeCells.Workbook(path.join(dataDir, "sampleFindingCellsContainingFormula.xlsx")); | |
// Accessing the first worksheet in the Excel file | |
const worksheet = workbook.getWorksheets().get(0); | |
// Instantiate FindOptions Object | |
const findOptions = new AsposeCells.FindOptions(); | |
findOptions.setLookInType(AsposeCells.LookInType.Formulas); | |
// Finding the cell containing the specified formula | |
const cell = worksheet.getCells().find("=SUM(A5:A10)", null, findOptions); | |
// Printing the name of the cell found after searching worksheet | |
console.log("Name of the cell containing formula: " + cell.getName()); |
使用 FindOptions 查找数据或公式
可以使用 Cells 集合的 Cells.find(object, Cell) 方法,结合各种 FindOptions,查找指定的值。通常,find 方法接受以下参数:
- 搜索值,要搜索的数据或值。
- 前一个单元格,上一个包含相同值的单元格。如果从开头开始搜索,可以将此参数设置为null。
- 查找选项,查找选项。
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const sourceDir = path.join(__dirname, "data"); | |
// Instantiate the workbook object | |
const workbook = new AsposeCells.Workbook(sourceDir + "sampleFindingDataOrFormulasUsingFindOptions.xlsx"); | |
workbook.calculateFormula(); | |
// Get Cells collection | |
const cells = workbook.getWorksheets().get(0).getCells(); | |
// Instantiate FindOptions Object | |
const findOptions = new AsposeCells.FindOptions(); | |
// Create a Cells Area | |
const ca = new AsposeCells.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.setSearchBackward(false); | |
findOptions.setSearchOrderByRows(true); | |
// Set the lookintype, you may specify, values, formulas, comments etc. | |
findOptions.setLookInType(AsposeCells.LookInType.Values); | |
// Set the lookattype, you may specify Match entire content, endswith, starwith etc. | |
findOptions.setLookAtType(AsposeCells.LookAtType.EntireContent); | |
// Find the cell with value | |
const cell = cells.find(341, null, findOptions); | |
if (cell !== null) { | |
console.log("Name of the cell containing the value: " + cell.getName()); | |
} else { | |
console.log("Record not found "); | |
} |
查找包含指定字符串值或数字的单元格
也可以调用同在 Cells 集合中的 find 方法,结合各种 FindOptions,查找指定的字符串值。
指定 FindOptions.setLookInType 和 FindOptions.setLookAtType 属性。以下示例代码演示了如何使用这些属性查找在单元格字符串的开头、中间或结尾的各种字符串。
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Instantiate the workbook object | |
const workbook = new AsposeCells.Workbook(path.join(dataDir, "book1.xls")); | |
// Get Cells collection | |
const cells = workbook.getWorksheets().get(0).getCells(); | |
const opts = new AsposeCells.FindOptions(); | |
opts.setLookInType(AsposeCells.LookInType.Values); | |
opts.setLookAtType(AsposeCells.LookAtType.EntireContent); | |
// Find the cell with the input integer or double | |
let cell1 = cells.find(205, null, opts); | |
if (cell1 !== null) { | |
console.log("Name of the cell containing the value: " + cell1.getName()); | |
} else { | |
console.log("Record not found "); | |
} | |
// Find the cell with the input string | |
let cell2 = cells.find("Items A", null, opts); | |
if (cell2 !== null) { | |
console.log("Name of the cell containing the value: " + cell2.getName()); | |
} else { | |
console.log("Record not found "); | |
} | |
// Find the cell containing the input string | |
opts.setLookAtType(AsposeCells.LookAtType.Contains); | |
let cell3 = cells.find("Data", null, opts); | |
if (cell3 !== null) { | |
console.log("Name of the cell containing the value: " + cell3.getName()); | |
} else { | |
console.log("Record not found "); | |
} |