Finding a value in a specific column

Using regular Excel automation, the "find" command is flexible enough to allow me to look in a specific column. Aspose has a FindString() command on the Cells class, but it will look in all cells. Is there some way to write code to find a string in a specific column? If there were a way to get a Cells collection that represented all the cells of a particular column, I could do that, but I don't know if there is a way to do that.


Thanks!
Bob

Hi Bob,

Now Aspose.Excel doesn’t support your requested feature. I will add new FindString methods to serve your need.

@xxxbobxxx,
Aspose.Excel is discontinued and no more active now. It is replaced by the latest product Aspose.Cells that is much advanced and contains all the latest features available in different versions of MS Excel. You can find value in a specific column only to increase the performance. For this you need to use FindOptions object that allows setting multiple options to find data. One of the option used in this example is set to search values only, second option is to set the search for the entire value in the cell and next option is to set the search area that is the column in which data is to be searched. Here is the simplified code to elaborate this feature:

private static void SearchData()
{
    // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET

    // Instantiate the workbook object
    Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets[0];

    //Fill dummy data
    worksheet.Cells.ImportObjectArray(Enumerable.Range(1, 10).Cast<object>().ToArray(), 0, 0, true);
    worksheet.Cells.ImportObjectArray(Enumerable.Range(11, 30).Cast<object>().ToArray(), 0, 1, true);
    worksheet.Cells.ImportObjectArray(Enumerable.Range(41, 15).Cast<object>().ToArray(), 0, 2, true);
    //workbook.Save("output.xlsx");

    // Get Cells collection
    Cells cells = workbook.Worksheets[0].Cells;

    FindOptions opts = new FindOptions();
    opts.LookInType = LookInType.Values;
    opts.LookAtType = LookAtType.EntireContent;

    //Find value in first column
    opts.SetRange(GetCellAreaForColumn(cells, 0));
    // Find the cell with the input integer or double
    Cell cell = cells.Find(8, null, opts);

    if (cell != null)
        Console.WriteLine("Name of the cell containing the value: " + cell.Name);
    else
        Console.WriteLine("Record not found ");
            
    //Find value in second column
    opts.SetRange(GetCellAreaForColumn(cells, 1));

    // Find the cell with the input integer or double
    cell = cells.Find(28, null, opts);

    if (cell != null)
        Console.WriteLine("Name of the cell containing the value: " + cell.Name);
    else
        Console.WriteLine("Record not found ");
}
private static CellArea GetCellAreaForColumn(Cells cells, short ColumnNo)
{
    return new CellArea() { StartColumn = ColumnNo, EndColumn = ColumnNo, StartRow = 0, EndRow = cells.EndCellInColumn(ColumnNo).Row };
}

Here is the program output:

Name of the cell containing the value: A8
Name of the cell containing the value: B18

For more information about searching data refer to the following article:
Find or Search Data

Give a try to this new product by downloading the latest free trial version here:
Aspose.Cells for .NET(Latest version)

You may download a runnable solution here for detailed testing of this new product.