美文网首页
.NET 在线预览文档非常全面的案例

.NET 在线预览文档非常全面的案例

作者: 曾小芳zxf | 来源:发表于2020-01-11 17:07 被阅读0次

前段时间项目有一个在线预览office文档的需求,做了一个小栗子,和大家一起学习学习!如何实现这个功能呢?我总结了几个方案:

方案一:将需要转换的文档通过office组件转换成html查看

方案二:将需要转换的文档通过office组件转换成pdf查看

方案三:通过在服务器部署微软的office web apps进行文档预览    参考网页: https://www.cnblogs.com/yanweidie/p/4516164.html

方案四:office 在线预览服务

方案五:使用网上很多收费或者不收费的第三方插件

我使用的是方案一,也试过方案二和方案三,相比而言,转换成html比转换成pdf的效率要快很多,只是页面没有pdf美观,而在服务器部署office web apps配置失败了,所以选择放弃。

以下是方案一的案例:

1.首先引入com组件中office库,然后在程序集扩展中引入word的dll,我本来是想使用.net  core做这个在线预览offce文档,结果发现.net core 并没有office库,可能是我没有找到

 值得注意的是,虽然项目里引入office库,但运行项目后,发现会遇到各种问题,原因是office库并没有你电脑的office应用匹配,所以需要在电脑需要部署office插件

1.运行栏中输入命令:dcomcnfg,打开组件服务管理窗口,组件服务->我的电脑->DCOM配置

2.找到Microsoft Excel Application 设置属性标识:交互式用户,在安全了的三个配置全选自定义配置,添加Everyone 组,给这个组的权限都勾选未允许,

3.再找到 Microsoft PowerPoint 幻灯片  权限设置和第二步一样

4.再找到Microsoft Word 97 - 2003 文档 权限设置和第二步一样  (可能office版本不同)

5.在项目的webconfig  里   <system.web> 下 添加   <identity impersonate="true" userName="Administrator" password="123456"/>  userName、password为服务器的对应账号密码

查了很多资料,亲测可用  参考网页:https://www.cnblogs.com/5426z/articles/4865312.html

2.预览Excel

/// 预览Excel

        /// </summary>

        /// <param name="physicalPath">解码的地址</param>

        /// <param name="url"></param>

        public string PreviewExcel(string physicalPath, string url)

        {

            var resultUrl = string.Empty;

            var serverUrl = string.Empty;

            try

            {

                Microsoft.Office.Interop.Excel.Application application = null;

                //参数

                Workbook workbook = null;

                application = new Microsoft.Office.Interop.Excel.Application();

                object missing = Type.Missing;

                object trueObject = true;

                application.Visible = false;

                application.DisplayAlerts = false;

                workbook = application.Workbooks.Open(physicalPath, missing, trueObject, missing, missing, missing,

                  missing, missing, missing, missing, missing, missing, missing, missing, missing);

                object format = XlFileFormat.xlHtml;

                string htmlName = Path.GetFileNameWithoutExtension(physicalPath) + ".html";

                String outputFile = Path.GetDirectoryName(physicalPath) + "\\" + htmlName;

                workbook.SaveAs(outputFile, XlFileFormat.xlHtml, null, null, false, false,

                  XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);

                workbook.Close();

                application.Quit();

                resultUrl = Path.GetDirectoryName(url) + "\\" + htmlName;

                var lastUrl = Path.GetFileName(resultUrl);

                resultUrl = ConfigurationManager.AppSettings["ResultUrl"] + lastUrl;

                serverUrl = ConfigurationManager.AppSettings["ServerUrl"];

            }

            catch (Exception ex)

            {

                this.Write(ex);

            }

            return serverUrl + resultUrl;

}

4.预览word

> /// 预览Word(HtmL类型)

>        /// </summary>

>        /// <param name="physicalPath"></param>

>        /// <param name="url"></param>

>        /// <returns></returns>

>        public string PreviewWord(string physicalPath, string url)

>        {

>

>            var resultUrl = string.Empty;

>            var serverUrl = string.Empty;

>            object outputFile1 = new object();

>            object missing1 = new object();

>            //检查doc 是否被实例化

>            var idd = 0;

>            var ss = 1;

>            try

>            {

>                Microsoft.Office.Interop.Word._Application application = null;

>                Microsoft.Office.Interop.Word._Document doc = null;

>                application = new Microsoft.Office.Interop.Word.Application();

>                //参数

>                object missing = Missing.Value;

>                object trueObject = true;

>                application.Visible = false;

>                application.DisplayAlerts = WdAlertLevel.wdAlertsNone;

>                object physicalPathObj = physicalPath;

>                //实例化

>                doc = application.Documents.Open(ref physicalPathObj, ref missing, ref trueObject, ref missing, ref missing, ref missing,

>                  ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref

> missing);

>                string htmlName = Path.GetFileNameWithoutExtension(physicalPath) + ".html";

>                object outputFile = Path.GetDirectoryName(physicalPath) + "\\" + htmlName;

>                object format = WdSaveFormat.wdFormatHTML;

>                outputFile1 = outputFile;

>                missing1 = missing;

>                idd = doc.DocID;

>                ss = doc.CompatibilityMode;

>                //转换并保存

>                doc.SaveAs(ref outputFile, ref format, ref missing, ref missing, ref missing,

>                                  ref missing, XlSaveAsAccessMode.xlNoChange, ref missing,

>                                  ref missing, ref missing, ref missing, ref missing);

>                doc.Close();

>                application.Quit();

>                //返回137的路径

>

>                resultUrl = Path.GetDirectoryName(url) + "\\" + htmlName;

>                var lastUrl = Path.GetFileName(resultUrl);

>                resultUrl = ConfigurationManager.AppSettings["ResultUrl"] + lastUrl;

>                serverUrl = ConfigurationManager.AppSettings["ServerUrl"];

>            }

>            catch (Exception ex)

>            {

>                this.Write(ex, $"{outputFile1}+{missing1}+{idd.ToString()}+{ss.ToString()}+{physicalPath}");

>

>            }

>            return serverUrl + resultUrl; }

其余的信息请转到 https://blog.csdn.net/qq_42638454/article/details/103577309

相关文章

  • .NET 在线预览文档非常全面的案例

    前段时间项目有一个在线预览office文档的需求,做了一个小栗子,和大家一起学习学习!如何实现这个功能呢?我总结了...

  • 产品三维模型在线预览

    产品在线展示案例预览 玉镯在线预览:http://www.yanhuangxueyuan.com/3D/yuzhu...

  • 在线文档预览

    最近做一个项目,需用到在线文档预览功能,查了下资料,微软的Office Web Apps支持doc,ppt,xls...

  • 对接WPS文档的在线预览编辑服务

    一、简介 金山文档在线预览编辑服务,是以 H5 网页的形式提供,支持全平台接入。在线服务完整的使用,需要“对接方前...

  • PDF、Office 等文档预览

    1.Office 文档预览 对于 Office 类型的文档,可以用 Office 官方提供的在线预览 Viewer...

  • Java操作文库使用swfTools案例

    1 模仿在线预览 1.1 背景需求 Java+FlexPaper+swfTools仿百度文库文档在线预览系统设计与...

  • Office文档在线预览编辑解决方案

    微软的在线预览 利用iframe和微软的Office文档在线预览功能,可以在页面上动态加载doc、excel、pp...

  • 在线预览文档方法

    主要实现方法自己看我发的链接 除了第一个要注册试用 其他全部可以免费使用 第一个超链接http://www....

  • Office文档在线预览

    1.整体思路 Office文档在线预览的功能,经过一番研究,采用的解决思路是,将文档转换成PDF格式,在浏览器中预...

  • WPS文档在线预览接入的一点心得

    花了大半天时间接入WPS文档在线预览功能,还算比较顺利。 原来的OA系统一直用到文档在线预览功能,之前是用微软+o...

网友评论

      本文标题:.NET 在线预览文档非常全面的案例

      本文链接:https://www.haomeiwen.com/subject/xwxnmctx.html