这只是一个小程序,就是将ppt转换成html,方法很多,为了以后备用,在此记录一下,也和大家分享 源码如下: using System; using System.Collections.Generic; using System.Text; using System.IO; using PPT = Microsoft.Office.Interop.PowerPoint; using

这只是一个小程序,就是将ppt转换成html,方法很多,为了以后备用,在此记录一下,也和大家分享
  源码如下:

using System;
  using System.Collections.Generic;
  using System.Text;
  using System.IO;
  using PPT = Microsoft.Office.Interop.PowerPoint;
  using System.Reflection;

namespace WritePptDemo
  {
      class Program
      {
          static void Main(string[] args)
          {
              string   path;         //文件路径变量

PPT.Application pptApp;     //Excel应用程序变量
                PPT.Presentation pptDoc;     //Excel文档变量

PPT.Presentation pptDoctmp;

path   = @"C:\MyPPT.ppt";      //路径
              pptApp =   new PPT.ApplicationClass();   //初始化

//如果已存在,则删除
              if   (File.Exists((string)path))
              {
                    File.Delete((string)path);
              }

//由于使用的是COM库,因此有许多变量需要用Nothing代替
              Object   Nothing = Missing.Value;
              pptDoc =   pptApp.Presentations.Add(Microsoft.Office.Core.MsoTriState.msoFalse);
                pptDoc.Slides.Add(1,   Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutText);

string text = "示例文本";

foreach   (PPT.Slide slide in pptDoc.Slides)
              {
                    foreach (PPT.Shape shape in slide.Shapes)
                    {
                        shape.TextFrame.TextRange.InsertAfter(text);
                    }
              }

//WdSaveFormat为Excel文档的保存格式
                PPT.PpSaveAsFileType format = PPT.PpSaveAsFileType.ppSaveAsDefault;

//将excelDoc文档对象的内容保存为XLSX文档 
                pptDoc.SaveAs(path, format, Microsoft.Office.Core.MsoTriState.msoFalse);

//关闭excelDoc文档对象 
                pptDoc.Close();

//关闭excelApp组件对象 
                pptApp.Quit();

Console.WriteLine(path + " 创建完毕!");

Console.ReadLine();

string   pathHtml = @"c:\MyPPT.html";

PPT.Application pa = new PPT.ApplicationClass();

pptDoctmp = pa.Presentations.Open(path,   Microsoft.Office.Core.MsoTriState.msoTrue,   Microsoft.Office.Core.MsoTriState.msoFalse,   Microsoft.Office.Core.MsoTriState.msoFalse);
                PPT.PpSaveAsFileType formatTmp = PPT.PpSaveAsFileType.ppSaveAsHTML;
                pptDoctmp.SaveAs(pathHtml, formatTmp,   Microsoft.Office.Core.MsoTriState.msoFalse);
                pptDoctmp.Close();
              pa.Quit();
                Console.WriteLine(pathHtml + " 创建完毕!");
          }
      }
  }
 
 
  以上程序是使用C# 先创建一个ppt 文件并向里面写入了文字,然后再把此ppt 转换成html  ,对于上面程序需要说明的其实就是
  引用dll的问题, 在项目中添加引用,在com 组件中选择 microsoft powerpoint 11.0 object   library ,而如果你的电脑没有安装 office 2003 是不会有这个的,而如果安装的是office 2007 则是microsoft powerpoint 12.0 object   library。而且即使你引用成功后,还是会编译不通过,是因为少安装了 office   PIA  ,在安装office 时,如果你是选择的典型安装是不会安装这个的,因为这个只针对开发人员才会用到。可以到网上下载一个 office PIA 安装下就ok了
 
  ----------------------------------Office文件转换成Html格式---------------------------------------------------------------------------------------------------------------------------------------------------------
  
  
  
 using   Microsoft.Office.Core;
 using Microsoft.Office.Interop.PowerPoint;

