您好,欢迎访问三七文档
c#操作PPT前段时间要做一个把指定图片放到新建的ppt的东西,在网上找了点资料看了一下,发现用C#做好像是最简单的一个,一下是在网上找的一段代码,直接贴进去就可以运行,但是在运行之前一定要添加dll支持:项目-添加引用-com-microsoftofficeobjectlibraty项目-添加引用-com-microsoftpowerpointobjectlibratyusingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingMicrosoft.Office.Core;usingPowerPoint=Microsoft.Office.Interop.PowerPoint;usingGraph=Microsoft.Office.Interop.Graph;usingSystem.Runtime.InteropServices;namespaceWindowsApplication1{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidbutton1_Click(objectsender,System.EventArgse){ShowPresentation();GC.Collect();}privatevoidShowPresentation(){StringstrTemplate,strPic;strTemplate=C:\\ProgramFiles\\MicrosoftOffice\\Templates\\PresentationDesigns\\Blends.pot;strPic=C:\\Windows\\BlueLace16.bmp;boolbAssistantOn;PowerPoint.ApplicationobjApp;PowerPoint.PresentationsobjPresSet;PowerPoint._PresentationobjPres;PowerPoint.SlidesobjSlides;PowerPoint._SlideobjSlide;PowerPoint.TextRangeobjTextRng;PowerPoint.ShapesobjShapes;PowerPoint.ShapeobjShape;PowerPoint.SlideShowWindowsobjSSWs;PowerPoint.SlideShowTransitionobjSST;PowerPoint.SlideShowSettingsobjSSS;PowerPoint.SlideRangeobjSldRng;Graph.ChartobjChart;//Createanewpresentationbasedonatemplate.objApp=newPowerPoint.Application();objApp.Visible=MsoTriState.msoTrue;objPresSet=objApp.Presentations;objPres=objPresSet.Open(strTemplate,MsoTriState.msoFalse,MsoTriState.msoTrue,MsoTriState.msoTrue);objSlides=objPres.Slides;//BuildSlide#1://Addtexttotheslide,changethefontandinsert/positiona//pictureonthefirstslide.objSlide=objSlides.Add(1,PowerPoint.PpSlideLayout.ppLayoutTitleOnly);objTextRng=objSlide.Shapes[1].TextFrame.TextRange;objTextRng.Text=MySamplePresentation;objTextRng.Font.Name=ComicSansMS;objTextRng.Font.Size=48;objSlide.Shapes.AddPicture(strPic,MsoTriState.msoFalse,MsoTriState.msoTrue,150,150,500,350);//BuildSlide#2://Addtexttotheslidetitle,formatthetext.Alsoaddacharttothe//slideandchangethecharttypetoa3Dpiechart.objSlide=objSlides.Add(2,PowerPoint.PpSlideLayout.ppLayoutTitleOnly);objTextRng=objSlide.Shapes[1].TextFrame.TextRange;objTextRng.Text=MyChart;objTextRng.Font.Name=ComicSansMS;objTextRng.Font.Size=48;objChart=(Graph.Chart)objSlide.Shapes.AddOLEObject(150,150,480,320,MSGraph.Chart.8,,MsoTriState.msoFalse,,0,,MsoTriState.msoFalse).OLEFormat.Object;objChart.ChartType=Graph.XlChartType.xl3DPie;objChart.Legend.Position=Graph.XlLegendPosition.xlLegendPositionBottom;objChart.HasTitle=true;objChart.ChartTitle.Text=Hereitis...;//BuildSlide#3://Changethebackgroundcolorofthisslideonly.Addatexteffecttotheslide//andapplyvariouscolorschemesandshadowstothetexteffect.objSlide=objSlides.Add(3,PowerPoint.PpSlideLayout.ppLayoutBlank);objSlide.FollowMasterBackground=MsoTriState.msoFalse;objShapes=objSlide.Shapes;objShape=objShapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect27,TheEnd,Impact,96,MsoTriState.msoFalse,MsoTriState.msoFalse,230,200);//自动播放的代码(开始)//Modifytheslideshowtransitionsettingsforall3slidesin//thepresentation.int[]SlideIdx=newint[3];for(inti=0;i3;i++)SlideIdx[i]=i+1;objSldRng=objSlides.Range(SlideIdx);objSST=objSldRng.SlideShowTransition;objSST.AdvanceOnTime=MsoTriState.msoTrue;objSST.AdvanceTime=3;objSST.EntryEffect=PowerPoint.PpEntryEffect.ppEffectBoxOut;//PreventOfficeAssistantfromdisplayingalertmessages:bAssistantOn=objApp.Assistant.On;objApp.Assistant.On=false;//RuntheSlideshowfromslides1thru3.objSSS=objPres.SlideShowSettings;objSSS.StartingSlide=1;objSSS.EndingSlide=3;objSSS.Run();//Waitfortheslideshowtoend.objSSWs=objApp.SlideShowWindows;while(objSSWs.Count=1)System.Threading.Thread.Sleep(100);//ReenableOfficeAssisant,ifitwason:if(bAssistantOn){objApp.Assistant.On=true;objApp.Assistant.Visible=false;}//自动播放的代码(结束)//ClosethepresentationwithoutsavingchangesandquitPowerPoint.objPres.Close();objApp.Quit();}}}
本文标题:C#操作ppt
链接地址:https://www.777doc.com/doc-5597719 .html