bool rep = xlWorkSheet.Cells.Replace(textBox1.Text,
textBox2.Text,
Excel.XlLookAt.xlWhole,
Excel.XlSearchOrder.xlByRows,
false,
Type.Missing,
Type.Missing,
Type.Missing);
答案 0 :(得分:1)
你不能这样做。如果参数正确,则Range的Replace方法始终返回true。我知道你唯一可以做的就是首先查找文本的出现次数,然后替换它。绝对没有优化,但我想不出别的东西。
bool rep = xlWorkSheet.Cells.Find(textBox1.Text,
Type.Missing,
Type.Missing,
Excel.XlLookAt.xlWhole,
Excel.XlSearchOrder.xlByRows,
Type.Missing,
false,
Type.Missing,
Type.Missing) != null;
if (rep)
xlWorkSheet.Cells.Replace(textBox1.Text,
textBox2.Text,
Excel.XlLookAt.xlWhole,
Excel.XlSearchOrder.xlByRows,
false,
Type.Missing,
Type.Missing,
Type.Missing);
答案 1 :(得分:1)
以下代码可以正常使用:
Excel.Range Range = xlWorkSheet.UsedRange;
currentFind = Range .Find(textBox1.Text, Type.Missing,Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByRows,Excel.XlSearchDirection.xlNext,false,
Type.Missing, Type.Missing);
if (currentFind!=null)
{
SheetsArray.Add(xlWorkSheet.Name);
}