using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Web;
  using System.IO;
  using System.Text.RegularExpressions;
  using Nd.Webs;
  using Aspose.Cells;
  using Aspose.Words;
  using ND.CommonHelper;
  using Microsoft.Office.Interop.PowerPoint;
  using Microsoft.Office.Core;

namespace Api.Note.Base
  {
      #region Office文件转换成Html格式
      class OfficeHtmlBo
      {
          #region InsertHeadHtml
          /// <summary>
          /// InsertHeadHtml
          /// </summary>
          /// <param   name="WordFilePath">InsertHeadHtml</param>
          private string   InsertHeadHtml(string strHtml, string realPath)
          {
              int index   = strHtml.IndexOf("<body");
              strHtml =   strHtml.Insert(index, "<div   style='height:60px;font-size:14px;margin:0px 0px   12px 0px;padding:14px 4px 12px 12px;line-height:24px;height:1%;'>以下是该文档的HTML预览效果。<br/><span>由于是网页提取显示word中的内容,有可能部分显示与源文档中有差异,如想查看更准确的信息,</span>您可以点击&nbsp;<a   style='color:6699FF;text-decoration:underline;'   href='/Lib/UdControls/Download.aspx?action=Download&appFormCode=" +   HttpContext.Current.Request.QueryString["appFormCode"].ToString() +   "&path=" +   HttpContext.Current.Request.QueryString["path"].ToString() +   "&encrypt=" + HttpContext.Current.Request.QueryString["encrypt"].ToString()   + "'><b>下载原始附件</b></a></div>");

Regex   reg = new   Regex(@"(?<start><img[^>]+?src="")(?<src>[^""]+?)(?<end>""[^>]+?>)");
              strHtml =   reg.Replace(strHtml, delegate(Match m)
              {
                    return string.Format("{0}{1}{2}{3}",
                        m.Groups["start"].Value,
                        realPath,
                        m.Groups["src"].Value,
                        m.Groups["end"].Value
                        );
              });

return strHtml;
          }
          #endregion

#region GetLeftStr
          /// <summary>
          /// 截取字符串左边指定长度
          /// </summary>
          /// <param   name="str"></param>
          /// <param   name="length"></param>
          ///   <returns></returns>
          public string GetLeftStr(string   str, int length)
          {
              length =   length * 2;
              string   tempStr = "";
              int i = 0;
              foreach   (char c in str)
              {
                    tempStr += c.ToString();
                    if (((int)c >= 33) && ((int)c <= 126))
                    {
                        //字母和符号原样保留 
                        i += 1;
                    }
                    else
                    {
                        i += 2;
                    }
                    if (i >= length)
                    {
                        return tempStr;
                    }
              }
              return   str;
          }

#endregion

#region 将Word文档转换成HTML格式
          /// <summary>
          /// 将Word文档转换成HTML格式
          /// </summary>
          /// <param   name="WordFilePath">Word文档格式</param>
          private void WordToHtmlFile(string   WordFilePath)
          {
              try
              {
                    // 指定原文件和目标文件
                    string realPath = WordFilePath.Substring(0,   WordFilePath.LastIndexOf("/") + 1);
                    WordFilePath = System.Web.HttpContext.Current.Server.MapPath(WordFilePath);
                    object target = WordFilePath.Substring(0,   WordFilePath.LastIndexOf(".")) + ".html";
                    //string realPath = WordFilePath.Substring(0,   WordFilePath.LastIndexOf(".")) + ".html";

if (!File.Exists(target.ToString()))
                    {
                        Document doc = new Document(WordFilePath);
                        doc.Save(target.ToString(), SaveFormat.Html);
                    }

StreamReader sr = new StreamReader(target.ToString(), Encoding.Default);
                    string strHtml = sr.ReadToEnd();

strHtml = InsertHeadHtml(strHtml, realPath);
                    HttpContext.Current.Response.Write(strHtml);

sr.Close();
              }
              catch   (Exception ex)
              {
                    //记录异常
                    LogEntry logEntry = new LogEntry();
                    logEntry.Message = ex.Message;
                    logEntry.Title = "---->将Word文档转换成HTML格式异常[WordToHtmlFile]";
                    logEntry.TimeStamp = DateTime.Now;
                    logEntry.LogEntryType = LogEntryType.Error;
                    logEntry.LogCatalog = LogCatalog.ExceptionLog;
                    logEntry.StackTrace = ex.StackTrace;
                    LogPosition logPosition = LogPosition.FileLog;
                    string positionParameter =   SysConfig.ToString(SysConfig.GetAppSetting("LogPath"));
                    SysLogger.Write(logEntry, logPosition, positionParameter);
              }
          }
          #endregion

#region 将Excel文件转换成HTML格式
          /// <summary>
          /// 将Excel文件转换成HTML格式
          /// </summary>
          /// <param   name="ExcelFilePath">Excel文件路径</param>
          private void   ExcelToHtmlFile(string ExcelFilePath)
          {
              try
              {
                    string realPath = ExcelFilePath.Substring(0,   ExcelFilePath.LastIndexOf("/") + 1);
                    int index = ExcelFilePath.LastIndexOf("/");
                    string fileName;
                    if (ExcelFilePath.IndexOf(":") != -1)
                    {
                        fileName = ExcelFilePath.Split(new char[] { ':' })[0].ToString();
                        fileName = GetLeftStr(fileName.Substring(0,   fileName.LastIndexOf(".")), 10) +   fileName.Substring(fileName.LastIndexOf("."));
                        fileName = HttpUtility.UrlEncode(fileName, Encoding.UTF8);
                    }
                    else
                    {
                        fileName = ExcelFilePath.Substring(index + 1, ExcelFilePath.Length - index -   1);
                        fileName = GetLeftStr(fileName.Substring(0,   fileName.LastIndexOf(".")), 10) +   fileName.Substring(fileName.LastIndexOf("."));
                        //编码
                        fileName = HttpUtility.UrlEncode(fileName, Encoding.UTF8);
                    }
                    fileName = fileName.Substring(0, fileName.LastIndexOf("."));
                    ExcelFilePath = System.Web.HttpContext.Current.Server.MapPath(ExcelFilePath);
                    //目标html文件路径
                    object target = ExcelFilePath.Substring(0,   ExcelFilePath.LastIndexOf(".")) + ".html";

string target2 = ExcelFilePath.Substring(0,   ExcelFilePath.LastIndexOf("\\")) + "\\" + fileName +   "_files\\sheet001.htm";
                    if (!File.Exists(target.ToString()))
                    {
                        //为了保险,只读方式打开 
                        //object readOnly = true;
                        //// 指定另存为格式(html) 
                        //object format = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;

////打开Excel文件
                        //oBook = excelApp.Workbooks.Open(ExcelFilePath, Unknown, readOnly,
                        //    Unknown, Unknown, Unknown, Unknown, Unknown, Unknown,
                        //    Unknown, Unknown, Unknown, Unknown, Unknown, Unknown);

//// 转换格式 
                        //oBook.SaveAs(target, format, Unknown, Unknown, Unknown, Unknown,
                        //       Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
                        //     Unknown, Unknown, Unknown, Unknown, Unknown);

Workbook wBook = new Workbook();
                        wBook.Open(ExcelFilePath);
                        wBook.Save(target.ToString(), FileFormatType.Html);
                    }

StreamReader sr = new StreamReader(target2.ToString(), Encoding.Default);
                    string strHtml = sr.ReadToEnd();

strHtml = InsertHeadHtml(strHtml, realPath);

strHtml = strHtml.Replace("window.location.replace", "");
                    strHtml = strHtml.Replace("filelist.xml", realPath + "/"   + fileName + "_files/filelist.xml");
                    strHtml = strHtml.Replace("stylesheet.css", realPath +   "/" + fileName + "_files/stylesheet.css");
                    HttpContext.Current.Response.Write(strHtml);

sr.Close();
              }
              catch   (Exception ex)
              {
                    //记录异常
                    LogEntry logEntry = new LogEntry();
                    logEntry.Message = ex.Message;
                    logEntry.Title = "---->将Excel文件转换成HTML格式[ExcelToHtmlFile]";
                    logEntry.TimeStamp = DateTime.Now;
                    logEntry.LogEntryType = LogEntryType.Error;
                    logEntry.LogCatalog = LogCatalog.ExceptionLog;
                    logEntry.StackTrace = ex.StackTrace;
                    LogPosition logPosition = LogPosition.FileLog;
                    string positionParameter =   SysConfig.ToString(SysConfig.GetAppSetting("LogPath"));
                    SysLogger.Write(logEntry, logPosition, positionParameter);
              }
          }
          #endregion

#region 将PPT文件转换成HTML格式
          /// <summary>
          /// 将PPT文件转换成HTML格式
          /// </summary>
          /// <param   name="PptFilePath">PPT文件路径</param>
          private void PptToHtmlFile(string   PptFilePath)
          {
                ApplicationClass ac = new ApplicationClass();
                Presentation pptFile = null;
              try
              {
                    string realPath = PptFilePath.Substring(0,   PptFilePath.LastIndexOf(".")) + ".html";
                    PptFilePath = System.Web.HttpContext.Current.Server.MapPath(PptFilePath);
                    //获得html文件名
                    object target = PptFilePath.Substring(0,   PptFilePath.LastIndexOf(".")) + ".html";

if (!File.Exists(target.ToString()))
                    {
                        if (PptFilePath.Contains(".pptx"))
                        {
                            pptFile = ac.Presentations.Open2007(PptFilePath, MsoTriState.msoCTrue,   MsoTriState.msoCTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
                            pptFile.SaveAs(target.ToString(), PpSaveAsFileType.ppSaveAsHTML,   MsoTriState.msoCTrue);
                        }
                        else if (PptFilePath.Contains(".ppt"))
                        {
                            pptFile = ac.Presentations.Open(PptFilePath, MsoTriState.msoCTrue,   MsoTriState.msoCTrue, MsoTriState.msoFalse);
                            pptFile.SaveAs(target.ToString(), PpSaveAsFileType.ppSaveAsHTML,   MsoTriState.msoCTrue);
                        }
                    }
                    //StreamReader sr = new StreamReader(target.ToString(), Encoding.Default);
                    //string strHtml = sr.ReadToEnd();
                    //Response.Write(strHtml);
                    HttpContext.Current.Response.Redirect(realPath);
              }
              finally
              {
                    if (pptFile != null)
                    {
                        pptFile.Close();
                    }
                    ac.Quit();
                    GC.Collect();
              }
          }
          #endregion
      }
      #endregion

}

c# 将PPT转换成HTML的更多相关文章

  1. 杂项-PPT:如何把幻灯片ppt转换成视频

    ylbtech-杂项-PPT:如何把幻灯片ppt转换成视频 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 1. https://jingyan.baidu.co ...

  2. PDF转换成Word,ppt转换成word

    pdf与word我没找到直接转换的方式,不过可以用间接方式嘛! pdf ==>picture ==>word!ppt转word的原理也是先把ppt转成图片,再把图片插入word! 先准备好 ...

  3. C#.net word excel powerpoint (ppt) 转换成 pdf 文件

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  4. 将ppt转换成PDF

    import sys import os import glob import win32com.client def convert(files, formatType = 32): powerpo ...

  5. word ppt excel文档转换成pdf

    1.把word文档转换成pdf (1).添加引用 using Microsoft.Office.Interop.Word; 添加引用 (2).转换方法 /// <summary> /// ...

  6. C# 将PowerPoint文件转换成PDF文件

    PowerPoint的优势在于对演示文档的操作上,而用PPT查看资料,反而会很麻烦.这时候,把PPT转换成PDF格式保存,再浏览,不失为一个好办法.在日常编程中和开发软件时,我们也有这样的需要.本文旨 ...

  7. Java中Office(word/ppt/excel)转换成HTML实现

    运行条件:JDK + jacob.jar + jacob.dll 1) 把jacob.dll在 JAVA_HOME\bin\ 和 JAVA_HOME\jre\bin\ 以及C:\WINDOWS\sys ...

  8. 如何用ABBYY把PDF转换成PPT

    在电子科技迅速发展的今天,文件格式转换并不是什么稀罕事,因为现在都是电子化办公,出现很多文件格式,但是不同的场合需要的格式不同,所以常常需要进行文件格式的转换.PDF转换成PPT也是众多文件格式转换中 ...

  9. PDF文件怎么转换成PPT

    在日常办公中大家都会发现PDF文件目前是比较常见的一种文件,有的时候大家会需要将PDF转换成PPT为了去更好的演示,毕竟PPT文件在演示方面具有着较好的特点,那如何将PDF文件转换成PPT文件呢,今天 ...

