以下使用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;}}
}
本文发布于:2024-02-02 20:58:25,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170687872146402.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |