• C#操作Excel替换关键字 Johan


            /// <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();
                }
            }

  • 相关阅读:
    MYSQL判断某个表是否已经存在
    百度、雅虎、谷歌搜索引擎接口调用注意事项
    Codeigniter整合Tank Auth权限类库的教程
    短链接的生成算法
    自定义String
    运算符和结合性
    字符串类封装
    运算符重载
    数组类封装
    友元
  • 原文地址:https://www.cnblogs.com/contain/p/3026757.html
Copyright © 2020-2023  润新知