zoukankan      html  css  js  c++  java
  • c#读Word文件,最后彻底释放资源

    我顶 字号:

    private void button5_Click(object sender, System.EventArgs e)
        {
          object filename="D:\\nettest\\window\\serial\\a.doc"; //要打开的文档路径
          string strKey="正文"; //要搜索的文本
          object MissingValue=Type.Missing;
          object oMissing = System.Reflection.Missing.Value;

          Word.Application wp=new Word.ApplicationClass();
    Word.Document wd=wp.Documents.Open(ref filename,ref MissingValue,
            ref MissingValue,ref MissingValue,
            ref MissingValue,ref MissingValue,
            ref MissingValue,ref MissingValue,
            ref MissingValue,ref MissingValue,
            ref MissingValue,ref MissingValue,
            ref MissingValue,ref MissingValue,
            ref MissingValue,ref MissingValue);

          if (wd.Content.Text.IndexOf(strKey)>=0)
          {
            MessageBox.Show("文档中包含指定的关键字!","搜索结果",MessageBoxButtons.OK);
          }
          else
          {
            MessageBox.Show("文档中没有指定的关键字!","搜索结果",MessageBoxButtons.OK);
          }
          wd.Close(ref oMissing, ref oMissing, ref oMissing);
          System.Runtime.InteropServices.Marshal.ReleaseComObject (wd);
          wd = null;

          wp.Quit(ref oMissing, ref oMissing, ref oMissing);
          System.Runtime.InteropServices.Marshal.ReleaseComObject (wp);
          wp = null;
          System.GC.Collect();
        }

        private void button6_Click(object sender, System.EventArgs e)
        {
          object filename="D:\\nettest\\window\\serial\\a.doc"; //要打开的文档路径
          string strKey="正文"; //要搜索的文本
          object MissingValue=Type.Missing;
          object oMissing = System.Reflection.Missing.Value;

          Word.Application wp=new Word.ApplicationClass();
          Word.Document wd=wp.Documents.Open(ref filename,ref MissingValue,
            ref MissingValue,ref MissingValue,
            ref MissingValue,ref MissingValue,
            ref MissingValue,ref MissingValue,
            ref MissingValue,ref MissingValue,
            ref MissingValue,ref MissingValue,
            ref MissingValue,ref MissingValue,
            ref MissingValue,ref MissingValue);

          wd.Content.Find.Text=strKey;
          if (wd.Content.Find.Execute(ref MissingValue,ref MissingValue,
            ref MissingValue,ref MissingValue,
            ref MissingValue,ref MissingValue,
            ref MissingValue,ref MissingValue,
            ref MissingValue,ref MissingValue,
            ref MissingValue,ref MissingValue,
            ref MissingValue,ref MissingValue,
            ref MissingValue))
          {
            MessageBox.Show("文档中包含指定的关键字!","搜索结果",MessageBoxButtons.OK);
          }
          else
          {
            MessageBox.Show("文档中没有指定的关键字!","搜索结果",MessageBoxButtons.OK);
          }
          wd.Close(ref oMissing, ref oMissing, ref oMissing);
          System.Runtime.InteropServices.Marshal.ReleaseComObject (wd);
          wd = null;

          wp.Quit(ref oMissing, ref oMissing, ref oMissing);
          System.Runtime.InteropServices.Marshal.ReleaseComObject (wp);
          wp = null;
          System.GC.Collect();
        }

        private void button7_Click(object sender, System.EventArgs e)
        {
          object filename="D:\\nettest\\window\\serial\\a.doc"; //要打开的文档路径
          string strKey="正文"; //要搜索的文本
          object MissingValue=Type.Missing;
          object oMissing = System.Reflection.Missing.Value;

          Word.Application wp=new Word.ApplicationClass();
          Word.Document wd=wp.Documents.Open(ref filename,ref MissingValue,
            ref MissingValue,ref MissingValue,
            ref MissingValue,ref MissingValue,
            ref MissingValue,ref MissingValue,
            ref MissingValue,ref MissingValue,
            ref MissingValue,ref MissingValue,
            ref MissingValue,ref MissingValue,
            ref MissingValue,ref MissingValue);

          wd.Content.Find.Text=strKey;
          int i=0,iCount=0;
          Word.Find wfnd;

          if (wd.Paragraphs!=null && wd.Paragraphs.Count>0)
          {
            iCount=wd.Paragraphs.Count;
            for(i=1;i<=iCount;i++)
            {
              wfnd=wd.Paragraphs[i].Range.Find;
              wfnd.ClearFormatting();
              wfnd.Text=strKey;
              if (wfnd.Execute(ref MissingValue,ref MissingValue,
                ref MissingValue,ref MissingValue,
                ref MissingValue,ref MissingValue,
                ref MissingValue,ref MissingValue,
                ref MissingValue,ref MissingValue,
                ref MissingValue,ref MissingValue,
                ref MissingValue,ref MissingValue,
                ref MissingValue))
              {
    MessageBox.Show("文档中包含指定的关键字!","搜索结果",MessageBoxButtons.OK);
                break;
              }
            }
          }
          wd.Close(ref oMissing, ref oMissing, ref oMissing);
          System.Runtime.InteropServices.Marshal.ReleaseComObject (wd);
          wd = null;

          wp.Quit(ref oMissing, ref oMissing, ref oMissing);
          System.Runtime.InteropServices.Marshal.ReleaseComObject (wp);
          wp = null;
          System.GC.Collect();
        }

  • 相关阅读:
    类之OCP(Open Closed Principle):开闭原则
    OO书籍 zz
    ObjectOriented Design Heuristics (zz)
    CSS3 filter(滤镜) 网站整体变灰色调
    js和jquery设置css样式的几种方法
    20条书写CSS代码
    js 调用向html追加内容
    移动端检测微信浏览器返回,关闭,进入后台操作
    防止表单重复提交的4种方法
    CSS浮动标准修复top塌陷和清除浮动及IE兼容标准格式
  • 原文地址:https://www.cnblogs.com/swarb/p/9924395.html
Copyright © 2011-2022 走看看