如何使用带有类似表达式的.Find()方法使用c#

问题描述 投票:10回答:2

在类似表达式的excel文件范围内查找字符串

文件excel如下所示:

----------------------------------------------------------
 # |     A     |      B      |      C      |      D      |
----------------------------------------------------------
 1 | A VALUE1  |   B VALUE1  |   C VALUE1  |   D VALUE1  |
----------------------------------------------------------
 2 | A VALUE2  |   B VALUE2  |   C VALUE2  |   D VALUE2  |
----------------------------------------------------------

现在我要做的是在B VALUE2 C VALUE2中输入这个字符串TB_Search_Text.Text来搜索它

UPDATE

这里有一些案例的解释

第二个字符串值C VALUE2可能存在或不存在我的意思

如果我一起找到B VALUE2C VALUE2

B VALUE2

C VALUE2

所有这些以前的字符串情况都将被视为匹配..我无法连接两个字符串,因为它将忽略最后两个匹配

对于下面的方法,它将返回未找到的字符串,那么我该怎么做才能使它工作?

    Microsoft.Office.Interop.Excel.Application oXL = new Microsoft.Office.Interop.Excel.Application();
    Microsoft.Office.Interop.Excel.Workbook oWB;
    Microsoft.Office.Interop.Excel.Range currentFind = null;
    Microsoft.Office.Interop.Excel.Range firstFind = null;

    Excel.Range oRng = oXL.get_Range("A1", "XFD1048576");

    currentFind = oRng.Find(TB_Search_Text.Text,
                            missing,
                            Excel.XlFindLookIn.xlValues,
                            Excel.XlLookAt.xlPart,
                            Excel.XlSearchOrder.xlByRows,
                            Excel.XlSearchDirection.xlNext,
                            false,
                            missing,
                            missing);
c# excel .net-3.5
2个回答
2
投票

如果您正在寻找3个选项中的任何一个 - 连接或单个值,您可以尝试以下方法:

  • 从工作簿中读取这两个值,并将它们写入C#中的列表。 (在下面的代码中我硬编码了)
  • 然后在列表中循环,直到找不到内容或列表为空。这是循环的条件:

while (currentFind == null & cnt < lookForList.Count)

  • 最后打印行和列,看看你找到了什么。

using System;
using System.Collections.Generic;
using Excel = Microsoft.Office.Interop.Excel;

class StartUp
{
    static void Main()
    {
        Excel.Application excel = null;
        excel = new Excel.Application();
        excel.Visible = true;        
        string filePath = @"C:\YourOwnPath\TestWB.xlsx";
        Excel.Workbook wkb = null;
        wkb = Open(excel, filePath);

        string part1 = "some value";
        string part2 = "some other value";
        string part12 = string.Concat(part1, part2);
        List<string> lookForList = new List<string> { part1, part2, part12 };
        Excel.Range currentFind = null;
        Excel.Range searchedRange = excel.get_Range("A1", "XFD1048576");
        int cnt = 0;
        while (currentFind == null & cnt < lookForList.Count)
        {
            //make sure to specify all the parameters you need in .Find()
            currentFind = searchedRange.Find(lookForList[cnt]);
            cnt++;
        }
        if (currentFind!=null)
        {
            Console.WriteLine("Found:");
            Console.WriteLine(currentFind.Column);
            Console.WriteLine(currentFind.Row);
        }        
        wkb.Close(true);
        excel.Quit();
    }

    public static Excel.Workbook Open(Excel.Application excelInstance, 
                            string fileName, bool readOnly = false, bool editable = true, 
                            bool updateLinks = true)
    {
        Excel.Workbook book = excelInstance.Workbooks.Open(
            fileName, updateLinks, readOnly,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, editable, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing);
        return book;
    }
}

一般来说,如果你想模仿SQL中的Like,那么xlXlLookAt.xlPart就足够了。您甚至不需要连接您正在搜索的两个值。


如果你想用一些空间来寻找它们,那么连接它们看起来是个好主意:

string concatenated = string.Concat(oWB.Range["B2"].Value2, " ", oWB.Range["C2"].Value2)

要么

currentFind = oRng.Find(concatenated,
                                            missing,
                                            Excel.XlFindLookIn.xlValues,
                                            Excel.XlLookAt.xlPart,
                                            Excel.XlSearchOrder.xlByRows,
                                            Excel.XlSearchDirection.xlNext,
                                            false,
                                            missing,
                                            missing);

String Concat MSDN


0
投票

你可以改变上面的程序,将Range中的最后一个单元格信息改为下面提到的那个并尝试。

Application.get_Range(“A1”,“D2”);

您也可以查看给定How to: Programmatically Search for Text in Worksheet Ranges 的示例

© www.soinside.com 2019 - 2024. All rights reserved.