• 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;
            }
  • 相关阅读:
    请输入关键字
    如何把心动变成行动
    理解ASP.NET MVC系列之一:ASP.NET MVC基于MVC设计模式
    window.showModalDialog()
    visual studio 2010 winform程序不能添加对system.web的引用[转载]
    理解ASP.NET MVC系列之三:从URL到Route
    Dan计划:重新定义人生的10000个小时
    为Visual Studio添加配色方案
    [转载]用缓存服务器负载均衡 提数据库查询效率
    Json的序列化和反序列化
  • 原文地址:https://www.cnblogs.com/kaixing/p/2252486.html
Copyright © 2020-2023  润新知