最近在写一个工具,设计到将界面内容到处到PPT中,且导出的内容能够编辑。网上搜了很多C#导出到PPT的方法,无非都是官方文档稍微改改到处传。因此结合MSDN的文档外加自己的摸索,将对PPT的操作封装了一下,里面包含几个常用的方法:添加文本框、直线、箭头、矩形、图片。后面有机会再继续扩展。
注:这里只给出了封装的类,直接使用可能会有问题,记得添加Office2007对应组件的引用。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Core;
using PPT = Microsoft.Office.Interop.PowerPoint;
using System.Windows;
using System.Collections;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using Tools.functionModel.file;
using System.Drawing;
using System.Drawing.Imaging;
//using System.Windows.Controls;
namespace Tools.baseModel.common {
/// <summary>
/// PPT文档操作实现类.
/// </summary>
public class pptBase {
private string temPath = "";
private string pptPath = "";
#region=========基本的参数信息=======
PPT.Application pptApp; //PPT应用程序变量
public PPT.Application PptApp {
get { return pptApp; }
set { pptApp = value; }
}
PPT.Presentation pptDoc; //PPT文档变量
PPT.Slides pptSlides = null;
PPT.Slide pptSlide = null;
private int pageCount=0;
#endregion
public PPT.Shapes Shapes {
get { return pptSlide.Shapes; }
}
public pptBase(string path) {
this.temPath = commonPath.fiberFolder + "/other/template.pot";
this.pptPath = path;
//如果已存在,则删除
if (File.Exists((string)pptPath)) {
File.Delete((string)pptPath);
}
FileInfo file = new FileInfo(this.temPath);
pptApp = new PPT.Application(); //初始化
pptApp.Visible = MsoTriState.msoTrue;