天天看点

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

首先应用中引入Microsoft.Office.Interop.Excel、Microsoft.Office.Interop.Word两个dll,将嵌入式互操作类型设为False,

WORD转换成PDF代码如下:

#region 将word文档转换成PDF格式

        /// <summary>

        /// 将word文档转换成PDF格式

        /// </summary>

        /// <param name="sourcePath">源文件路径</param>

        /// <param name="targetPath">目标文件路径</param>

        /// <returns></returns>

        private bool WordConvertPDF(string sourcePath, string targetPath)

        {

            bool result;

            Word.WdExportFormat exportFormat = Word.WdExportFormat.wdExportFormatPDF;   //PDF格式

            object paramMissing = Type.Missing;

            Word.ApplicationClass wordApplication = new Word.ApplicationClass();

            Word.Document wordDocument = null;

            try

            {

                object paramSourceDocPath = sourcePath;

                string paramExportFilePath = targetPath;

                Word.WdExportFormat paramExportFormat = exportFormat;

                bool paramOpenAfterExport = false;

                Word.WdExportOptimizeFor paramExportOptimizeFor =

                        Word.WdExportOptimizeFor.wdExportOptimizeForPrint;

                Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;

                int paramStartPage = 0;

                int paramEndPage = 0;

                Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;

                bool paramIncludeDocProps = true;

                bool paramKeepIRM = true;

                Word.WdExportCreateBookmarks paramCreateBookmarks =

                        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;

                }

                else

                    result = false;

            }

            catch (Exception 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;

        }

        #endregion

EXCEL转换成PDF代码如下:

#region 将excel文档转换成PDF格式

        /// <summary>

        /// 将excel文档转换成PDF格式

        /// </summary>

        /// <param name="sourcePath">源文件路径</param>

        /// <param name="targetPath">目标文件路径</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”一样