使用类似表达式
在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
以搜索它
更新
这里有一些案例的更多解释
第二个字符串值C VALUE2
可能存在或不存在我的意思
如果我一起找到B VALUE2
和 C 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);
答案 0 :(得分:2)
如果您正在寻找3种optinon中的任何一种 - 连接或单个值,您可以尝试以下方法:
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);
答案 1 :(得分:0)
您可以更改上述程序,将Range中的最后一个单元格信息更改为下面提到的那个并尝试。
Application.get_Range(“A1”,“D2”);
您也可以查看给定How to: Programmatically Search for Text in Worksheet Ranges
的示例