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

  • 相关阅读:
    Java多线程系列--“JUC集合”08之 LinkedBlockingQueue
    Java多线程系列--“JUC集合”07之 ArrayBlockingQueue
    Java多线程系列--“JUC集合”06之 ConcurrentSkipListSet
    Java多线程系列--“JUC集合”05之 ConcurrentSkipListMap
    Java多线程系列--“JUC集合”04之 ConcurrentHashMap
    Java多线程系列--“JUC集合”03之 CopyOnWriteArraySet
    Java多线程系列--“JUC集合”02之 CopyOnWriteArrayList
    Java多线程系列--“JUC集合”01之 框架
    Java多线程系列--“JUC锁”11之 Semaphore信号量的原理和示例
    Java多线程系列--“JUC锁”10之 CyclicBarrier原理和示例
  • 原文地址:https://www.cnblogs.com/swarb/p/9924395.html
Copyright © 2011-2022 走看看