如何实现Asp.Net在线预览Word文档的最佳解决方案和详细思路?
- 内容介绍
- 文章标签
- 相关推荐
本文共计698个文字,预计阅读时间需要3分钟。
目录+项目特点+解决方案+深入思路:
1.将Word文档转换为Html,再预览Html。
2.近几天有个老项目找到我,有很多吗?比我工作年限都长,看到这个项目我还得叫声前啊。
目录
- 项目特点
- 解决方案
- 大致思路:先将Word文档转换Html,再预览Html。
- 1、Word文档转Html
- 2、预览
前几天有个老项目找到我,有多老呢?比我工作年限都长,见到这个项目我还得叫一声前辈。
这个项目目前使用非常稳定,十多年了没怎么更新过,现在客户想加一个小功能:在线预览Word文档。
首先想到的是用第三方的服务,例如WPS的开放平台。
刚看完文档,客户来了句,要一次性的哦,后续再付费的通通不要。
得嘞,换其他方案吧。
项目特点
Asp.Net不带Core,.NET Framework 4.0,部署在Windows平台上。
解决方案
大致思路:先将Word文档转换Html,再预览Html。
1、Word文档转Html
先引用Office的DLL,在COM里面,注意:电脑需要安装Office哦。
又注意:请在DLL属性里面将嵌入互操作类型改为False
转换过程一个方法搞定:
using Microsoft.Office.Interop.Word; public static string WordToHtml(string path) { string root = AppDomain.CurrentDomain.BaseDirectory; var htmlName = $"{Guid.NewGuid().ToString("N")}.html"; var htmlPath = root + $"Resource/Temporary/"; if (!Directory.Exists(htmlPath)) { Directory.CreateDirectory(htmlPath); } ApplicationClass word = new ApplicationClass(); Type wordType = word.GetType(); Documents docs = word.Documents; Type docsType = docs.GetType(); Document doc = (Document)docsType.InvokeMember("Open", BindingFlags.InvokeMethod, null, docs, new Object[] { (object)path, true, true }); Type docType = doc.GetType(); docType.InvokeMember("SaveAs", BindingFlags.InvokeMethod, null, doc, new object[] { (htmlPath + htmlName), WdSaveFormat.wdFormatFilteredHTML }); docType.InvokeMember("Close", BindingFlags.InvokeMethod, null, doc, null); wordType.InvokeMember("Quit", BindingFlags.InvokeMethod, null, word, null); return htmlName; }
2、预览
上一步Word转Html的方法已经准备就绪,我们再准备这样一个Word文档。
简单写一下逻辑:
是不是特别简单,我们再看看成品效果。
这种方案局限性比较大,部署平台必须安装Office,刚好客户能满足。
到此这篇关于Asp.Net在线预览Word文档的解决方案与思路的文章就介绍到这了,更多相关Asp.Net在线预览Word文档内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!
本文共计698个文字,预计阅读时间需要3分钟。
目录+项目特点+解决方案+深入思路:
1.将Word文档转换为Html,再预览Html。
2.近几天有个老项目找到我,有很多吗?比我工作年限都长,看到这个项目我还得叫声前啊。
目录
- 项目特点
- 解决方案
- 大致思路:先将Word文档转换Html,再预览Html。
- 1、Word文档转Html
- 2、预览
前几天有个老项目找到我,有多老呢?比我工作年限都长,见到这个项目我还得叫一声前辈。
这个项目目前使用非常稳定,十多年了没怎么更新过,现在客户想加一个小功能:在线预览Word文档。
首先想到的是用第三方的服务,例如WPS的开放平台。
刚看完文档,客户来了句,要一次性的哦,后续再付费的通通不要。
得嘞,换其他方案吧。
项目特点
Asp.Net不带Core,.NET Framework 4.0,部署在Windows平台上。
解决方案
大致思路:先将Word文档转换Html,再预览Html。
1、Word文档转Html
先引用Office的DLL,在COM里面,注意:电脑需要安装Office哦。
又注意:请在DLL属性里面将嵌入互操作类型改为False
转换过程一个方法搞定:
using Microsoft.Office.Interop.Word; public static string WordToHtml(string path) { string root = AppDomain.CurrentDomain.BaseDirectory; var htmlName = $"{Guid.NewGuid().ToString("N")}.html"; var htmlPath = root + $"Resource/Temporary/"; if (!Directory.Exists(htmlPath)) { Directory.CreateDirectory(htmlPath); } ApplicationClass word = new ApplicationClass(); Type wordType = word.GetType(); Documents docs = word.Documents; Type docsType = docs.GetType(); Document doc = (Document)docsType.InvokeMember("Open", BindingFlags.InvokeMethod, null, docs, new Object[] { (object)path, true, true }); Type docType = doc.GetType(); docType.InvokeMember("SaveAs", BindingFlags.InvokeMethod, null, doc, new object[] { (htmlPath + htmlName), WdSaveFormat.wdFormatFilteredHTML }); docType.InvokeMember("Close", BindingFlags.InvokeMethod, null, doc, null); wordType.InvokeMember("Quit", BindingFlags.InvokeMethod, null, word, null); return htmlName; }
2、预览
上一步Word转Html的方法已经准备就绪,我们再准备这样一个Word文档。
简单写一下逻辑:
是不是特别简单,我们再看看成品效果。
这种方案局限性比较大,部署平台必须安装Office,刚好客户能满足。
到此这篇关于Asp.Net在线预览Word文档的解决方案与思路的文章就介绍到这了,更多相关Asp.Net在线预览Word文档内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

