Search in Excel
GemBox.Spreadsheet for Java has been retired
Following example shows how to search text in a spreadsheet file. It also shows how to use CellRange.iterator
for iterating over range of cells.

import com.gembox.spreadsheet.*;
class Program {
public static void main(String[] args) throws java.io.IOException {
// If using the Professional version, put your serial key below.
SpreadsheetInfo.setLicense("FREE-LIMITED-KEY");
ExcelFile workbook = ExcelFile.load("%InputFileName%");
String searchText = "Apollo 13";
ExcelWorksheet worksheet = workbook.getWorksheet(0);
StringBuilder sb = new StringBuilder();
RowColumn position = worksheet.getCells().findText(searchText, false, false);
if (position == null) {
sb.append("Can't find text.\n");
} else {
sb.append(searchText + " was launched on " + worksheet.getCell(position.getRow(), 2).getValue() + ".\n");
if (worksheet.getCell(position.getRow(), 1).getValue() instanceof String) {
String nationality = (String) worksheet.getCell(position.getRow(), 1).getValue();
String nationalityText = nationality.trim().toLowerCase();
int nationalityCounter = 0;
java.util.Iterator<ExcelCell> iterator = worksheet.getColumn(1).getCells().getReadIterator();
while (iterator.hasNext()) {
ExcelCell cell = iterator.next();
if (cell.getValue() instanceof String && ((String) cell.getValue()).trim().toLowerCase().equals(nationalityText))
nationalityCounter++;
}
sb.append(String.format("There are %1$s entries for %2$s.", nationalityCounter, nationality));
}
}
System.out.println(sb.toString());
}
}
See also
Next steps
Published: December 13, 2018 | Modified: December 19, 2022 | Author: Marek Turis