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

  • 相关阅读:
    Vue axios 使用记录
    Vue CLI 项目创建
    Vue中的事件修饰符。
    js 中控制打印方向横向还是纵向。
    element ui 列表中删除只能删除最后一项
    文件上传formData上传之前查看对象里的值
    GridControl_gridView 单元格内容换行(wrap)
    SqlTest(2013-07-10)
    printPreviewControl1 打印预览
    避免button处理事件过程中 点击按钮触发事件的方法
  • 原文地址:https://www.cnblogs.com/contain/p/3026757.html
Copyright © 2020-2023  润新知