asp.net:HTML预览docx文档内容
2011-06-10 11:14
471 查看
自gmail推出office文档的预览功能以来,各大邮箱纷纷跟进,可是居然找不到成熟的文档,感到匪夷所思,商业机密?
这次碰到个小需求是要在网页里预览doc文档,
其中一种解决方式,就是在上传doc文件的时候,用代码操作word的“另存为”功能,存成html,
相关信息,可google"asp.net 预览 doc文档"关键词,查出来的都是同一篇。
这种方式的局限在
1,每一份文档会生成一个html副本和资源文件夹,空间浪费严重
2,服务器一定要安装office,当然你也可以尝试引入必要dll,然后根据错误提示进行复制的DCOM配置,这一点每次把我把折腾疯了,最后都是装office解决的。。。
3,目前未测试docx
大致实现如下,非常有诚意地提醒大家,最好还是安装office后再试
引入相关DLL后(比如word,或excel),参考下述代码:
=============
至此中文资源没有了,于是继续寻找,发现了一个老外的实现,但是只支持word2007,因为office2007是基于openXML的,当然可以自行解析
见此地址
http://blog.maartenballiauw.be/post/2008/01/11/Preview-Word-files-(docx)-in-HTML-using-ASPNET-OpenXML-and-LINQ-to-XML.aspx
那就折衷吧,至少这种解决方案可以实现.docx结尾的url直接预览,并且提供了生成下载链接的规则。
这次碰到个小需求是要在网页里预览doc文档,
其中一种解决方式,就是在上传doc文件的时候,用代码操作word的“另存为”功能,存成html,
相关信息,可google"asp.net 预览 doc文档"关键词,查出来的都是同一篇。
这种方式的局限在
1,每一份文档会生成一个html副本和资源文件夹,空间浪费严重
2,服务器一定要安装office,当然你也可以尝试引入必要dll,然后根据错误提示进行复制的DCOM配置,这一点每次把我把折腾疯了,最后都是装office解决的。。。
3,目前未测试docx
大致实现如下,非常有诚意地提醒大家,最好还是安装office后再试
引入相关DLL后(比如word,或excel),参考下述代码:
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; using System.Diagnostics; using Word = Microsoft.Office.Interop.Word; using Excel = Microsoft.Office.Interop.Excel; using System.Reflection; using Microsoft.Office.Interop.Excel; public partial class upload_preview : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { GenerationWordHTML("E:\\20110502.doc", "E:\\20110502.html"); GenerationExcelHTML("E:\\20110502.xls", "E:\\20110502.html"); } /// <summary> /// Ecxel文件生成HTML并保存 /// </summary> /// <param name="FilePath">需要生成的ecxel文件的路径</param> /// <param name="saveFilePath">生成以后保存HTML文件的路径</param> /// <returns>是否生成成功,成功为true,反之为false</returns> protected bool GenerationExcelHTML(string FilePath, string saveFilePath) { try { Excel.Application app = new Excel.Application(); app.Visible = false; Object o = Missing.Value; ///打开文件 /*下面是Microsoft Excel 9 Object Library的写法: */ /*_Workbook xls = app.Workbooks.Open(FilePath, o, o, o, o, o, o, o, o, o, o, o, o);*/ /*下面是Microsoft Excel 10 Object Library的写法: */ _Workbook xls = app.Workbooks.Open(FilePath, o, o, o, o, o, o, o, o, o, o, o, o, o, o); ///转换格式,另存为 HTML /*下面是Microsoft Excel 9 Object Library的写法: */ /*xls.SaveAs(saveFilePath, Excel.XlFileFormat.xlHtml, o, o, o, o, XlSaveAsAccessMode.xlExclusive, o, o, o, o);*/ /*下面是Microsoft Excel 10 Object Library的写法: */ xls.SaveAs(saveFilePath, Excel.XlFileFormat.xlHtml, o, o, o, o, XlSaveAsAccessMode.xlExclusive, o, o, o, o, o); ///退出 Excel app.Quit(); return true; } catch { return false; } finally { //最后关闭打开的excel 进程 Process[] myProcesses = Process.GetProcessesByName("EXCEL"); foreach (Process myProcess in myProcesses) { myProcess.Kill(); } } } /// <summary> /// WinWord文件生成HTML并保存 /// </summary> /// <param name="FilePath">需要生成的word文件的路径</param> /// <param name="saveFilePath">生成以后保存HTML文件的路径</param> /// <returns>是否生成成功,成功为true,反之为false</returns> private bool GenerationWordHTML(string FilePath, string saveFilePath) { try { Word.ApplicationClass word = new Word.ApplicationClass(); 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[] { FilePath, true, true }); /// 转换格式,另存为 HTML Type docType = doc.GetType(); /*下面是Microsoft Word 9 Object Library的写法: */ /*docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFilePath, Word.WdSaveFormat.wdFormatHTML });*/ /*下面是Microsoft Word 10 Object Library的写法: */ docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFilePath, Word.WdSaveFormat.wdFormatFilteredHTML }); /// 退出 Word wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null); return true; } catch { return false; } finally { //最后关闭打开的winword 进程 Process[] myProcesses = Process.GetProcessesByName("WINWORD"); foreach (Process myProcess in myProcesses) { myProcess.Kill(); } } } }
=============
至此中文资源没有了,于是继续寻找,发现了一个老外的实现,但是只支持word2007,因为office2007是基于openXML的,当然可以自行解析
见此地址
http://blog.maartenballiauw.be/post/2008/01/11/Preview-Word-files-(docx)-in-HTML-using-ASPNET-OpenXML-and-LINQ-to-XML.aspx
那就折衷吧,至少这种解决方案可以实现.docx结尾的url直接预览,并且提供了生成下载链接的规则。
相关文章推荐
- 【ASP.NET 进阶】仿百度文库文档在线预览(支持格式.pdf,.doc,docx,xls,xlsx,.ppt,pptx)
- ASP.NET 中如何对生成的 HTML 内容流进行控制?
- asp.net实现word文档在线预览功能代码
- 利用ASP.NET输出指定内容的WORD、EXCEL、TXT、HTM等类型的文档
- ASP.NET将Word文档转化为HTML格式的文档
- ASP.NET 中如何对生成的 HTML 内容流进行控制
- asp.net 获取HTML meta标记中的内容
- ASP.NET实现word文档在线预览功能代码
- ASP.NET实现word文档在线预览功能代码-JavaScript编程
- ASP.NET导出页面内容至word文档
- asp.net将内容导出到Excel,Table表格数据(html)导出EXCEL
- ASP.NET 中如何对生成的 HTML 内容流进行控制
- 利用Asp.net Ajax异步获取xml文档内容
- ASP.NET压缩输出的HTML内容
- Asp.net解析HTML并利用XPATH分析提取内容
- asp.net实现word文档在线预览功能的方法
- ASP.NET实现word文档在线预览功能代码
- 利用Asp.net Ajax异步获取xml文档内容
- ASP.NET 中如何对生成的 HTML 内容流进行控制?
- ASP.NET 中如何对生成的 HTML 内容流进行控制?