• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > C#实现将PPT转换成HTML的方法

C#实现将PPT转换成HTML的方法

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了c#实现接口,c#实现计算器,c#实现登陆界面,c#实现增删改查,c#实现动画效果等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文是一个C#的小程序,主要实现将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 + " 创建完毕!");
     }
   }
 }
 
</div>

以上程序是使用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>您可以点击 <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

    



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C#中实现判断某个类是否实现了某个接口
  • C#实现控制Windows系统关机、重启和注销的方法
  • C#实现简单播放mp3的方法
  • C# 实现计算生辰八字
  • C#实现图片上传与浏览切换的方法
  • 使用C#实现在word中插入页眉页脚的方法
  • C#实现快捷键的几种常用方法汇总
  • C#实现将PPT转换成HTML的方法
  • 基于C#实现的仿windows左侧伸缩菜单效果
  • C#实现修改系统时间的方法

相关文章

  • 2017-05-28再议C#中的装箱与拆箱的问题详解
  • 2017-05-28C#中const 和 readonly 修饰符的用法详解
  • 2017-05-28在C#中根据HardwareID获取驱动程序信息的实现代码
  • 2017-05-28在winform下实现左右布局多窗口界面的方法之续篇
  • 2017-05-28解析c#显示友好时间的实现代码
  • 2017-05-28c#之圆形无标题栏椭圆窗体的实现详解
  • 2017-05-28C#获取进程和对进程的操作
  • 2017-05-28C#中如何执行存储过程方法
  • 2017-05-28c#创建浮动工具栏功能示例
  • 2017-05-28WinForm实现页面按钮定时隐藏功能

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • Unity3D动态对象优化代码分享
    • C#判断一天、一年已经过了百分之多少的方法
    • C#基于OLEDB获取Excel文件表结构信息的方法
    • C# 利用ICSharpCode.SharpZipLib实现在线压缩和解压缩
    • c# 引用类型与值类型的区别详解
    • C#中Activator.CreateInstance()方法用法分析
    • C#使用foreach语句遍历集合类型的方法
    • C#从控制台读取字符串的方法
    • c#代码自动修改解决方案下任意文件实例
    • C# 中的??操作符浅谈

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有