日期:2014-05-17  浏览次数:20563 次

C#操作PPT的问题,高手有木有
根据C#中数据源,自动在PPT生成柱状饼状图。
并且当数据源改变时,PPT中柱状饼状图也随之改变
如何实现该操作?


------解决方案--------------------
C# code
c#操作powerpoint的一个例子
2009-03-14 15:17
摘自: http://c05.blog.hexun.com/7283637_d.html
//实现PPT文本查找功能
//关键代码
private void searchPPT(string[] keyWordList,string pptFileName)//在指定的ppt文档中搜索keyWord
         {
              //Microsoft powerpoint 11.0 object library这个是所添加的引用         
            showMsg(lbShowMsg,"启动搜索"+pptFileName);
         //其中Presentation代表一个 PowerPoint 文档,Slide表示PowerPoint文档中的单张幻灯片
              //TextFrame是幻灯片上的文本框,TextRange是文本框中的文本。
              PowerPoint.ApplicationClass pa;
              PowerPoint.Presentation pp;
              pa=null;
              pp=null;
              try
              {
                  
                   showMsg(lbShowMsg,"尝试打开 "+pptFileName);
                   //打开ppt文档
                   pa=new PowerPoint.ApplicationClass();
                   pp=pa.Presentations.Open(pptFileName,
                       Microsoft.Office.Core.MsoTriState.msoTrue,
                       Microsoft.Office.Core.MsoTriState.msoTrue,Microsoft.Office.Core.MsoTriState.msoFalse);
                   PowerPoint.TextRange oText;
    
                   int slideCount=pp.Slides.Count;//总的幻灯片数
        
                   foreach(PowerPoint.Slide slide in pp.Slides)//对每张幻灯片
                   {
                      
              showMsg(lbShowMsg,"正在搜索"+pptFileName+" 幻灯片"+slide.SlideNumber.ToString()+"/"+slideCount);
                       foreach(PowerPoint.Shape shape in slide.Shapes)//对所有的元素
                       {
                      
                     if(shape.HasTextFrame==Microsoft.Office.Core.MsoTriState.msoTrue)//如果此幻灯片中有文本框
                            {
                                 foreach(string keyWord in keyWordList)//对每组关键字
                                 {
                                     oText=null;
                                     oText=shape.TextFrame.TextRange.Find
                      (keyWord,0,Microsoft.Office.Core.MsoTriState.msoFalse,Microsoft.Office.Core.MsoTriState.msoTrue);
                                     if (oText!=null)
                                     {
                                    
                                          string temp=pptFileName.Remove(0,pptFileName.LastIndexOf("\\")+1);
                                          string name=temp.Remove(temp.LastIndexOf("."),4);
                                          int index=slide.SlideNumber;
                         lbResult.Items.Add(name+" 幻灯片"+index.ToString()+"/"+slideCount);//添加到搜索结果中
                                          lbResult.Update();
                                          resultText.Add(shape.TextFrame.TextRange.Text);
                                          continue;
                                     }
                                 }
                            }
                       }
                   }
             
              }
        
              catch
              {
              }
              finally
              {
               //释放资源
                   if(pp!=null)
                   {
                       System.Runtime.InteropServices.Marshal.ReleaseComObject(pp);
                       pp=null;
                   }
                   if(pa!=null)
                   {
                       System.Runtime.InteropServices.Marshal.ReleaseComObject(pa);
                       pa=null;
                   }                  
              }
         }