zoukankan      html  css  js  c++  java
  • C#实现office文档转换为PDF或xps

    需要安装office 2007 还有一个office2007的插件OfficeSaveAsPDFandXPS
    下载地址
    这是一个微软官方出的office插件。
    如果无法下载,可以下载我的:https://files.cnblogs.com/kaixing/SaveAsPDFandXPS.zip
     

    安装wps后,也支持wps格式

     
    首先添加以下com组件的引用
    Microsoft Word 12.0 Object Library
    Microsoft PowerPoint 12.0 Object Library
    Microsoft Excel 12.0 Object Library
     
    ------------------------------------------------------
    程序中:
    using Word = Microsoft.Office.Interop.Word;
    using Excel = Microsoft.Office.Interop.Excel;
    using PowerPoint = Microsoft.Office.Interop.PowerPoint;
    using Microsoft.Office.Core;
     
    1.word转换方法
    private bool Convert(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)
             //定义的很多属性,大家可以去查下手册ExportAsFixedFormat;
                        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;
            }
    解决方法:
        
        修改你添加的引用的属性,将类型改为false
    大家可以使用我前面的上传的代码,简单的试验
    使用方法: Convert(sourcePath, targetPath, Word.WdExportFormat.wdExportFormatPDF);
    sourcePath为你上次文件路径..如:D:\project\UpLoadFile\UpLoadFile\files\123.docx
    targetPath为你装换文件路径..如:D:\project\UpLoadFile\UpLoadFile\files\123.pdf
    Word.WdExportFormat.wdExportFormatPDF为转换的格式
     
    //Excel转换方法
            private bool ExcelConvert(string sourcePath, string targetPath, Excel.XlFixedFormatType targetType)
            {
                bool result;
                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);

                    workBook.ExportAsFixedFormat(targetType, target, Excel.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;
            }

            //PowerPoint转换方法
            private bool PowerPointConvert(string sourcePath, string targetPath, PowerPoint.PpSaveAsFileType targetFileType)
            {
                bool result;
                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;
            }
  • 相关阅读:
    5.scala中的对象
    4.scala中的类
    第八章 前端框架
    第六章 用户管理
    第五章 权限验证
    第四章 功能初始化
    第三章 项目结构
    第二章 基于二进制进行权限管理的理论知识
    第一章 权限管理DEMO简介
    NopCommerce源代码分析之用户验证和权限管理
  • 原文地址:https://www.cnblogs.com/kaixing/p/2252486.html
Copyright © 2011-2022 走看看