关于.net调用com组件将word或exce转为pdf或html进行预览的问题整理

摘要:
=null){wordDocument.Close(refparamMissing,refparamMissing,refparamMissing);wordDocument=null;}if(wordApplication!

1.先贴代码(dll地址自己百度下载,名字叫做Microsoft.Office.Interop.Word.dll)

 /      // <summary> 
        /// 将word文档转换成PDF格式 
        /// </summary> 
        /// <param name="sourcePath"></param> 
        /// <param name="targetPath"></param> 
        /// <returns></returns> 
        public bool ConvertWord2Pdf(string sourcePath, string targetPath)
        {
            bool result;
            LogWriter.WriteErrorLog("0");
            Microsoft.Office.Interop.Word.WdExportFormat exportFormat = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
            LogWriter.WriteErrorLog("1");
            object paramMissing = Type.Missing;
            LogWriter.WriteErrorLog("2");
            Microsoft.Office.Interop.Word.Application wordApplication = new Microsoft.Office.Interop.Word.Application();
            LogWriter.WriteErrorLog("3");
            Microsoft.Office.Interop.Word.Document wordDocument = null;
            LogWriter.WriteErrorLog("4");
            try
            {
                object paramSourceDocPath = sourcePath;
                string paramExportFilePath = targetPath;
                LogWriter.WriteErrorLog("5");
                Microsoft.Office.Interop.Word.WdExportFormat paramExportFormat = exportFormat;
                LogWriter.WriteErrorLog("6");
                Microsoft.Office.Interop.Word.WdExportOptimizeFor paramExportOptimizeFor =
                        Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
                LogWriter.WriteErrorLog("7");
                Microsoft.Office.Interop.Word.WdExportRange paramExportRange = Microsoft.Office.Interop.Word.WdExportRange.wdExportAllDocument;
                LogWriter.WriteErrorLog("8");
                int paramStartPage = 0;
                int paramEndPage = 0;
                LogWriter.WriteErrorLog("9");
                Microsoft.Office.Interop.Word.WdExportItem paramExportItem = Microsoft.Office.Interop.Word.WdExportItem.wdExportDocumentContent;
                LogWriter.WriteErrorLog("10");
                Microsoft.Office.Interop.Word.WdExportCreateBookmarks paramCreateBookmarks =
                        Microsoft.Office.Interop.Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
                LogWriter.WriteErrorLog("11");
                wordDocument = wordApplication.Documents.Open(
                        ref paramSourceDocPath, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing);
                LogWriter.WriteErrorLog("12");
                if (wordDocument != null)
                {
                    LogWriter.WriteErrorLog("13");
                    wordDocument.ExportAsFixedFormat(paramExportFilePath,
                            paramExportFormat, false,
                            paramExportOptimizeFor, paramExportRange, paramStartPage,
                            paramEndPage, paramExportItem, true,
                            true, paramCreateBookmarks, true,
                            true, false,
                            ref paramMissing);
                    LogWriter.WriteErrorLog("14");
                }
                result = true;
                LogWriter.WriteErrorLog("15");
            }
            catch (Exception ex)
            {
                LogWriter.WriteErrorLog(ex);
                result = false;
            }
            finally
            {
                if (wordDocument != null)
                {
                    wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
                    wordDocument = null;
                }
                if (wordApplication != null)
                {
                    wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
                    wordApplication = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return result;
        }

        /// <summary> 
        /// word转成html 
        /// </summary> 
        /// <param name="wordFileName"></param> 
        public string WordToHtml(object wordFileName)
        {
            try
            {

                LogWriter.WriteErrorLog("0");
                //在此处放置用户代码以初始化页面 
                Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
                LogWriter.WriteErrorLog("1");
                Type wordType = word.GetType();
                LogWriter.WriteErrorLog("2");
                Microsoft.Office.Interop.Word.Documents docs = word.Documents;
                LogWriter.WriteErrorLog("3");
                //打开文件 
                Type docsType = docs.GetType();
                LogWriter.WriteErrorLog("4");
                Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { wordFileName, true, true });
                LogWriter.WriteErrorLog("5");
                LogWriter.WriteErrorLog(doc.ToString());
                //转换格式,另存为 
                Type docType = doc.GetType();
                LogWriter.WriteErrorLog("6");
                string wordSaveFileName = wordFileName.ToString();
                LogWriter.WriteErrorLog("7");
               
                string strSaveFileName = wordSaveFileName.Substring(0, wordSaveFileName.LastIndexOf(".")) + ".html"; //wordSaveFileName.Substring(0, wordSaveFileName.Length - 3) + "html";
                LogWriter.WriteErrorLog("8");
                object saveFileName = (object)strSaveFileName;
                LogWriter.WriteErrorLog("9");
                docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
                LogWriter.WriteErrorLog("10");
                docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
                LogWriter.WriteErrorLog("11");
                //退出 Word 
                wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
                LogWriter.WriteErrorLog("12");
                return saveFileName.ToString();
            }
            catch (Exception ex)
            {
                LogWriter.WriteErrorLog(ex.ToString());
                return "";
            }
        }

2.上面的代码主要的功能分别是将word转换为pdf和将word转换为html,以上代码在本地执行完全没问题,可以正常生成html或者word,但是在服务器上就会出现问题,问题如下

3.问题1,通过以上的log日志我们可以发现,以上代码都执行到了0,后面的代码没有执行,同时报了以下的错误,也就是说在都是在调用com组件上出现了问题

错误日志

System.UnauthorizedAccessException: Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} 
failed due to the following error: 80070005 拒绝访问。 (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).

4.以上的错误可以通过以下几步来解决

1).运行dcomcnfg -》组件服务-》计算机-》我的电脑-》DCom配置-》找到Microsoft Word文档

