C#读PPT文件与搜索

C#读取与搜索PPT文件内容
这个C#代码示例展示了如何打开PPT文件,并遍历每张幻灯片上的形状来检索文本。它还包含了搜索特定关键词的功能,但注释掉了这部分代码。用户可以通过输入关键词在PPT文件中查找文本。

private void button10_Click(object sender, System.EventArgs e)
    {
      string fileName = "d://a.ppt";

      PowerPoint.Application pptApp = new PowerPoint.ApplicationClass();
      PowerPoint.Presentation pptPre = pptApp.Presentations.Open(fileName,
        Microsoft.Office.Core.MsoTriState.msoTrue,
        Microsoft.Office.Core.MsoTriState.msoFalse,
        Microsoft.Office.Core.MsoTriState.msoFalse);
      PowerPoint.Slides mySlides = pptPre.Slides;

      string[] strKeyWordList = {"无线"}; //要搜索的文本
      try
      {
        /*
        for (int i=0;i<mySlides.Count;i++)
        {
          PowerPoint.Shapes myShapes = mySlides[i].Shapes;
          for (int j=0;j<myShapes.Count;j++)
          {
            if (myShapes[j].HasTextFrame == Microsoft.Office.Core.MsoTriState.msoTrue)
            {
              if (Microsoft.Office.Core.MsoTriState.msoTrue == myShapes[j].TextFrame.HasText)
              {
                textBox2.Text = textBox2.Text + myShapes[j].TextFrame.TextRange.Text;
              }
            }
          }
        }
        */
        foreach(PowerPoint.Slide sld in pptPre.Slides)
        {
          foreach(PowerPoint.Shape shp in sld.Shapes)
          {
            if(Microsoft.Office.Core.MsoTriState.msoTrue == shp.HasTextFrame)
            {
              if(Microsoft.Office.Core.MsoTriState.msoTrue == shp.TextFrame.HasText)
              {
                textBox2.Text = textBox2.Text + shp.TextFrame.TextRange.Text;
              }
            }
          }
        }
        /*
        PowerPoint.TextRange oText = null;
        foreach(PowerPoint.Slide slide in pptPre.Slides)
        {
          foreach(PowerPoint.Shape shape in slide.Shapes)
          {
            foreach(string strKeyWord in strKeyWordList)
            {
              oText = shape.TextFrame.TextRange.Find(strKeyWord, 0,
              Microsoft.Office.Core.MsoTriState.msoFalse,
              Microsoft.Office.Core.MsoTriState.msoTrue);

              if(oText!=null)
              {
                MessageBox.Show("文档中包含指定的关键字 " + strKeyWord + " !", "搜索结果", MessageBoxButtons.OK);
                continue;
              }
            }
          }
        }
        */
      }
      finally
      {
        pptPre.Close();
        System.Runtime.InteropServices.Marshal.ReleaseComObject (pptPre);
        pptPre = null;
        pptApp.Quit();
        System.Runtime.InteropServices.Marshal.ReleaseComObject (pptApp);
        pptApp = null;
      }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值