zoukankan      html  css  js  c++  java
  • 解决本地调用office组件成功,但是发布到IIS中出现的错误(检索COM类工厂中CLSID为{00024500-0000-0000-C000-000000000046}的组件时失败)

    在C#操作word或者Excel,我们可能会用到微软内置的COM组件,会出现很多问题。

    如:在本地调试导出Excel没有问题,发布到IIS就有问题了,检测到的异常:

    我们会发现在iis上运行的程序,没有打开word的进程。

    因为你vs是管理员权限,而iis没有权限。

    所以这要提高iis的权限。

    启动IIS,应用程序池-“选定的应用程序池”-高级设置-进程模拟-标识:
    选择自定义帐户然后单击设置以打开设置凭据对话框,输入管理员的用户名和密码。

    输入管理员的账号和密码

    在%windir%/System32/config/systemprofile下,建立Desktop文件夹。

    一切IIS对COM组件的操作 均需要设置标识为 隶属于administrator组的用户.

    -----------------------------------------------------------------------------------------------------------------

    记录:

       //Word转换成pdf
            /// <summary>
            /// 把Word文件转换成为PDF格式文件
            /// </summary>
            /// <param name="sourcePath">源文件路径</param>
            /// <param name="targetPath">目标文件路径</param>
            /// <returns>true=转换成功</returns>
    
            private bool DOCConvertToPDF(string sourcePath, string targetPath)
            {
                bool result = false;
                Microsoft.Office.Interop.Word.WdExportFormat exportFormat = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
                object paramMissing = Type.Missing;
                Microsoft.Office.Interop.Word.ApplicationClass wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
                Microsoft.Office.Interop.Word.Document wordDocument = null;
                try
                {
                    object paramSourceDocPath = sourcePath;
                    string paramExportFilePath = targetPath;
    
                    Microsoft.Office.Interop.Word.WdExportFormat paramExportFormat = exportFormat;
                    bool paramOpenAfterExport = false;
                    Microsoft.Office.Interop.Word.WdExportOptimizeFor paramExportOptimizeFor = Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
                    Microsoft.Office.Interop.Word.WdExportRange paramExportRange = Microsoft.Office.Interop.Word.WdExportRange.wdExportAllDocument;
                    int paramStartPage = 0;
                    int paramEndPage = 0;
                    Microsoft.Office.Interop.Word.WdExportItem paramExportItem = Microsoft.Office.Interop.Word.WdExportItem.wdExportDocumentContent;
                    bool paramIncludeDocProps = true;
                    bool paramKeepIRM = true;
                    Microsoft.Office.Interop.Word.WdExportCreateBookmarks paramCreateBookmarks = Microsoft.Office.Interop.Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
                    bool paramDocStructureTags = true;
                    bool paramBitmapMissingFonts = true;
                    bool paramUseISO19005_1 = false;
    
                    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);
                    if (wordDocument != null)
                        wordDocument.ExportAsFixedFormat(paramExportFilePath,
                        paramExportFormat, paramOpenAfterExport,
                        paramExportOptimizeFor, paramExportRange, paramStartPage,
                        paramEndPage, paramExportItem, paramIncludeDocProps,
                        paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
                        paramBitmapMissingFonts, paramUseISO19005_1,
                        ref paramMissing);
                    result = true;
                }
                catch
                {
                    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>
            /// 把Excel文件转换成PDF格式文件
            /// </summary>
            /// <param name="sourcePath">源文件路径</param>
            /// <param name="targetPath">目标文件路径</param>
            /// <returns>true=转换成功</returns>
            private bool XLSConvertToPDF(string sourcePath, string targetPath)
            {
                bool result = false;
                Microsoft.Office.Interop.Excel.XlFixedFormatType targetType = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;
                object missing = Type.Missing;
                Microsoft.Office.Interop.Excel.ApplicationClass application = null;
                Microsoft.Office.Interop.Excel.Workbook workBook = null;
                try
                {
                    application = new Microsoft.Office.Interop.Excel.ApplicationClass();
                    object target = targetPath;
                    object type = targetType;
                    workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
                            missing, missing, missing, missing, missing, missing, missing, missing, missing);
    
                    workBook.ExportAsFixedFormat(targetType, target, Microsoft.Office.Interop.Excel.XlFixedFormatQuality.xlQualityMinimum, true, false, missing, missing, missing, missing);
                    result = true;
                }
                catch
                {
                    result = false;
                }
                finally
                {
                    if (workBook != null)
                    {
                        workBook.Close(true, missing, missing);
                        workBook = null;
                    }
                    if (application != null)
                    {
                        application.Quit();
                        application = null;
                    }
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }
                return result;
            }
            /// <summary>
            /// 把PowerPoing文件转换成PDF格式文件
            /// </summary>
            /// <param name="sourcePath">源文件路径</param>
            /// <param name="targetPath">目标文件路径</param>
            /// <returns>true=转换成功</returns>
            //private bool PPTConvertToPDF(string sourcePath, string targetPath)
            // {
            //    bool result;
            //    PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
            //    object missing = Type.Missing;
            //    PowerPoint.ApplicationClass application = null;
            //    PowerPoint.Presentation persentation = null;
            //    try
            //    {
            //        application = new PowerPoint.ApplicationClass();
            //        persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
            //        persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);
    
            //        result = true;
            //    }
            //    catch
            //    {
            //        result = false;
            //    }
            //    finally
            //    {
            //        if (persentation != null)
            //        {
            //            persentation.Close();
            //            persentation = null;
            //        }
            //        if (application != null)
            //        {
            //            application.Quit();
            //            application = null;
            //        }
            //        GC.Collect();
            //        GC.WaitForPendingFinalizers();
            //        GC.Collect();
            //        GC.WaitForPendingFinalizers();
            //    }
            //    return result;
            //  }
  • 相关阅读:
    webpack高级概念code splitting 和 splitChunks (系列五)
    webpack高级概念Develoment 和 Production 不同环境的配置 (系列四)
    webpack高级概念Tree Shaking (树摇)(系列三)
    HarmonyOS三方件开发指南(16)-VideoCache 视频缓存
    鸿蒙开源第三方组件——uCrop_ohos图片裁剪组件
    Hi3516如何连接Wifi(三)
    【鸿蒙学院】鸿蒙IDE迎来重大更新,新特性足以让你尖叫
    《鸿蒙系统物联网模组——Neptune 三天全攻略》课件、代码
    预览器和编辑器双重发力,DevEco Studio 2.1 Beta 3强势来袭
    强大的鸿蒙开发环境 —— DevEco Studio 2.1 Beta3发布
  • 原文地址:https://www.cnblogs.com/jason-davis/p/com.html
Copyright © 2011-2022 走看看