如果是win7服务器或者window server服务器 需要运行comexp.msc -32 否则在DCom配置找不到Microsoft Word文档

office下载地址:http://www.xitongtiandi.net/soft_yy/2076.html

2).单击属性打开此应用程序的属性对话框。

3).单击标识选项卡,然后选择交互式用户。

4).单击"安全"选项卡,分别在"启动和激活权限"和"访问权限"组中选中"自定义",然后自定义->编辑->账户

关于步骤 4) 网上说需要添加自定义->编辑->添加ASP.NET账户和IUSER_计算机名,然而我并没有找到这两个账户,所以我就添加了个everyone权限,这个权限可能不安全,因为我是自己的测试服务器,所 以这样设置没有问题,如果觉得不安全,可以自己试试添加别的账户试试

5).打开IIS->应用程序池->高级设置->将标识由原来的ApplicationPoolIdentity改为LocalSystem(有人说改为LocalServer也可以,我我自己试了下是不行的,不知道别人可不可以),这一步非常重要,网上好 多解决方案都没有这一步

5.设置好后再次运行,会发现,日志显示word转pdf并没有执行13和14,word转html只执行到了5,没有执行6往后面的代码,同时爆出一个错误为将对象引用到实例。通过这个错误可以判断 出Microsoft.Office.Interop.Word.Document是null,这又该怎么解决呢,网上查了一大堆,一个有用的没有,直到偶然间发现一篇博客,以下就是解决方案

6.找到目录C:WindowsSysWOW64configsystemprofile 然后在里面新建一个文件夹Desktop ,注意大小写,就是这么简单,惊不惊喜,意不意外,同理excel转pdf或者html就需要重新百度下别的代码了,并且DCom配置改成excel的就行了,这里不做说明

免责声明:文章转载自《关于.net调用com组件将word或exce转为pdf或html进行预览的问题整理》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇git设置Eclipse中忽略的文件手动部署kubernetes(k8s)和利用ansible自动部署k8s下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

HTML之Data URL(转)

Data URL给了我们一种很巧妙的将图片“嵌入”到HTML中的方法。跟传统的用img标记将服务器上的图片引用到页面中的方式不一样,在Data URL协议中,图片被转换成base64编码的字符串形式,并存储在URL中,冠以mime-type。本文中,我将介绍如何巧妙的使用Data URL优化网站加载速度和执行效率。 1. Data URL基本原理 Data...

完整版QQ(腾讯)开放平台操作指南(包含:qq登录能力获取等等)

之前我和大家提过,我要购买第三方的APP服务,就相当于有自己的APP了,现在APP对接上线之前需要做大量的准备工作,在此把步骤分享给大家,这样可以节省大家很多时间。 完整版QQ(腾讯)开放平台操作指南(包含:账号注册,移动应用申请,qq登录能力获取,上传更新安装包) 前期资料填写阶段需完成 一、进入应用宝开发者平台(地址:http://open.qq.co...

SAP NetWeaver Business Client (NWBC) 简介

SAP NetWeaver Business Client (NWBC) 简介 学习版本NWBC v3.0 1.NWBC 简介 SAP NetWeaver Business Client (NWBC) 是新一代SAP用户界面,集成了SAPGUI事务和新的web dynpro应用,类似于桌面应用程序。 SAP NetWeaver商业客户(NWBC)是...

ASP.NET MVC5(五):身份验证、授权

使用Authorize特性进行身份验证 通常情况下,应用程序都是要求用户登录系统之后才能访问某些特定的部分。在ASP.NET MVC中,可以通过使用Authorize特性来实现,甚至可以对整个应用程序全局使用Authorize特性。 Authorize的用法 本节以一个添加产品的示例来说明Authorize的使用方法。首先,创建Product类、添加属性...

五十六 SMTP发送邮件

SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件、HTML邮件以及带附件的邮件。 Python对SMTP支持有smtplib和email两个模块,email负责构造邮件,smtplib负责发送邮件。 首先,我们来构造一个最简单的纯文本邮件: from email.mime.text import MIMEText msg =...

Vue安装与简单使用

Vue入门 使用Typora打开https://pan.baidu.com/s/1Mf3ZFSthdVUQevqWr777eA 提取码: hg9b vue中文官网教学 安装与使用,我也经常看这个 点击进入 认识Vue Vue (读音 /vjuː/,类似于 **view**) 是一套用于构建用户界面的**渐进式框架** 安装Node.js...