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;
}
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值