對PPT的操作(文字替換,圖片插入等)
阿新 • • 發佈:2019-01-04
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();
}
}
}
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();
}
}
}