Csharp调用微软COM转换ppt为HTML

使用微软的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;
        }
    }
}


阅读更多

更多精彩内容