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

1.将word转化为html显示

//========================================================================
// 函数名: WordToHtml
/// <summary>
/// Word转成Html
/// </summary>
/// <param name="wordfilename">word文件名</param>
/*=======================================================================
变更记录
序号   更新日期  开发者   变更内容
0001   2008/07/22 张 新建
=======================================================================*/
public static string WordToHtml(object wordfilename)
{
//在此处放置用户代码以初始化页面
word.Application word = new word.Application();
Type wordtype = word.GetType();
word.Documents docs = word.Documents;
//打开文件
Type docstype = docs.GetType();
word.Document doc = (word.Document)docstype.InvokeMember("open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new object[] { wordfilename, true, true });
//转换格式,另存为
Type doctype = doc.GetType();
string wordsavefilename = wordfilename.ToString();
string strsavefilename = wordsavefilename.Substring(, wordsavefilename.Length - ) + "html";
object savefilename = (object)strsavefilename;
doctype.InvokeMember("saveas", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { savefilename, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
doctype.InvokeMember("close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
// 退出 word
wordtype.InvokeMember("quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
return savefilename.ToString();
}

2.将PPT转化为html显示

//========================================================================
// 函数名: PPTToHtml
/// <summary>
/// PPT转成Html
/// </summary>
/// <param name="pptFilename">PPT文件名</param>
/*=======================================================================
变更记录
序号   更新日期  开发者   变更内容
0001   2008/07/22 张 新建
=======================================================================*/
public string PPTToHtml(string pptFilename)
{
//被转换的html文档保存的位置
string saveFileName = pptFilename + ".html";
Microsoft.Office.Interop.PowerPoint.Application ppt = new Microsoft.Office.Interop.PowerPoint.Application();
Microsoft.Office.Core.MsoTriState m1 = new MsoTriState();
Microsoft.Office.Core.MsoTriState m2 = new MsoTriState();
Microsoft.Office.Core.MsoTriState m3 = new MsoTriState();
Microsoft.Office.Interop.PowerPoint.Presentation pp = ppt.Presentations.Open(pptFilename, m1, m2, m3);
pp.SaveAs(saveFileName, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsHTML, Microsoft.Office.Core.MsoTriState.msoTriStateMixed);
pp.Close();
//返回文件名
return saveFileName;
}

3.将Excel转化为html显示

//========================================================================
// 函数名: ExcelToHtml
/// <summary>
/// Excel转成Html
/// </summary>
/// <param name="excelFileName">Excel文件名</param>
/*=======================================================================
变更记录
序号   更新日期  开发者   变更内容
0001   2008/07/22 张 新建
=======================================================================*/
public string ExcelToHtml(string excelFileName)
{
//实例化Excel
Microsoft.Office.Interop.Excel.Application repExcel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workbook = null;
Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
//打开文件,n.FullPath是文件路径
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);
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[];
string filesavefilename = excelFileName.ToString();
string strsavefilename = filesavefilename.Substring(, filesavefilename.Length - ) + "html";
object savefilename = (object)strsavefilename;
object ofmt = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;
//进行另存为操作
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);
object osave = false;
//逐步关闭所有使用的对象
workbook.Close(osave, Type.Missing, Type.Missing);
repExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
worksheet = null;
//垃圾回收
GC.Collect();
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
workbook = null;
GC.Collect();
System.Runtime.InteropServices.Marshal.ReleaseComObject(repExcel.Application.Workbooks);
GC.Collect();
System.Runtime.InteropServices.Marshal.ReleaseComObject(repExcel);
repExcel = null;
GC.Collect();
//依据时间杀灭进程
System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("EXCEL");
foreach (System.Diagnostics.Process p in process)
{
if (DateTime.Now.Second - p.StartTime.Second > && DateTime.Now.Second - p.StartTime.Second < )
{
p.Kill();
}
} return savefilename.ToString();
}

以上是转换成为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. HDU2520 我是菜鸟,我怕谁

    http://acm.hdu.edu.cn/showproblem.php?pid=2520 我是菜鸟,我怕谁 Time Limit: 2000/1000 MS (Java/Others) Memor ...

  2. sql server Case when 的用法

    sql Case 仅仅返回第一个符合条件的值,剩下的Case部分将会被自动忽略. Case 的使用有两种格式:简单Case函数和Case搜索函数. 简单Case 函数: Case sex when ' ...

  3. chm 字体修改

    今天打开从网络下载的“[MSDN]Csharp编程指南+参考手册.chm”文件,以为看看里面所提供的一些知识点,但是发现文件显示的字体觉得有点别扭,以为能够像网页那样ctrl键+鼠标滚轮就能进行字体的 ...

  4. [codevs]失恋28天题目系列

    失恋28天-追女孩篇 题目描述 Description 呵呵,相信大家失恋33天都看过吧,里面的主人公黄小仙和王小贱都有印象吧!这回我要给大家讲的是我 们班同学的失恋经历,呵呵他总共失恋了28天.但是 ...

  5. nyoj重建二叉树(不真的建立)

      感觉c++很陌生啊 题目很简单,给你一棵二叉树的后序和中序序列,求出它的前序序列(So easy!). 输入 输入有多组数据(少于100组),以文件结尾结束.每组数据仅一行,包括两个字符串,中间用 ...

  6. linux 安装java环境(jdk)

    第一步:下载jdk-7-linux-i586.tar.gz wget -c http://download.oracle.com/otn-pub/java/jdk/7/jdk-7-linux-i586 ...

  7. Extjs中grid行的上移和下移

    一.将up和down按钮放到tbar中,然后选中grid行即可实现上移和下移 var up = new Ext.Action({ text : 'Up', icon : 'up.png',//或者添加 ...

  8. strip, 关于去除目标文件种的不必要信息

    对于so动态库及可执行文件,可以直接调用不带参数的strip (-s, 即--strip-all)去除大多数不必要的信息.因为so库非常标准,所以strip之后仍然可以进行完美的动态连接:而可执行文件 ...

  9. 64位linux安装android sdk的问题

    截至到今天,似乎在64位机器下安装android sdk存在不能运行的问题,可以用以下方法解决: Android SDK requires: Fedora 17 64bit with 32bit An ...

  10. 函数(C++ Primer读书笔记)

    C++ Primer 第五版课后题 练习6.32 :下面的函数合法吗?如果合法,说明其功能:如果不合法,修改其中的错误并解释原因. #include <iostream> using na ...