c# 操作PPT

前段时间要做一个把指定图片放到新建的ppt的东西,在网上找了点资料看了一下,发现用C#做好像是最简单的一个,一下是在网上找的一段代码,直接贴进去就能够执行,可是在执行之前一定要加入dll支持:

项目->加入引用->com->microsoft
office object libraty

项目->加入引用->com->microsoft
powerpoint object libraty

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using Microsoft.Office.Core;

using PowerPoint = Microsoft.Office.Interop.PowerPoint;

using Graph = Microsoft.Office.Interop.Graph;

using System.Runtime.InteropServices;

namespace WindowsApplication1

{

public
partial class Form1 : Form

{

public
Form1()

{

InitializeComponent();

}

private
void button1_Click(object sender, System.EventArgs e)

{

ShowPresentation();

GC.Collect();

}

private
void ShowPresentation()

{

String
strTemplate, strPic;

strTemplate
=

"C:\\Program
Files\\Microsoft Office\\Templates\\Presentation Designs\\Blends.pot";

strPic
= "C:\\Windows\\Blue Lace 16.bmp";

bool
bAssistantOn;

PowerPoint.Application
objApp;

PowerPoint.Presentations
objPresSet;

PowerPoint._Presentation
objPres;

PowerPoint.Slides
objSlides;

PowerPoint._Slide
objSlide;

PowerPoint.TextRange
objTextRng;

PowerPoint.Shapes
objShapes;

PowerPoint.Shape
objShape;

PowerPoint.SlideShowWindows
objSSWs;

PowerPoint.SlideShowTransition
objSST;

PowerPoint.SlideShowSettings
objSSS;

PowerPoint.SlideRange
objSldRng;

Graph.Chart
objChart;

//Create
a new presentation based on a template.

objApp
= new PowerPoint.Application();

objApp.Visible
= MsoTriState.msoTrue;

objPresSet
= objApp.Presentations;

objPres
= objPresSet.Open(strTemplate,

MsoTriState.msoFalse,
MsoTriState.msoTrue, MsoTriState.msoTrue);

objSlides
= objPres.Slides;

//Build
Slide #1:

//Add
text to the slide, change the font and insert/position a

//picture
on the first slide.

objSlide
= objSlides.Add(1, PowerPoint.PpSlideLayout.ppLayoutTitleOnly);

objTextRng
= objSlide.Shapes[1].TextFrame.TextRange;

objTextRng.Text
= "My Sample Presentation";

objTextRng.Font.Name
= "Comic Sans MS";

objTextRng.Font.Size
= 48;

objSlide.Shapes.AddPicture(strPic,
MsoTriState.msoFalse, MsoTriState.msoTrue,

150,
150, 500, 350);

//Build
Slide #2:

//Add
text to the slide title, format the text. Also add a chart to the

//slide
and change the chart type to a 3D pie chart.

objSlide
= objSlides.Add(2, PowerPoint.PpSlideLayout.ppLayoutTitleOnly);

objTextRng
= objSlide.Shapes[1].TextFrame.TextRange;

objTextRng.Text
= "My Chart";

objTextRng.Font.Name
= "Comic Sans MS";

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
= "Here it is...";

//Build
Slide #3:

//Change
the background color of this slide only. Add a text effect to the slide

//and
apply various color schemes and shadows to the text effect.

objSlide
= objSlides.Add(3, PowerPoint.PpSlideLayout.ppLayoutBlank);

objSlide.FollowMasterBackground
= MsoTriState.msoFalse;

objShapes
= objSlide.Shapes;

objShape
= objShapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect27,

"The
End", "Impact", 96, MsoTriState.msoFalse, MsoTriState.msoFalse, 230, 200);

//
自己主动播放的代码(開始)

//Modify
the slide show transition settings for all 3 slides in

//the
presentation.

int[]
SlideIdx = new int[3];

for
(int i = 0; i < 3; i++) SlideIdx[i] = i + 1;

objSldRng
= objSlides.Range(SlideIdx);

objSST
= objSldRng.SlideShowTransition;

objSST.AdvanceOnTime
= MsoTriState.msoTrue;

objSST.AdvanceTime
= 3;

objSST.EntryEffect
= PowerPoint.PpEntryEffect.ppEffectBoxOut;

//Prevent
Office Assistant from displaying alert messages:

bAssistantOn
= objApp.Assistant.On;

objApp.Assistant.On
= false;

//Run
the Slide show from slides 1 thru 3.

objSSS
= objPres.SlideShowSettings;

objSSS.StartingSlide
= 1;

objSSS.EndingSlide
= 3;

objSSS.Run();

//Wait
for the slide show to end.

objSSWs
= objApp.SlideShowWindows;

while
(objSSWs.Count >= 1) System.Threading.Thread.Sleep(100);

//Reenable
Office Assisant, if it was on:

if
(bAssistantOn)

{

objApp.Assistant.On
= true;

objApp.Assistant.Visible
= false;

}

//
自己主动播放的代码(结束)

//Close
the presentation without saving changes and quit PowerPoint.

objPres.Close();

objApp.Quit();

}

}

}

