C# - 无法替换Excel文件中句子中的文本

时间:2016-12-21 13:39:59

标签: c# excel excel-interop

我写了一个代码来替换excel文件中的文本。但问题是,如果在句子中找到文本,则此代码不会替换文本。难道我做错了什么 ?在Word文档中,我可以通过将MatchWholeWord标志更改为false来解决此问题。这是我的代码:

 static void ReplaceTextInExcelFile1(string filename, string replace, string replacement)
        {
            object m = Type.Missing;

            // open excel.
            Excel.Application app = new Excel.Application();

            // open the workbook. 
            Workbook wb = app.Workbooks.Open(
                filename,
                m, false, m, m, m, m, m, m, m, m, m, m, m, m);

            // get the active worksheet. (Replace this if you need to.) 

            Worksheet ws = null;
            Range r = null;
            app.DisplayAlerts = false;

            for (int x = 0; x < wb.Worksheets.Count; x++)
            {
                ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[x + 1];

                // get the used range. 
                r = (Range)ws.UsedRange;

                // call the replace method to replace instances. 
                bool success = (bool)r.Replace(replace, replacement, XlLookAt.xlWhole, XlSearchOrder.xlByRows,false, m, m, m);
            }

            // save and close. 
            wb.Save();
            app.Quit();
            app = null;
        }

0 个答案:

没有答案