• C#实现office文档转换为PDF格式


     1.安装组件OfficeSaveAsPDFandXPS

    需要安装office 2007 还有一个office2007的插件OfficeSaveAsPDFandXPS

    下载地址
      office 2007     链接:http://pan.baidu.com/s/1eSeQD38 密码:j5qy
     
    这是一个微软官方出的office插件。
     
     
    2.安装好之后,打开VS,以VS2013为例
    2.1添加以下组件的引用
    “项目”--“引用”--“添加引用..”--在程序集中搜索"Microsoft",找到如下3个dll
    Microsoft.Office.Interop.Word
    Microsoft.Office.Interop.PowerPoint
    microsoft.office.interop.Excel

    dll成功引入后,项目的引用里面则会出现相应的dll控件名称。

    修改上图中3个dll的属性"嵌入互操作类型"为False

    引入dll:Microsoft.Office.Core

    文件版本为15.0.5031.1000

    该dll的文件名称为OFFICE.DLL

     即可看到如下图的dll已引入成功

    2.2相应类文件顶部添加以下组件的引用

    using Word = Microsoft.Office.Interop.Word;
    using Excel = Microsoft.Office.Interop.Excel;
    using PowerPoint = Microsoft.Office.Interop.PowerPoint;
    using Microsoft.Office.Core;
    using System;
    using Microsoft.Office.Interop.Excel;
    using Microsoft.Office.Interop.PowerPoint;
    using Microsoft.Office.Interop.Word;

     
    我们可以使用一个枚举类型来决定生成文件的类型
    Word.WdExportFormat wd = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
    Excel.XlFixedFormatType excelType = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;
    PowerPoint.PpSaveAsFileType ppType = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
     
    2.3相应类文件中的转换代码
            #region office文件转换为pdf文件
             
            /// <summary>
            /// 将word文档转换成PDF格式
            /// </summary>
            /// <param name="sourcePath">源文件路径</param>
            /// <param name="targetPath">目标文件路径</param> 
            /// <param name="exportFormat"></param>
            /// <returns></returns>
            private bool WordConvertPDF(string sourcePath, string targetPath, Word.WdExportFormat exportFormat)
            {
                bool result;
                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;
                }
                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> 
            /// <param name="targetType"></param>
            /// <returns></returns>
            private bool ExcelConvertPDF(string sourcePath, string targetPath, XlFixedFormatType targetType)
            {
                bool result;
                object missing = Type.Missing;
                Excel.ApplicationClass application = null;
                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);
    
                    workBook.ExportAsFixedFormat(targetType, target, XlFixedFormatQuality.xlQualityStandard, 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>
            /// 将ppt文档转换成pdf格式
            /// </summary>
            /// <param name="sourcePath">源文件路径</param>
            /// <param name="targetPath">目标文件路径</param> 
            /// <param name="targetFileType"></param>
            /// <returns></returns>
            private bool PPTConvertPDF(string sourcePath, string targetPath, PpSaveAsFileType targetFileType)
            {
                bool result;
                object missing = Type.Missing;
                PowerPoint.ApplicationClass application = null;
                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;
            }
    
            #endregion
    View Code

    权限配置第一种方案:

    在组件在服务器中添加权限

    单击开始,单击运行, 然后键入 DCOMCNFG。选择要自动运行的应用程序。应用程序名称如下所示:
    Microsoft Access 97 - Microsoft Access 数据库
    Microsoft Access 2000/2002 - Microsoft Access 应用程序
    Microsoft Excel 97/2000/2002 - Microsoft Excel 应用程序
    Microsoft Word 97 - Microsoft Word Basic
    Microsoft Word 2000/2002 - Microsoft Word 文档
    单击属性打开此应用程序的属性对话框。
     

    1)标示—运行此应用程序的用户账户—下列用户;然后输入Administrator用户组中的一个用户。

    注:更改服务器中组件服务所用到的用户密码时,须在组件中重新输入新的密码,才能正常生成Word。

    2)安全—启动和激活权限,选择“自定义”,添加IIS_WPG用户的本地启动、本地激活权限;

    3)安全—访问权限,选择“自定义”,添加IIS_WPG用户的本地访问权限;

     

     权限配置第二种方案:

    对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的临时文件,以下操作解决问题:

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

     引自:http://www.cnblogs.com/louby/p/7053262.html

    另一功能:word文档或者Excel文档想要转换xps格式

    地址:https://blog.csdn.net/guochunyang/article/details/69549231

  • 相关阅读:
    实现Promise的first等各种变体
    js打乱数组的实战应用
    Vue单页面中进行业务数据的上报
    如何实现一个楼中楼的评论系统
    vue实现对表格数据的增删改查
    用CSS3实现无限循环的无缝滚动
    使用vue实现tab操作
    redis事务与关系型数据库事务比较
    优先队列原理与实现
    MySQL排序原理与案例分析
  • 原文地址:https://www.cnblogs.com/ggll611928/p/9019439.html
Copyright © 2020-2023  润新知