【发布时间】:2018-02-16 13:13:35
【问题描述】:
新手在这里寻求帮助。
我有这个有效的代码,但它只确认是否找到了文本框文本。
我一直在尝试对其进行调整,以在搜索 Excel 工作表时确认文本框文本是否与 Excel 单元格完全匹配。 请帮忙,因为我是新手,有点迷路了。
private void button1_Click(object sender, EventArgs e)
{
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(@"C:\pricefile.xlsx");
Excel.Worksheet xlWorkSheet = xlWorkBook.Worksheets["Sheet1"];
Excel.Range colRange = xlWorkSheet.Columns["A:A"];
string searchString = textBox1.Text;
Excel.Range resultRange = colRange.Find(
What: searchString,
LookIn: Excel.XlFindLookIn.xlValues,
LookAt: Excel.XlLookAt.xlPart,
SearchOrder: Excel.XlSearchOrder.xlByRows,
SearchDirection: Excel.XlSearchDirection.xlNext
);
if (resultRange is null)
{
MessageBox.Show("Did not find " + searchString + " in Price File");
}
else
{
MessageBox.Show("Found " + searchString + " in Price File");
}
xlWorkBook.Close(false);
xlApp.Quit();
}
任何帮助都会很棒。 谢谢。
【问题讨论】:
-
不就是将LookAt参数设置为Excel.XlLookAt.xlWhole的情况吗?
-
完美。太感谢了。如果生活中的一切都那么简单。 :)
-
我已将其发布为答案,因此您可以将其标记为对遇到相同问题的任何其他用户的正确。
标签: c# excel search textbox verify