对PPT的操作(文字替换,图片插入等)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using POWERPOINT = Microsoft.Office.Interop.PowerPoint;
using OFFICECORE = Microsoft.Office.Core;
using Microsoft.SqlServer.Server;
using Microsoft.Office.Core;


namespace ConsoleApp1
{
    class Program
    {
        public static POWERPOINT.Application objApp =null;
        public static POWERPOINT.Presentation objPresSet = null;
        POWERPOINT.SlideShowWindows objSSWs;
        POWERPOINT.SlideShowTransition objSST;
        POWERPOINT.SlideShowSettings objSSS;
        POWERPOINT.SlideRange objSldRng;
        static void Main(string[] args)
        {
            var uuid = Guid.NewGuid().ToString();
            FileRemove(uuid);           
        }
        /// <summary>
        /// 创建模板文件
        /// </summary>
        /// <param name="id">GUID--讲评的ID</param>
        public static void FileRemove(string id)
        {


            //根据GUID生成文件夹
            Directory.CreateDirectory(@"../Upload/" + id);
            //找到原始文件
            String sourcePath = "../Upload/Dome.ppt";
            String targetPath = "../Upload/"+id+"/"+id+".ppt";
            bool isrewrite = true; // true=覆盖已存在的同名文件,false则反之
            File.Copy(sourcePath, targetPath, isrewrite);
            Program program = new Program();
            program.PPTOpen(targetPath);
            //循环编写Code以达到批量替换
            program.ReplaceAll("系统录入编写人", "GK");
            program.PPTpic();
            program.PPTClose();
        }
        /// <summary>
        /// 打开PPT
        /// </summary>
        /// <param name="filePath"></param>
        public void PPTOpen(string filePath)
        {
            //防止连续打开多个PPT程序.
            if (objApp != null) { return; }
            try
            {
                objApp = new POWERPOINT.Application();
                //objApp.ShowInTaskBar
                //objApp.DisplayGuides = MsoTriState.msoFalse;
                FileInfo fileInfo = new FileInfo(@""+filePath+"");              
                objPresSet = objApp.Presentations.Open(fileInfo.FullName, OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoFalse);
                objApp.Assistant.On = false;
                objSSS = objPresSet.SlideShowSettings;
                objSSS.Run();
            }
            catch (Exception ex)
            {
                objApp.Quit();
            }
        }
        public void ReplaceAll(string OldText, string NewText)
        {
            //页面总页数
            int num = objApp.Presentations.Count;


            for (int j = 1; j <= num; j++)
            {
                //循环PPT页
                POWERPOINT.Slide slide = objPresSet.Slides[j];
                for (int i = 1; i <= slide.Shapes.Count; i++)
                {
                    POWERPOINT.Shape shape = slide.Shapes[i];
                    if (shape.TextFrame != null)
                    {
                        POWERPOINT.TextFrame textFrame = shape.TextFrame;
                        try
                        {
                            if (textFrame.TextRange != null)
                            {
                                //替换文字操作方法
                                textFrame.TextRange.Replace(OldText, NewText);
                            }
                        }
                        catch
                        { }
                    }
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public void PPTpic()
        {
                POWERPOINT.CustomLayout slides = objPresSet.Slides[1].CustomLayout;
                objPresSet.Slides.AddSlide(5,slides);
                POWERPOINT.Slide slide = objPresSet.Slides[5];
                slide.Shapes.AddPicture("C:/Users/l/Desktop/ConsoleApp1/ConsoleApp1/Upload/8b5491cd-1e75-42f9-90e8-8de7780f3219.png",MsoTriState.msoFalse,MsoTriState.msoTrue, 1, 1, 700, 500);
            
        }


        // <summary>
        /// 关闭PPT文档。
        /// </summary>
        public void PPTClose()
        {
            //装备PPT程序。
            if (objPresSet != null)
            {
               
                    objPresSet.Save();
            }
            if (objApp != null)
                objApp.Quit();
                GC.Collect();
        }
    }
}
    
 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Robin let

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值