Csharp调用微软COM转换ppt为HTML
2015-07-10 15:58
447 查看
使用微软的office中的ppt软件只要使用另存为就可以把一个ppt保存为HTML网页文件。但如何通过程序调用完成转化呢?
以下使用office 2007为例,其他版本略有不同。 1添加引用,对于不同的版本,有所不一样。当然前提自然是安装了对应的微软office软件。2转换代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PPT = Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;
namespace CommonConvert
{
public class PPTToHtml
{
/// <summary>
/// 使用反射调用方法,返回生成的HTML文件路径
/// </summary>
/// <param name="pptFullFileName"></param>
/// <returns></returns>
public string PptToHtmlFile(object pptFullFileName)
{
//在此处放置用户代码以初始化页面
PPT.Application ppt = new PPT.Application();
Type wordType = ppt.GetType();
PPT.Presentations docs = ppt.Presentations;
//打开文件
Type docsType = docs.GetType();
PPT.Presentation doc = (PPT.Presentation)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { pptFullFileName, false,false,false });
// PPT.Presentation doc = (PPT.Presentation)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { pptFullFileName});//后三个参数不设置,会出现错误Presentations.Open : Invalid request. The PowerPoint Frame window does not exist.
Type docType = doc.GetType();
string htmlFullFileName = pptFullFileName + ".html"; //HTML文件路径
object ofmt = PPT.PpSaveAsFileType.ppSaveAsHTML;
//转换格式,另存为
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { htmlFullFileName, ofmt });
docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
//退出
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, ppt, null);
return htmlFullFileName;
}
/// <summary>
/// 直接调用方法,不使用反射
/// </summary>
/// <param name="pptFullFileName"></param>
/// <returns></returns>
public string PptToHtmlFile2(string pptFullFileName)
{
//在此处放置用户代码以初始化页面
PPT.Application ppt = new PPT.Application();
PPT.Presentations docs = ppt.Presentations;
//打开文件
PPT.Presentation doc = docs.Open(pptFullFileName, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
// PPT.Presentation doc = docs.Open(pptFullFileName); //后三个参数不设置,会出现错误Presentations.Open : Invalid request. The PowerPoint Frame window does not exist.
string htmlFullFileName = pptFullFileName + ".html"; //HTML文件路径
//转换格式,另存为
doc.SaveAs(htmlFullFileName, PPT.PpSaveAsFileType.ppSaveAsHTML);
doc.Close();
//退出
ppt.Quit();
return htmlFullFileName;
}
}
}
相关文章推荐
- Csharp调用微软COM转换excel为HTML
- Csharp调用微软COM转换word为HTML
- C#笔记 委托 事件 匿名方法 Action
- c# listview 控件
- C# 日期格式化
- c# 窗口关闭提示
- C# const, readonly, static readonly
- C#中值类型与引用类型通俗理解
- C#类的成员(字段、属性、方法)
- C# 接口
- C#类和类的实例
- C#修饰符
- C#流程控制语句
- C# 抽象类
- 在C#中数组,ArrayList,List都能够存储一组对象,那么这三者到底有什么样的区别呢
- C#的handler.post runnable实现: delegate
- C#语法糖之 ReflectionSugar 通用反射类
- 开源Math.NET基础数学类库使用(11)C#计算相关系数
- C# 继承
- C#上传demo