在 Excel 中使用 C# 代码搜索和替换范围内的数据

可能的使用场景

有时,您需要搜索并替换特定范围内的数据,忽略所需范围外的任何单元格值。 Aspose.Cells for Python via .NET 允许您将搜索限制为特定范围。 本文介绍了如何使用Python代码在Excel中搜索并替换范围内的数据。

如何使用Aspose.Cells for Python Excel库搜索和替换范围内的数据

Aspose.Cells for Python via .NET 提供了FindOptions.set_range()方法,用于在搜索数据时指定范围。 以下代码示例在范围内搜索和替换数据。

from aspose.cells import CellArea, FindOptions, LookAtType, LookInType, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
filePath = dataDir + "input.xlsx"
workbook = Workbook(filePath)
worksheet = workbook.worksheets[0]
# Specify the range where you want to search
# Here the range is E3:H6
area = CellArea.create_cell_area("E9", "H15")
# Specify Find options
opts = FindOptions()
opts.look_in_type = LookInType.VALUES
opts.look_at_type = LookAtType.ENTIRE_CONTENT
opts.set_range(area)
cell = None
while True:
cell = worksheet.cells.find("search", cell, opts)
if cell:
cell.put_value("replace")
else:
break
# Save the workbook
workbook.save(dataDir + "output.out.xlsx")