现在的位置: 首页 > 综合 > 正文

使用C#为PowerPoint文档插入部分文本或图片

2018年05月14日 ⁄ 综合 ⁄ 共 1342字 ⁄ 字号 评论关闭

using System;

using System.Collections.Generic;

using Sytem.Text;

using System.IO;

using PPT = Microsoft.Office.Interop.PowerPoint;

using System.Reflection;

namespace WritePPTDemo

{

  class Program

  {

    static void Main(string[] args)

    {

      string path;

      PPT.Application pptApp;

      PPT.Presentation pptDoc;

      path = @"d:/test.ppt";

      pptApp  = new PPT.ApplicationClass();

      if(File.Exists(path))

      {

        File.Delete(path);

      }

      Object nothing = Missing.Value;

      pptDoc = pptApp.Presentations.Add(Microsoft.Office.Core.msoTriState.msoFalse);

      pptDoc.Slides.Add(1,Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayouText);

      string text = "示例文本";

      foreach(PPT.Slide slide in pptDoc.Slides)

      {

        foreach(PPT.Shape shape in slide.Shapes)

        {

          shape.TextFrame.TextRange.InsertAfter(text);

        }

      }

      /*

      //添加图片

      pptDoc.Slides.Add(1,Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayouBlank);

      string pic = @"d:/test.bmp";

      foreach(PT.Slide slide int pptDoc.Slides)

      {

        slide.Shape.AddPicture(pic,Microsoft.Office.Core.MsoTriState.msoFalse,

           Microsoft.Office.Core.MsoTriState.msoTure,150,150,300,200);

      }

      */

      PPT.PpSaveAsFileType format = PPT.PpSaveAsFileType.ppSaveAsDefault;

      pptDoc.SaveAs(path,format,Microsoft.Office.Core.MsoTriState.msoFalse);

      pptDoc.Close();

      pptApp.Quit();

    }

  }

抱歉!评论已关闭.