c# - 如何使用 C# 将 .Find() 方法与 like 表达式一起使用

标签 c# excel .net-3.5

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

例子

excel 文件如下所示:

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

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

更新

这里有更多的解释

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

如果我找到 B VALUE2 C VALUE2

B值2

C值2

所有这些先前的字符串案例都将被视为匹配.. 我无法连接两个字符串,因为它会忽略最后两个匹配项

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

    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);

最佳答案

如果您正在寻找 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;
    }
}

一般来说,如果你想模仿 Like来自 SQL,然后是 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

关于c# - 如何使用 C# 将 .Find() 方法与 like 表达式一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48901724/

相关文章:

c# - 为应用程序创建更新系统的方向和意见

c# - Type.IsSubclassOf 的行为不符合预期

c# - 创建规则以对参数属性强制执行默认值

c# - 从接收邮件服务器 (POP) 读取电子邮件

从另一个类调用方法后,C# 设置 Form Parent

java - 比较两个excel文件中的数据并在第三个文件中写入相应的映射

vba - 一个程序将返回一行中最小值的单元格地址?

c# - 捕获 Windows 窗体上的 Enter 键按下不起作用

vba - 如何查找和编辑 VBA 变量所引用的命名范围?

c# - 如何以编程方式在 DataTable 上使用 Telerik RadGrid FilterExpression