• 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;
            }
  • 相关阅读:
    Git知识总览(四) git分支管理之rebase 以及 cherry-pick相关操作
    基于visual Studio2013解决算法导论之002归并排序
    基于visual Studio2013解决算法导论之001插入排序
    android用户界面之ScrollView教程实例汇总
    android ScrollView--Linearlayout可以上下拖动
    在 Windows Azure 网站上使用 Django、Python 和 MySQL:创建博客应用程序
    Windows Azure 网站 (WAWS) 和中间证书
    Windows Azure 社区新闻综述(#78 版)
    通过 HTTPS 和 SSL 确保 Windows Azure 网站 (WAWS) 安全
    盘点:#AzureChat
  • 原文地址:https://www.cnblogs.com/kaixing/p/2252486.html
Copyright © 2020-2023  润新知