/// <summary>
/// 查找替换关键字
/// </summary>
/// <param name="fileName">文件名</param>
private void FindWord(string fileName)
{
Excel.Application ep = new Excel.ApplicationClass();
try
{
object MissingValue = Type.Missing;
string strKeyWord = txtFindWord.Text.Trim(); //指定要搜索的文本,如果有多个,则声明string[]
Excel.Workbook ew = ep.Workbooks.Open(fileName, MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue);
Excel.Worksheet ews;
int iEWSCnt = ew.Worksheets.Count;
Excel.Range oRange;
object oText = strKeyWord.Trim().ToUpper();
for (int i = 1; i <= iEWSCnt; i++)
{
ews = null;
ews = (Excel.Worksheet)ew.Worksheets[i];
oRange = null;
oRange = ((Excel.Range)ews.UsedRange).Find(
oText, MissingValue, MissingValue,
MissingValue, MissingValue, Excel.XlSearchDirection.xlNext,
MissingValue, MissingValue, MissingValue);
if (oRange != null && oRange.Cells.Rows.Count >= 1 && oRange.Cells.Columns.Count >= 1)
{
oRange.Replace(oText, txtReplace.Text, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue);
ew.Save();
}
}
ep.Quit();
}
catch
{
ep.Quit();
}
}