简体   繁体   中英

Excel Find operation in C# will skip the first row

Below is a working code. I figured out that in the find criteria that by changing the starting point of the search criteria as: wsSheet.Cells[65000, columnNumber] it will solve the issue. This is not a real solution though.

Is there another way of forcing the Find operation to start at the first row of the excel sheet?

public static string FindFirstStringInColumn(Excel.Workbook wbBook, string strSheet, string strSearchMe, string strColumnLetter)

    {
        Excel.Worksheet wsSheet = null;
        wsSheet = (Excel.Worksheet)wbBook.Worksheets[strSheet];
        Excel.Range currentFind = null;


        Excel.Range range = (Excel.Range)wsSheet.Columns[strColumnLetter, Type.Missing];
        int columnNumber = ExcelCOM.GetColumnNumber(strColumnLetter);
        //~~> Search for strSearchMe
        currentFind = range.Find(strSearchMe,
                        wsSheet.Cells[65000, columnNumber],
                        Excel.XlFindLookIn.xlValues,
                        Excel.XlLookAt.xlPart,
                        Excel.XlSearchOrder.xlByRows,
                        Excel.XlSearchDirection.xlNext,
                        false,
                        false,
                        false);

        if (currentFind != null)
        {
            string sAddress = currentFind.get_Address(false, false, Excel.XlReferenceStyle.xlA1, false, false);
            //~~> Display the found Address
            return (sAddress).ToString();
        }
        else
        {
            return ("Nothing found");
        }

    }

该参数是可选的,请使用命名参数而不指定它,它应以该范围内的“最高”单元格开头。

From what I've been able to gather, try specifying

range.Cells[range.Count]

as your "After" parameter value, which at least gets rid of the hardcoded 65000, and should work correctly for ranges other than entire columns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM