日期:2014-05-17  浏览次数:20992 次

C#t替换EXCEL字符串报错问题

xSheet.Cells.Replace(what, replace);

找到替换的会执行,找不到就会报错,有没有像WORD里的  WORD.wdreplace.replaceAll()这样的函数,这样WORD就不会报错

------解决方案--------------------
        /// <summary>
        /// 替换excel字符
        /// </summary>
        /// <param name="strOldText"></param>
        /// <param name="strNewText"></param>
        public void ExcelReplace( string strOldText, string strNewText)
        {

            int icount = wb.Sheets.Count;
                object what = strOldText; //查找字符串 

                object retxt = strNewText; //替换字符串      

                if (xSheet.Cells.Find(strOldText, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                    XlSearchDirection.xlNext, Missing.Value, Missing.Value) != null)
                {
                    for (int i = 1; i <= icount; i++)
                    {
                        xSheet = (Worksheet)wb.Sheets[i];
                    }

                    xSheet.Cells.Replace(what, retxt, Missing.Value,

                            Missing.Value, Missing.Value, Missing.Value,

                            Missing.Value, Missing.Value);
                }
        }