随机推荐

  1. 【转】iPhone屏幕尺寸、分辨率及适配

    原文网址:http://blog.csdn.net/phunxm/article/details/42174937 1.iPhone尺寸规格 设备 iPhone 宽 Width 高 Height 对角 ...

  2. (function(){})()这个是什么?有不明觉厉的感觉么?

    今天在RunJs上看到一个人分享的一个jquery代码,写的是jquery弹性滑动效果.不过,看着看着,发现一句代码(function{})(),突然有种不明觉厉的感觉. 事实上,只是因为我们没有用过 ...

  3. why dicePlayer cannot player with defy mb526

    硬件加速视频播放器 DicePlayer v2.0.38 ... ..... ...... ........ \ 局限性:- 视频兼容性依赖于您设备的视频硬解码能力

  4. Android 模拟器中sdcard操作

    1.  在模拟器中创建sdcard目录,方法如下: 1. mksdcard命令 用cmd进入SDK的Tools目录,执行mksdcard命令.会出现如下帮助信息 我们可以看到sdcard image支 ...

  5. 祭奠我的csdn博客

    本人在csdn的博客莫名其妙地被封了(http://blog.csdn.net/fty8788),非常郁闷. 回忆起,可能是我近半年由于工作事情忙很少写博客了,被某213盗用发了不恰当的东东.我也查不 ...

  6. ubuntu开发软件的安装

    今天下午发现ubuntu12.04坏了,无奈只能重新安装,建议读者配置自己的ubuntu后备份一个,免得坏了重新安装,花了两个小时才把ubuntu的交叉环境弄好,其中搭建了tptp通信协议,还有arm ...

  7. 在xcode上搭建OpenGL3.x运行环境

    最近开始学习OpenGL,网上的教程太散乱,于是打算照着红宝书<OpenGL编程指南(第七版)>来学习. 于是在Mac上搭建一下Demo环境.比较方便的是,OS X上已经装了OpenGL ...

  8. Python 核心数据类型

    1.Python中一切皆对象 2.Python中不需要申明对象类型,对象的类型由运行的表达式决定 3.创建了对象意味着绑定了对象的操作到此对象,也就是在固有的对象上只能调用该对象特有的操作.比如只能将 ...

  9. Cloudera 建议使用 NTP 使 Hadoop 群集实现时间同步

    主机的 NTP 服务未响应时钟偏差请求. 建议 这是主机运行状况测试,用于检查主机的系统时钟是否与其 NTP 服务器不同步.该测试能检查“ntpdc -c loopinfo”命令报告的主机时钟偏差绝对 ...

  10. 在WinForm编程中犯的一些错误

    1.一直以为,MouseClick事件在鼠标点击时发生,MouseDoubleClick事件在鼠标双击时发生.那么在单击鼠标时会调用MouseClick事件处理程序,双击鼠标时会调用MouseDoub ...