Search in Excel

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.

Excel search with GemBox.Spreadsheet
Screenshot of Excel search with GemBox.Spreadsheet
Upload your file (Drag file here)
import com.gembox.spreadsheet.*;

class Program {

    public static void main(String[] args) throws java.io.IOException {
        // If using 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());
    }
}

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Spreadsheet for Java yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.