C#.net 操作 ppt

真是蛋疼,pdf,word,excel的数据下载功能做完了,现在又增加了ppt的数据下载功能需求,google了一下,发现这方面的资料不多,在一些国外网站上可以零零散散地找到一些相关的资料,稍作整理,在此给出一个示例。本程序调用了MS的Com组件,因此请先添加引用,笔者在本机安装的是office 2003完整版,添加的引用是:Microsoft PowerPoint 11.0 Object Library。

using MsPPT = Microsoft.Office.Interop.PowerPoint;

asp.net操作PowerPoint程序示例如下:

private void CreatePPT()
{
         MsPPT.Application appPowerPoint = null;
         MsPPT.Presentations ppt = null;
         MsPPT._Presentation presentation = null;
         MsPPT.Slides slides = null;
         MsPPT._Slide slide = null;
         MsPPT.TextRange textRange = null;
         MsPPT.Shape shape = null;
         MsPPT.Shapes shapes = null;
         MsPPT.Table table = null;
         string fileName = string.Format("{0}/{1}.ppt", Server.MapPath("."), DateTime.Now.Ticks.ToString());
         string strTemplate = "C:\\Program Files\\Microsoft Office\\Templates\\Presentation Designs\\Blends.pot";
         try
         {
                 appPowerPoint = new MsPPT.ApplicationClass();
                 appPowerPoint.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
                 ppt = appPowerPoint.Presentations;
                 // 根据模板创建新的演示文稿
                 presentation = ppt.Open(strTemplate,
                 Microsoft.Office.Core.MsoTriState.msoFalse,
                 Microsoft.Office.Core.MsoTriState.msoTrue,
                 Microsoft.Office.Core.MsoTriState.msoTrue);
                 slides = presentation.Slides;
                 // 为幻灯片添加文本
                 slide = slides.Add(1, Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutTitle);
                 textRange = slide.Shapes[1].TextFrame.TextRange;
                 textRange.Text = "我的示例幻灯片";
                 /// 新建一张幻灯片
                 slide = slides.Add(2, Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutText);
                 textRange = slide.Shapes[1].TextFrame.TextRange;
                 textRange.Text = "第二张幻灯片";
                 // 添加一幅图片
                 slide.Shapes.AddPicture("http://www.baidu.com/img/baidu_logo.gif",
                 Microsoft.Office.Core.MsoTriState.msoFalse,
                 Microsoft.Office.Core.MsoTriState.msoTrue, 100, 150, 270, 129);
                 // 添加一个表格
                 shapes = slide.Shapes;
                 shape = shapes.AddTable(3, 3, 105, 320, 400, 100);
                 table = shape.Table;
                 for (int i = 0; i < table.Rows.Count; i )
                 {
                          for (int j = 0; j < table.Columns.Count; j )
                          {
                                   table.Cell(i 1, j 1).Shape.TextFrame.TextRange.Font.Size = 12;
                                   table.Cell(1 1, j 1).Shape.TextFrame.TextRange.Text = string.Format("[{0},{1}]", i 1, j 1);
                          }
                  }
                 // 保存PPT
                 presentation.SaveAs(fileName,
                 Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsDefault,
                 Microsoft.Office.Core.MsoTriState.msoFalse);
         }
         catch (Exception ex)
         {
                  throw ex;
         }
         finally
         {
                  presentation.Close();
                  appPowerPoint.Assistant.Visible = false;
                  appPowerPoint.Quit();
                  GC.Collect();
         }
}

 

请问楼主怎么把PPT里的“单点此处添加标题”框去掉啊?

recole: 在保存PPT之前加入以下代码,隐藏那些边框就行了
// Microsoft.Office.Interop.PowerPoint.Slides slides
foreach (_Slide s in slides)
{
if (string.IsNullOrEmpty(s.Shapes.Placeholders[1].TextFrame.TextRange.Text))
{
s.Shapes.Placeholders[1].Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
}
if (string.IsNullOrEmpty(s.Shapes.Placeholders[2].TextFrame.TextRange.Text))
{
s.Shapes.Placeholders[2].Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
}
}
阅读更多
想对作者说点什么? 我来说一句

C# PPT Operator

C#导出到PPT

leijf1239848066 leijf1239848066

2015-07-24 21:49:46

阅读数:1775

c# 操作 PPT,动态生成PPT

2012年11月12日 2.62MB 下载

C# 操作PPT的基本方法

2011年04月16日 879KB 下载

.net组件开发ppt

2009年07月09日 173KB 下载

没有更多推荐了,返回首页

不良信息举报

C#.net 操作 ppt

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