C#实现将PPT转换成HTML的方法
更新时间:2020年6月25日 11:35 点击:1978
本文是一个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 + " 创建完毕!"); } } }
以上程序是使用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 #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#做项目的时候,基本上都需要制作登录界面,那么今天我们就来一步步看看,如果简单的实现登录界面呢,本文给出2个例子,由简入难,希望大家能够喜欢。...2020-06-25
- 这篇文章主要介绍了C# 字段和属性的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下...2020-11-03
使用PHP+JavaScript将HTML页面转换为图片的实例分享
这篇文章主要介绍了使用PHP+JavaScript将HTML元素转换为图片的实例分享,文后结果的截图只能体现出替换的字体,也不能说将静态页面转为图片可以加快加载,只是这种做法比较interesting XD需要的朋友可以参考下...2016-04-19- 这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-03
- 本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
- 这篇文章主要介绍了C#实现简单的Http请求的方法,以实例形式较为详细的分析了C#实现Http请求的具体方法,需要的朋友可以参考下...2020-06-25
- 本文主要介绍了C#中new的几种用法,具有很好的参考价值,下面跟着小编一起来看下吧...2020-06-25
使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)
这篇文章主要介绍了使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25- 这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
- 这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
- 最近做一个小项目不可避免的需要前端脚本与后台进行交互。由于是在asp.net中实现,故问题演化成asp.net中jiavascript与后台c#如何进行交互。...2020-06-25
- 本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
- 轻松学习C#的基础入门,了解C#最基本的知识点,C#是一种简洁的,类型安全的一种完全面向对象的开发语言,是Microsoft专门基于.NET Framework平台开发的而量身定做的高级程序设计语言,需要的朋友可以参考下...2020-06-25
- 本文主要介绍了C#变量命名规则小结,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-09
- 这篇文章主要介绍了C#绘制曲线图的方法,以完整实例形式较为详细的分析了C#进行曲线绘制的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
- 这篇文章主要介绍了c#自带缓存使用方法,包括获取数据缓存、设置数据缓存、移除指定数据缓存等方法,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了c#中(&&,||)与(&,|)的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
- 这篇文章主要用实例讲解C#递归算法的概念以及用法,文中代码非常详细,帮助大家更好的参考和学习,感兴趣的朋友可以了解下...2020-06-25
- 下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25