Search in Excel Using C# and VB.NET

The following example shows how you can search text in a spreadsheet file with C# and VB.NET using the CellRange.FindText method.

The example will display all the occurrences of searched text in the targeted Excel column.

Excel search with GemBox.Spreadsheet
Screenshot of Excel search with GemBox.Spreadsheet
Upload your file (Drag file here)
using System;
using GemBox.Spreadsheet;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        var workbook = ExcelFile.Load("%InputFileName%");
        var worksheet = workbook.Worksheets[0];

        var searchText = "Apollo";
        var range = worksheet.Columns[0].Cells;

        while (range.FindText(searchText, out int row, out int column))
        {
            var cell = worksheet.Cells[row, column];
            Console.WriteLine($"Text was found in cell '{cell.Name}' (\"{cell.StringValue}\").");

            range = range.GetSubrangeAbsolute(row + 1, 0, worksheet.Rows.Count, 0);
        }
    }
}
Imports System
Imports GemBox.Spreadsheet

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        Dim workbook = ExcelFile.Load("%InputFileName%")
        Dim worksheet = workbook.Worksheets(0)

        Dim searchText = "Apollo"
        Dim range = worksheet.Columns(0).Cells

        Dim row As Integer, column As Integer
        While range.FindText(searchText, row, column)

            Dim cell = worksheet.Cells(row, column)
            Console.WriteLine($"Text was found in cell '{cell.Name}' (""{cell.StringValue}"").")

            range = range.GetSubrangeAbsolute(row + 1, 0, worksheet.Rows.Count, 0)
        End While

    End Sub
End Module

See also


Next steps

GemBox.Spreadsheet is a .NET component that enables you to read, write, edit, convert, and print spreadsheet files from your .NET applications using one simple API.

Download Buy