C#中的EXCEL的查找操作

最重要的是,range.find之后还要用range.findnext,就可以遍历所有的查找到的数据
代码如下:
using Microsoft.Office.Interop.Excel;
Application myExcel = new Application(); //开启EXCEL 服务
             object missing = System.Reflection.Missing.Value;
             myExcel.Application.Workbooks.Open(strExcelPath, missing, missing, missing, missing, missing, missing,
                 missing, missing, missing, missing, missing, missing, missing, missing);
             Workbook myBook = myExcel.Workbooks[1]; //获取工作簿
             _Worksheet sheet = (Worksheet)myBook.Sheets[ "RP750PDF" ]; //获取sheet
             int iCnt = sheet.UsedRange.Count;
             Excel.Range rgUsed, rgFound;
             rgUsed = sheet.get_Range( "A2:A10" ); //获取a2-10范围的梅子
             rgFound = (Excel.Range)rgUsed.Find( "RP757c-" , Type.Missing, //“RP757C-”即是要查找的字符串
                 Excel.XlFindLookIn.xlValues, //查找值,或者xlFormulas查找公式等
                 Excel.XlLookAt.xlPart, //这里用xlWhole返回的一直是空指针
                 Excel.XlSearchOrder.xlByRows, //按行查找
                 Excel.XlSearchDirection.xlNext, //建议就用xlNext
false , false );
             Range rgTmp = rgFound;

            do
            {
                var item = rgTmp.get_Value();
                int iRowNum = rgTmp.Row;
                int iColNum = rgTmp.Column;
                rgTmp = rgUsed.FindNext(rgTmp);
            } while (rgTmp.Address != rgFound.Address);//记录第一个找到的range地址,当下一个单元格的地址与第一个相同,则跳出。

你可能感兴趣的:(C#,excel,c#,开发语言)