有些时候可能需要将Excel,ppt和word转化为html在页面上显示。我从网上查到一些代码,记录在这里以供需要的朋友参考

1.将word转化为html显示

  1. //========================================================================
  2. // 函数名: WordToHtml
  3. /// <summary>
  4. /// Word转成Html
  5. /// </summary>
  6. /// <param name="wordfilename">word文件名</param>
  7. /*=======================================================================
  8. 变更记录
  9. 序号   更新日期  开发者   变更内容
  10. 0001   2008/07/22 张 新建
  11. =======================================================================*/
  12. public static string WordToHtml(object wordfilename)
  13. {
  14. //在此处放置用户代码以初始化页面
  15. word.Application word = new word.Application();
  16. Type wordtype = word.GetType();
  17. word.Documents docs = word.Documents;
  18. //打开文件
  19. Type docstype = docs.GetType();
  20. word.Document doc = (word.Document)docstype.InvokeMember("open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new object[] { wordfilename, true, true });
  21. //转换格式,另存为
  22. Type doctype = doc.GetType();
  23. string wordsavefilename = wordfilename.ToString();
  24. string strsavefilename = wordsavefilename.Substring(, wordsavefilename.Length - ) + "html";
  25. object savefilename = (object)strsavefilename;
  26. doctype.InvokeMember("saveas", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { savefilename, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
  27. doctype.InvokeMember("close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
  28. // 退出 word
  29. wordtype.InvokeMember("quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
  30. return savefilename.ToString();
  31. }

2.将PPT转化为html显示

  1. //========================================================================
  2. // 函数名: PPTToHtml
  3. /// <summary>
  4. /// PPT转成Html
  5. /// </summary>
  6. /// <param name="pptFilename">PPT文件名</param>
  7. /*=======================================================================
  8. 变更记录
  9. 序号   更新日期  开发者   变更内容
  10. 0001   2008/07/22 张 新建
  11. =======================================================================*/
  12. public string PPTToHtml(string pptFilename)
  13. {
  14. //被转换的html文档保存的位置
  15. string saveFileName = pptFilename + ".html";
  16. Microsoft.Office.Interop.PowerPoint.Application ppt = new Microsoft.Office.Interop.PowerPoint.Application();
  17. Microsoft.Office.Core.MsoTriState m1 = new MsoTriState();
  18. Microsoft.Office.Core.MsoTriState m2 = new MsoTriState();
  19. Microsoft.Office.Core.MsoTriState m3 = new MsoTriState();
  20. Microsoft.Office.Interop.PowerPoint.Presentation pp = ppt.Presentations.Open(pptFilename, m1, m2, m3);
  21. pp.SaveAs(saveFileName, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsHTML, Microsoft.Office.Core.MsoTriState.msoTriStateMixed);
  22. pp.Close();
  23. //返回文件名
  24. return saveFileName;
  25. }

3.将Excel转化为html显示

  1. //========================================================================
  2. // 函数名: ExcelToHtml
  3. /// <summary>
  4. /// Excel转成Html
  5. /// </summary>
  6. /// <param name="excelFileName">Excel文件名</param>
  7. /*=======================================================================
  8. 变更记录
  9. 序号   更新日期  开发者   变更内容
  10. 0001   2008/07/22 张 新建
  11. =======================================================================*/
  12. public string ExcelToHtml(string excelFileName)
  13. {
  14. //实例化Excel
  15. Microsoft.Office.Interop.Excel.Application repExcel = new Microsoft.Office.Interop.Excel.Application();
  16. Microsoft.Office.Interop.Excel.Workbook workbook = null;
  17. Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
  18. //打开文件,n.FullPath是文件路径
  19. workbook = repExcel.Application.Workbooks.Open(excelFileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
  20. worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[];
  21. string filesavefilename = excelFileName.ToString();
  22. string strsavefilename = filesavefilename.Substring(, filesavefilename.Length - ) + "html";
  23. object savefilename = (object)strsavefilename;
  24. object ofmt = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;
  25. //进行另存为操作
  26. workbook.SaveAs(savefilename, ofmt, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
  27. object osave = false;
  28. //逐步关闭所有使用的对象
  29. workbook.Close(osave, Type.Missing, Type.Missing);
  30. repExcel.Quit();
  31. System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
  32. worksheet = null;
  33. //垃圾回收
  34. GC.Collect();
  35. System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
  36. workbook = null;
  37. GC.Collect();
  38. System.Runtime.InteropServices.Marshal.ReleaseComObject(repExcel.Application.Workbooks);
  39. GC.Collect();
  40. System.Runtime.InteropServices.Marshal.ReleaseComObject(repExcel);
  41. repExcel = null;
  42. GC.Collect();
  43. //依据时间杀灭进程
  44. System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("EXCEL");
  45. foreach (System.Diagnostics.Process p in process)
  46. {
  47. if (DateTime.Now.Second - p.StartTime.Second > && DateTime.Now.Second - p.StartTime.Second < )
  48. {
  49. p.Kill();
  50. }
  51. }
  52.  
  53. return savefilename.ToString();
  54. }

以上是转换成为html文件的方法,转换成功后会在文件夹下会生对应的图片文件夹,和样式文件,和html文件。其中如果Excel中包含多个sheet时,转化后的html会包含多个tab,如果想每个sheet转化为一个页面,则需要修改代码

将Excel,ppt和word转化为html的更多相关文章

  1. asp.net 将ppt,word转化为pdf实现在线浏览详解

    1.首先添加应用:COM里面的Micsosoft Office 12.0 Object Library(VS2013基本都有14.0或者15.0 有的话一样的添加,因为我的没有只有12.0) : 2. ...

  2. PDF/WORD/EXCEL/PPT 文档在线阅读

    查资料看了2种解决方法: 1.通过办公软件dll转换,用flans去看 2.通过Aspose转换成pdf格式,在用js前台读pdf(我用的pdf.js) 今天我解决的就是WORD/EXCEL/PPT ...

  3. Atitit.office word  excel  ppt pdf 的web在线预览方案与html转换方案 attilax 总结

    Atitit.office word  excel  ppt pdf 的web在线预览方案与html转换方案 attilax 总结 1. office word  excel pdf 的web预览要求 ...

  4. 在线文档转换API word,excel,ppt等在线文件转pdf、png

    在线文档转换API提供word,excel,ppt等在线文件转pdf.png等,文档:https://www.juhe.cn/docs/api/id/259 接口地址:http://v.juhe.cn ...

  5. Office办公软件(Excel PPT Word)使用整理

    Office办公软件(Excel PPT Word)使用整理.. -------------- Excel默认打印预览于当前连接的打印机的纸张大小保持一致. Excel sheet不见了怎么办 --- ...

  6. java 如何将 word,excel,ppt如何转pdf--jacob

    问题:java 如果将 word,excel,ppt如何转pdf 我个人的观点:windows server下用 jacob; linux server下 用openoffice.   PS:1.本文 ...

  7. java 如何将 word,excel,ppt如何转pdf --openoffice (1)

    承上启下,可折叠 上一篇说的是:服务器是windows server时,用jacob将msoffice(指的是word,excel,ppt)转换成pdf. 若被部署项目的服务器是centOS等linu ...

  8. Aspose是一个很强大的控件,可以用来操作word,excel,ppt等文件

    Aspose是一个很强大的控件,可以用来操作word,excel,ppt等文件,用这个控件来导入.导出数据非常方便.其中Aspose.Cells就是用来操作Excel的,功能有很多.我所用的是最基本的 ...

  9. word/excel/ppt 2 PDF

    PHP 实现 word/excel/ppt 转换为 PDF 一般最常见的就是利用OpenOffice来转换,来看看实现的核心代码: class PDFConverter { private $com; ...

随机推荐

  1. 浅谈.NET的缓存(依赖和过期)

    Cache 线程安全,相当于static Arraylist. 缓存过期机制 1.设置过期时间 a.可设置过期时间 Cache.Insert());//设置10分钟过期 b.绝对过期时间 Cache. ...

  2. jsp 相关特性

    下面要注意一个问题,“只要浏览器关闭,session就消失”,那么这个论断是否正确呢?对于session除非程序通知服务器删除一个 session,否则服务器会一直保留,程序一般都是在用户做log o ...

  3. javascript原型和原型继承

    每一个javascript对象(null除外)都和原型对象相关联,每一个对象都从原型对象继承属性. 所有通过对象直接量创建的对象都具有同一个原型对象,并可以通过javascript代码Object.p ...

  4. hdoj 1231 最大连续子序列

    最大连续子序列 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  5. redis缓存技术

    初学redis缓存技术,如果文章写得不好还请谅解 应用环境:win7 实现环境:cmd,eclipse redis缓存技术的特点就在于高效,因为目前涉及的数据量逐渐增多,在对于数据的存储上面和sql以 ...

  6. 用NGUI做一个计时条!

    1.建立两个UISprite. 2.建立脚本CountingTime 3.编写脚本 public class CountTime : MonoBehaviour { //时间计时器 public fl ...

  7. JDK之jstat的用法

    http://www.51testing.com/html/92/77492-203728.html jstat的用法 用以判断JVM是否存在内存问题呢?如何判断JVM垃圾回收是否正常?一般的top指 ...

  8. Python递归遍历目录下所有文件

    #自定义函数: import ospath="D:\\Temp_del\\a"def gci (path): """this is a stateme ...

  9. S2SH商用后台权限系统第三讲

    个位博友: 您好!今天我们做下登录页面,已经如何登录系统.我们的登录页面很简单,用户名.密码.验证码.下面首先描述下验证码的概念,验证码是为了防止机器人恶意登录.我们这里的验证码采用4位数字,当然你也 ...

  10. 【转】Sqlserver通过链接服务器访问Oracle的那些事儿!

    原文:http://blog.sina.com.cn/s/blog_614b6f210100t80r.html 前言:1.不经历风雨,怎能见彩虹.2.充分利用BaiDu.google等搜索引擎查找资料 ...