Find cells with specific style
Using Microsoft Excel
These are the steps required to search cells with specific styles in MS Excel.
- Select Find & Select in the Home Tab.
- Select Find.
- Click Options if advanced options are not visible.
- Select Choose Format From Cell… from the Format dropdown.
- Select the cell with the style that you want to search.
- Click Find All to find all the cells with style similar to your selected cell.
Using Aspose.Cells for Java
Aspose.Cells for Java provides the feature to find cells in worksheet with some specific style. For this, the API provides FindOptions.setStyle() property.
Sample Code
The following code snippet finds all the cells which have the same style as that of cell A1 and changes the text inside those cells. Please see the screenshot of the source and output files to analyze the output of the sample code.
// 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"); |
After the execution of code, all the cells that have the same style as of cell A1 will have a text “Found”.
Screenshots
Figure: Source file with cells having styles
Here is the output file generated by the following code. You can see all the cells that have the same style as of cell A1 has a text “Found”
Figure: Output file with found cells after searching by A1 style