C#, searching an Excel file by columns only

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ASPnewb1
    New Member
    • Mar 2008
    • 14

    #1

    C#, searching an Excel file by columns only

    Hello,

    I'm trying to execute a find command on my Excel obj to find if a value exists in only one column, not the whole worksheet, what I had before that works (if the whole worksheet is searched):

    *************** *************** *************** *************** *************** **********
    Microsoft.Offic e.Interop.Excel .Application ExcelObj = new Microsoft.Offic e.Interop.Excel .Application();

    Workbook theWorkbook = ExcelObj.Workbo oks.Open("C:\\D ocuments and Settings\\MarkW \\Desktop\\ZipC ode Mapping.xlsx", 0, true, 5, "", "", true, XlPlatform.xlWi ndows, "\t", false, false, 0, true, 1, 0);

    Sheets sheets = theWorkbook.Wor ksheets;
    Worksheet worksheet = (Worksheet)shee ts.get_Item(1);

    Range range = worksheet.Cells .Find(searchStr ing, worksheet.Cells[1,1], XlFindLookIn.xl Values, XlLookAt.xlPart , Missing.Value, XlSearchDirecti on.xlNext, false, Missing.Value, false);



    *************** *************** *************** *************** *************** **

    works good in finding what I want to search but finds the search string inside longer strings (that I don't need).

    For instance I'm trying to search column 2 (area codes) only for say "512". Since all the area codes are length of 3, this should be no problem, but searching the worksheet I find other strings/numbers that contain 512, but they are not what I need,

    temp solution is this:

    *************** *************** *************** *************** *************** *****
    string add = range.get_Addre ss(true, true, XlReferenceStyl e.xlA1, Missing.Value, Missing.Value);

    int row = range.Row;
    int column = range.Column;
    string[] location = new string[4];
    string value = ((Range)workshe et.Cells[row, column]).Value2.ToStri ng();

    if (value.Length == searchString.Le ngth)
    {

    // do what needs to be done

    }

    *************** *************** *************** *************** **************

    I'd rather search what column I need, than testing if the string is the same length.

    I've tried:


    Range range = worksheet.Cells .Find(searchStr ing, worksheet.Colum ns[1,1],

    but I'm not sure what needs to be in the indexes.

    thanks!

    -Mark
Working...