查找具有特定样式的单元格
Contents
[
Hide
]
有时,您需要找到具有某种特定样式的单元格。本文演示了如何使用 Microsoft Excel 以及 Aspose.Cells for Java API 来实现这一点。
使用 Microsoft Excel
这些是在 MS Excel 中搜索具有特定样式的单元格所需的步骤。
- 选择查找并选择在里面主页选项卡.
- 选择寻找.
- 点击选项如果高级选项不可见。
- 选择从 Cell 选择格式…来自格式落下。
- 选择具有要搜索的样式的单元格。
- 点击找到所有查找所有样式与所选单元格相似的单元格。
使用 Aspose.Cells for Java
Aspose.Cells for Java 提供在工作表中查找具有特定样式的单元格的功能。为此,API 提供FindOptions.setStyle() 方法财产。
示例代码
以下代码片段查找所有与 cell 具有相同样式的单元格A1并更改这些单元格内的文本。请查看源文件和输出文件的屏幕截图以分析示例代码的输出。
This file contains hidden or 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-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(FindCellsWithSpecificStyle.class); | |
Workbook workbook = new Workbook(dataDir + "TestBook.xlsx"); | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Access the style of cell A1 | |
Style style = worksheet.getCells().get("A1").getStyle(); | |
// Specify the style for searching | |
FindOptions options = new FindOptions(); | |
options.setStyle(style); | |
Cell nextCell = null; | |
do { | |
// Find the cell that has a style of cell A1 | |
nextCell = worksheet.getCells().find(null, nextCell, options); | |
if (nextCell == null) | |
break; | |
// Change the text of the cell | |
nextCell.putValue("Found"); | |
} while (true); | |
workbook.save(dataDir + "out.xlsx"); |
执行代码后,所有与单元格 A1 具有相同样式的单元格都会显示文本“Found”。
截图
**数字:**包含具有样式的单元格的源文件
这是由以下代码生成的输出文件。可以看到所有和of cell样式相同的cellA1有一个文本“找到”
数字:搜索后包含找到的单元格的输出文件A1风格