C# 使用Microsoft.Office.Interop将Excel、Word转换成PDF遇到的问题总结 (2)

#region 将excel文档转换成PDF格式
        /// <summary>
        /// 将excel文档转换成PDF格式
        /// </summary>
        /// <param>源文件路径</param>
        /// <param>目标文件路径</param>
        /// <returns></returns>
        private bool ExcelConvertPDF(string sourcePath, string targetPath)
        {
            bool result;
            Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF; //PDF格式
            object missing = Type.Missing;
            Excel.ApplicationClass application = null;
            Excel.Workbook workBook = null;
            try
            {
                application = new 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);
                if (workBook != null)
                {
                    workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
                    result = true;
                }
                else
                    result = false;
            }
            catch (Exception ex)
            {
                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;
        }
        #endregion

对DCOM组件进行权限配置:

1、打开comexp.msc -32

2、Microsoft Excel Application、和Microsoft Word 97-2003 Document属性里面进行配置,如下:

  标识:设为“交互式用户”

  安全:启动和激活权限添加“NETWORK SERVICE”,勾选本地启动和本地激活,访问权限添加类似

以上两点设置完成后还有问题,继续以下操作:

3、应用进程池标识转换为“LocalSystem”

4、在C:/Windows/System32/config/systemprofile和C:/Windows/SysWOW64/config/systemprofile目录下创建名为Desktop目录

出现的问题:发布到服务器上后,WORD转换没问题,EXCEL转换PDF时转换卡住,只能生成temp的临时文件,以下操作解决问题:

4、在DCOM组件的Microsoft Excel Application、和Microsoft Word 97-2003 Document属性安全中额外添加“IIS_IUSRS”用户组,权限跟之前的“NETWORL SERVICE”一样

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzggjg.html