时间: 2024-08-25 04:39:30

c# 操作PPT的相关文章

C++通过COM接口操作PPT

一. 背景说明 在VS环境下,开发C++代码操作PPT,支持对PPT模板的修改.包括修改文本标签.图表.表格.满足大多数软件生成PPT报告的要求,先手工创建好PPT模板,在程序中修改模板数据. 二. 开发环境构建 通过VS2012的Class Wizard创建PowerPoint和Excel的COM接口:由于需要操作PPT中的图表,而图表的数据使用Excel存储的,需要修改图表的数据就得生成Excel的COM接口. 1.1 进入类向导 1.2 添加PowerPoint COM接口 1.3 添加E

JAVA通过COM接口操作PPT

一. 背景说明 在Eclipse环境下,开发JAVA代码操作PPT,支持对PPT模板的修改.包括修改文本标签.图表.表格.满足大多数软件生成PPT报告的要求,即先收工创建好模板,在程序中修改模板数据. 二. 开发环境搭建 下载jacob开源组件,解压后把jacob.jar添加到项目中.拷贝对应的dll文件到Path路径下.32位系统采用X86的dll, 64位系统采用X64的dll. 三. 实现合并PPT函数 1.1 合并PPT /** * 合并多个PPT文件.要求输出文件和合并文件均已存在,不

C# 操作PPt,去掉文本框的边框

using System; using System.Collections.Generic; using System.Linq; using System.Text; using OFFICECORE = Microsoft.Office.Core; using POWERPOINT = Microsoft.Office.Interop.PowerPoint; using System.Windows; using System.Collections; using System.Windo

c#如何操作ppt的播放

C#控制ppt的代码 包括打开ppt文件.读取幻灯页,插入幻灯片,自动播放等 using System.Collections.Generic; using System.Linq; using System.Text; using OFFICECORE = Microsoft.Office.Core; using POWERPOINT = Microsoft.Office.Interop.PowerPoint; using System.windows; using System.Collec

(转)C#操作PPT

原文地址:http://blog.163.com/loveyingchun_1314/blog/static/2382425120124312627530/ 引用Microsoft.Office.Core时需要在com选项中添加Microsoft Office 12.0 Object Library(我的Office版本是2007),如果没有这个需要修改Office添加开发支持(开始-程序-Office更改) using System; using System.Collections.Gene

使用POI操作PPT文档(插入文本、图片)转

1)如果是创建新的PPT文档,直接使用SlideShow和Slide类就可以,其中SlideShow表示PPT文档,Slide表示某一张幻灯片如下代码创建空的PPT文档: 1 SlideShow ppt = new SlideShow(); 2 Slide[] slides = ppt.getSlides(); 3 assertTrue(slides.length == 0); 4 savePPTFile(ppt); 5 6 private void savePPTFile(SlideShow

Jacob操作office文档(Word,PPT,Excel)

public boolean doc2pdf(String srcFilePath, String pdfFilePath) { ActiveXComponent app = null; Dispatch doc = null; try { ComThread.InitSTA(); app = new ActiveXComponent("Word.Application"); app.setProperty("Visible", false); Dispatch d

python操作word、ppt的详解

python使用win32com的心得 python可以使用一个第三方库叫做win32com达到操作com的目的, 我是安装了ActivePython的第三方库,从官网下载了安装包,该第三方库几乎封装了所有python下面的win32相关的操作,例如win32api,win32gui等等,可以说是比较齐全的了,下载地址可以自行百度获取.         主要是有个项目可能要用到ppt转换成视频的功能. 之后在想使用com操作excel还有word,ppt的时候,相信大部分人跟我一样,都是搜索py

Microsoft word/Excel/PPT打开报错问题

很多时候我们会发现我们辛辛苦苦从网上.论坛或QQ群等地下载下来的文档.表格.PPT竟然打不开,许多人以为是文件的问题,直接删除了,而身边很多朋友会将此类问题发于我让我一一解答,其实这些问题,网上都有相关的解决方法的,所以今天我在这里再为大家综合整理一番,供身边的同事.朋友及大家更好的参考学习. 一.EXCEL问题: 报错信息如下: 内存或磁盘空间不足,MICROSOFT EXCEL 无法再次打开或保存任何文档. 要想获得更多的可用内存,请关闭不再使用的工作薄或程序. 要想释放磁盘空间,请删除相应