在做之前,首先需要添加对 Microsoft PowerPoint 对象库和 Microsoft Graph 对象库的引用。为此,请按照下列步骤操作:
//在项目菜单上,单击添加引用。 //在 COM 选项卡上,找到 Microsoft PowerPoint 对象库,然后单击选择。还应找到 Microsoft Graph 对象库,然后单击选择。 //注意:Microsoft Office XP 不包含 PIA,但您可以下载 PIA。
// sTemplate 和 sPic 常量分别表示 PowerPoint 模板和图片的完整路径及文件名。按照需要修改这些路径以使用安装在您系统中的模板或图片。 以下是动态创建PPT源码:
String strTemplate, strPic;
strTemplate = Application.StartupPath + @"\mb.pptx";
strPic = Application.StartupPath + @"\top.jpg";
#region 定义变量
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;
PowerPoint.Table table = null;
PowerPoint.TextFrame txtFrame;
#endregion
//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);
//添加一行文字(left:40,top:110,width:328,height:38)
objTextRng = objSlide.Shapes.AddLabel(MsoTextOrientation.msoTextOrientationHorizontal, 40, 110, 328, 38).TextFrame.TextRange;
objTextRng.Text = " PPT";
objTextRng.Font.Color.RGB = 0x66CCFF; //设置字的颜色
objTextRng.Font.Size = 28; //字号
objTextRng.Font.Name = "微软雅黑";//字类型
#region 添加一个表格
//添加一个表格(行:3,列:3,left:88,top:235,width:530,height:130)
objShape = objSlide.Shapes.AddTable(5, 3, 88, 235, 530, 130);
objSlide.FollowMasterBackground = MsoTriState.msoFalse;
table = objShape.Table;
for (int i = 1; i <= table.Rows.Count; i++) {//循环创建行、列,并指定单元格字体大小,字体类型,单元格显示文本内容
for (int j = 1; j <= table.Columns.Count; j++) {
table.Cell(i, j).Shape.TextFrame.TextRange.Font.Size = 18;
table.Cell(i, j).Shape.TextFrame.TextRange.Font.Name = "微软雅黑";
if (i != 1) {
table.Cell(i, j).Shape.TextFrame.TextRange.Text = string.Format("[{0},{1}]", i, j);
}
}
}
//以下代码为表头
table.Cell(1, 1).Shape.TextFrame.TextRange.Font.Bold = MsoTriState.msoTrue;//设置单元格文本内容加粗
table.Cell(1, 2).Shape.TextFrame.TextRange.Font.Bold = MsoTriState.msoTrue;
table.Cell(1, 3).Shape.TextFrame.TextRange.Font.Bold = MsoTriState.msoTrue;
table.Cell(1, 1).Shape.TextFrame.TextRange.InsertAfter("预警分类");//设置单元格文本内容
table.Cell(1, 2).Shape.TextFrame.TextRange.InsertAfter("已配置指标数量");
table.Cell(1, 3).Shape.TextFrame.TextRange.InsertAfter("启用数量");
table.Cell(1, 1).Shape.TextFrame.HorizontalAnchor = MsoHorizontalAnchor.msoAnchorCenter;//设置单元格文本内容对齐方式
table.Cell(1, 2).Shape.TextFrame.HorizontalAnchor = MsoHorizontalAnchor.msoAnchorCenter;
table.Cell(1, 3).Shape.TextFrame.HorizontalAnchor = MsoHorizontalAnchor.msoAnchorCenter;
#endregion
//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.ppLayoutClipArtAndVerticalText);
objTextRng = objSlide.Shapes[1].TextFrame.TextRange;
objTextRng.Text = "生产指挥预警管理工作";
objTextRng.Font.Name = "Comic Sans MS";
objTextRng.Font.Size = 18;
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.ppLayoutComparison);
objSlide.FollowMasterBackground = MsoTriState.msoFalse;
objSlide.Shapes.AddPicture(strPic, MsoTriState.msoFalse, MsoTriState.msoTrue,
150, 170, 395, 215);
objShapes = objSlide.Shapes;
objShape = objShapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect27,
"谢谢!", "Impact", 54, MsoTriState.msoFalse, MsoTriState.msoFalse, 270, 420);
//Modify the slide show transition settings for all 3 slides in
//the presentation.
int[] SlideIdx = new int[1];
for (int i = 0; i < 1; 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 = 1;
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();
操作PPT模版中的内容源码:
using Microsoft.Office.Core;
using Graph = Microsoft.Office.Interop.Graph;
using System.Runtime.InteropServices;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
String strTemplate, strPic;
strTemplate = Application.StartupPath + @"\mb.pptx";
strPic = Application.StartupPath + @"\12.jpg";
PowerPoint.Application objApp;
PowerPoint.Presentations objPresSet;
PowerPoint._Presentation objPres;
PowerPoint.Slides objSlides;
//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); //打开模版PPT
objSlides = objPres.Slides; //获取模版PPT内所有的页对象
//(objSlides.Count)获取总页数;(objSlides[index])通过控制index定向到index页面获取到该对象,注:index从1开始
注:index从1开始,;objSlides[index].Shapes.Count获取当前页的层数(层数的概念描述不清楚,实际使用中自己尝试便知);
objSlides[index].Shapes[i],指定到index页中的i层;objSlides[index].Shapes[i].TextFrame.TextRange.Text
//获取模版PPT的第index页的i层的文本内容;
例子://获取模版PPT的第三页的表格
for (int i = 2; i <= objSlides[3].Shapes[5].Table.Rows.Count; i++) {
for (int j = 2; j <= objSlides[3].Shapes[5].Table.Columns.Count; j++) {
objSlides[3].Shapes[5].Table.Cell(i, j).Shape.TextFrame.TextRange.Font.Size = 18;
objSlides[3].Shapes[5].Table.Cell(i, j).Shape.TextFrame.TextRange.Font.Name = "微软雅黑";
if (i != 1) {
objSlides[3].Shapes[5].Table.Cell(i, j).Shape.TextFrame.TextRange.Text = string.Format("[{0},{1}]", i, j);
}
}
}
PowerPoint.PpSaveAsFileType format = PowerPoint.PpSaveAsFileType.ppSaveAsDefault;
objPres.SaveAs(Application.StartupPath+@"\ppt"+DateTime.Now.ToString("yyyyMMddHHmmss "), format, Microsoft.Office.Core.MsoTriState.msoFalse);
//Close the presentation without saving changes and quit PowerPoint.
objPres.Close();
objApp.Quit();
以上是本次对PPT操作的大概内容,不好之处还望大家多多指出。部分代码是来源于网络他人之作,也算是站在巨人肩膀上来写的这博客