//C#如何动态生成ppt幻灯片 并且自动播放呢 我有源码可是缺少引用 确实搞不明白了 我用的office2007
//源码如下:
//C# code
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 Microsoft.Office.Interop.Graph;
using System.Runtime.InteropServices;
using System.IO; namespace Example {
public partial class Form1 : Form
{ public Form1()
{
InitializeComponent();
}
String MyTemplateFile = Directory.GetCurrentDirectory() + " \\Blends.pot"; //浏览图像文件
private void button1_Click(object sender, EventArgs e)
{
this.openFileDialog1.Filter="所有图像文件(JPeg, Gif, Bmp, etc.)|*.jpg;*.jpeg; *.gif;*.bmp;*.tif; *.tiff; *.png| JPeg 图像文件 (*.jpg;*.jpeg)|*.jpg;*.jpeg |GIF 图像文件(*.gif)|*.gif |BMP 图像文件(*.bmp)|*.bmp|Tiff 图像文件(*.tif;*.tiff) |*.tif; *.tiff|Png 图像文件(*.png)| *.png |所有文件 (*.*)|*.*";
if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
{
string MyFileName=this.openFileDialog1.FileName;
this.pictureBox1.Image=Image.FromFile(MyFileName);
}
} //创建播放幻灯片
private void button2_Click(object sender, EventArgs e)
{
string MyFileName; PowerPoint.ApplicationClass MyApp;
PowerPoint.Presentations MyPresSet;
PowerPoint._Presentation MyPres;
PowerPoint.Slides MySlides;
PowerPoint._Slide MySlide;
PowerPoint.SlideShowTransition MySST;
PowerPoint.SlideShowSettings MySSS;
PowerPoint.SlideRange MySldRng;
PowerPoint.SlideShowWindows MySSWs;
bool MyAssistantOn;
int i, MyCount;
PowerPoint.TextRange MyTextRng;
Microsoft.Office.Interop.Graph.Chart MyChart;
PowerPoint.Shapes MyShapes;
PowerPoint.Shape MyShape;
try {
MyFileName = this.openFileDialog1.FileName;
if (!File.Exists(MyFileName))
{
MessageBox.Show("幻灯片模板文件实际不存在!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
MyApp = new PowerPoint.ApplicationClass();
MyApp.Visible = MsoTriState.msoCTrue;
MyPresSet = MyApp.Presentations;
MyPres = MyPresSet.Open(MyTemplateFile, MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue);
MySlides = MyPres.Slides;
//创建幻灯片第一页
MySlide=MySlides.Add(1,PowerPoint.PpSlideLayout.ppLayoutTitleOnly);
MyTextRng=MySlide.Shapes[1].TextFrame.TextRange;
MyTextRng.Text="125555555555555555555552111111";
MyTextRng.Font.Name="宋体";
MyTextRng.Font.Size=48;
MySlide.Shapes.AddPicture(MyFileName, MsoTriState.msoFalse,MsoTriState.msoTrue,150, 150, 500, 350);
//创建幻灯片第二页
MySlide=MySlides.Add(2, PowerPoint.PpSlideLayout.ppLayoutTitleOnly);
MyTextRng=MySlide.Shapes[1].TextFrame.TextRange; MyTextRng.Text="演示绘制饼图";
MyTextRng.Font.Name="宋体";
MyTextRng.Font.Size= 48;
MyChart = (Microsoft.Office.Interop.Graph.Chart)MySlide.Shapes.AddOLEObject(150, 150, 480, 320, "MSGraph.Chart.8", "", MsoTriState.msoFalse, "", 0, "",
MsoTriState.msoFalse).OLEFormat.Object;
MyChart.ChartType = Microsoft.Office.Interop.Graph.XlChartType.xl3DPie;
MyChart.Legend.Position = Microsoft.Office.Interop.Graph.XlLegendPosition.xlLegendPositionBottom;
MyChart.HasTitle= true; MyChart.ChartTitle.Text="经典系列丛书2006年度图书销量";
//创建幻灯片第三页
MySlide=MySlides.Add(3, PowerPoint.PpSlideLayout.ppLayoutBlank);
MySlide.FollowMasterBackground=MsoTriState.msoFalse;
MyShapes=MySlide.Shapes;
MyShape=MyShapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect29,"经典编程实例集锦", "Impact", 80,MsoTriState.msoFalse, MsoTriState.msoFalse, 50, 200);
//创建幻灯片第四页
MySlide=MySlides.Add(4, PowerPoint.PpSlideLayout.ppLayoutBlank);
MySlide.FollowMasterBackground=MsoTriState.msoFalse;
MyShapes=MySlide.Shapes;
MyShape=MyShapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect30,"精彩编程实例集锦","Impact", 60,MsoTriState.msoFalse, MsoTriState.msoFalse, 150, 10);
MyShape=MyShapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect29,"罗斌编著", "Impact", 80, MsoTriState.msoFalse, MsoTriState.msoFalse, 350, 250);
//创建幻灯片第五页
MySlide=MySlides.Add(5, PowerPoint.PpSlideLayout.ppLayoutBlank);
MySlide.FollowMasterBackground=MsoTriState.msoFalse;
MyShapes=MySlide.Shapes;
MyShape=MyShapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect27,"The End", "Impact", 96, MsoTriState.msoFalse, MsoTriState.msoFalse, 230, 200);
MyCount=(int)MySlides.Count; int[] MySlideIdx=new int[MyCount];
for (i = 0; i < MyCount; i++)
{
MySlideIdx[i]=i+1;
}
MySldRng = MySlides.Range(MySlideIdx);
MySST=MySldRng.SlideShowTransition;
MySST.AdvanceOnTime=MsoTriState.msoTrue;
MySST.AdvanceTime=1;
MySST.EntryEffect=PowerPoint.PpEntryEffect.ppEffectBoxOut;
MyAssistantOn=MyApp.Assistant.On;
MyApp.Assistant.On=false;
MySSS= MyPres.SlideShowSettings;
MySSS.StartingSlide=1;
MySSS.EndingSlide=1;
MySSS.Run();
MySSWs= MyApp.SlideShowWindows;
while (MySSWs.Count >= 1)
{
System.Threading.Thread.Sleep(2000);
}
if (MyAssistantOn)
{
MyApp.Assistant.On = true;
MyApp.Assistant.Visible = false;
}
MyPres.Close();
MyApp.Quit();
}
catch (Exception MyEx)
{
MessageBox.Show(MyEx.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
//源码如下:
//C# code
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 Microsoft.Office.Interop.Graph;
using System.Runtime.InteropServices;
using System.IO; namespace Example {
public partial class Form1 : Form
{ public Form1()
{
InitializeComponent();
}
String MyTemplateFile = Directory.GetCurrentDirectory() + " \\Blends.pot"; //浏览图像文件
private void button1_Click(object sender, EventArgs e)
{
this.openFileDialog1.Filter="所有图像文件(JPeg, Gif, Bmp, etc.)|*.jpg;*.jpeg; *.gif;*.bmp;*.tif; *.tiff; *.png| JPeg 图像文件 (*.jpg;*.jpeg)|*.jpg;*.jpeg |GIF 图像文件(*.gif)|*.gif |BMP 图像文件(*.bmp)|*.bmp|Tiff 图像文件(*.tif;*.tiff) |*.tif; *.tiff|Png 图像文件(*.png)| *.png |所有文件 (*.*)|*.*";
if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
{
string MyFileName=this.openFileDialog1.FileName;
this.pictureBox1.Image=Image.FromFile(MyFileName);
}
} //创建播放幻灯片
private void button2_Click(object sender, EventArgs e)
{
string MyFileName; PowerPoint.ApplicationClass MyApp;
PowerPoint.Presentations MyPresSet;
PowerPoint._Presentation MyPres;
PowerPoint.Slides MySlides;
PowerPoint._Slide MySlide;
PowerPoint.SlideShowTransition MySST;
PowerPoint.SlideShowSettings MySSS;
PowerPoint.SlideRange MySldRng;
PowerPoint.SlideShowWindows MySSWs;
bool MyAssistantOn;
int i, MyCount;
PowerPoint.TextRange MyTextRng;
Microsoft.Office.Interop.Graph.Chart MyChart;
PowerPoint.Shapes MyShapes;
PowerPoint.Shape MyShape;
try {
MyFileName = this.openFileDialog1.FileName;
if (!File.Exists(MyFileName))
{
MessageBox.Show("幻灯片模板文件实际不存在!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
MyApp = new PowerPoint.ApplicationClass();
MyApp.Visible = MsoTriState.msoCTrue;
MyPresSet = MyApp.Presentations;
MyPres = MyPresSet.Open(MyTemplateFile, MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue);
MySlides = MyPres.Slides;
//创建幻灯片第一页
MySlide=MySlides.Add(1,PowerPoint.PpSlideLayout.ppLayoutTitleOnly);
MyTextRng=MySlide.Shapes[1].TextFrame.TextRange;
MyTextRng.Text="125555555555555555555552111111";
MyTextRng.Font.Name="宋体";
MyTextRng.Font.Size=48;
MySlide.Shapes.AddPicture(MyFileName, MsoTriState.msoFalse,MsoTriState.msoTrue,150, 150, 500, 350);
//创建幻灯片第二页
MySlide=MySlides.Add(2, PowerPoint.PpSlideLayout.ppLayoutTitleOnly);
MyTextRng=MySlide.Shapes[1].TextFrame.TextRange; MyTextRng.Text="演示绘制饼图";
MyTextRng.Font.Name="宋体";
MyTextRng.Font.Size= 48;
MyChart = (Microsoft.Office.Interop.Graph.Chart)MySlide.Shapes.AddOLEObject(150, 150, 480, 320, "MSGraph.Chart.8", "", MsoTriState.msoFalse, "", 0, "",
MsoTriState.msoFalse).OLEFormat.Object;
MyChart.ChartType = Microsoft.Office.Interop.Graph.XlChartType.xl3DPie;
MyChart.Legend.Position = Microsoft.Office.Interop.Graph.XlLegendPosition.xlLegendPositionBottom;
MyChart.HasTitle= true; MyChart.ChartTitle.Text="经典系列丛书2006年度图书销量";
//创建幻灯片第三页
MySlide=MySlides.Add(3, PowerPoint.PpSlideLayout.ppLayoutBlank);
MySlide.FollowMasterBackground=MsoTriState.msoFalse;
MyShapes=MySlide.Shapes;
MyShape=MyShapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect29,"经典编程实例集锦", "Impact", 80,MsoTriState.msoFalse, MsoTriState.msoFalse, 50, 200);
//创建幻灯片第四页
MySlide=MySlides.Add(4, PowerPoint.PpSlideLayout.ppLayoutBlank);
MySlide.FollowMasterBackground=MsoTriState.msoFalse;
MyShapes=MySlide.Shapes;
MyShape=MyShapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect30,"精彩编程实例集锦","Impact", 60,MsoTriState.msoFalse, MsoTriState.msoFalse, 150, 10);
MyShape=MyShapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect29,"罗斌编著", "Impact", 80, MsoTriState.msoFalse, MsoTriState.msoFalse, 350, 250);
//创建幻灯片第五页
MySlide=MySlides.Add(5, PowerPoint.PpSlideLayout.ppLayoutBlank);
MySlide.FollowMasterBackground=MsoTriState.msoFalse;
MyShapes=MySlide.Shapes;
MyShape=MyShapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect27,"The End", "Impact", 96, MsoTriState.msoFalse, MsoTriState.msoFalse, 230, 200);
MyCount=(int)MySlides.Count; int[] MySlideIdx=new int[MyCount];
for (i = 0; i < MyCount; i++)
{
MySlideIdx[i]=i+1;
}
MySldRng = MySlides.Range(MySlideIdx);
MySST=MySldRng.SlideShowTransition;
MySST.AdvanceOnTime=MsoTriState.msoTrue;
MySST.AdvanceTime=1;
MySST.EntryEffect=PowerPoint.PpEntryEffect.ppEffectBoxOut;
MyAssistantOn=MyApp.Assistant.On;
MyApp.Assistant.On=false;
MySSS= MyPres.SlideShowSettings;
MySSS.StartingSlide=1;
MySSS.EndingSlide=1;
MySSS.Run();
MySSWs= MyApp.SlideShowWindows;
while (MySSWs.Count >= 1)
{
System.Threading.Thread.Sleep(2000);
}
if (MyAssistantOn)
{
MyApp.Assistant.On = true;
MyApp.Assistant.Visible = false;
}
MyPres.Close();
MyApp.Quit();
}
catch (Exception MyEx)
{
MessageBox.Show(MyEx.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}