`
ming_7755
  • 浏览: 37716 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

C#实现office文档转换为PDF或xps的一些方法

 
阅读更多

这段时间一直在研究office文档转为PDF或xps格式的方法。查找了一些资料。
一下是我所知道的一些方法
代码支持任意office格式
需要安装office 2007 还有一个office2007的插件OfficeSaveAsPDFandXPS
下载地址
这是一个微软官方出的office插件。
安装好之后,打开VS,以VS2005为例
新建windows应用程序项目
添加以下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;
 
我们可以使用一个枚举类型来决定生成文件的类型
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;
 
这里Word跟Excel我使用了ExportAsFixedFormat,PowerPoint我使用了SaveAs方法。
对于Word跟PowerPoint效果是一样的,只是SaveAs方法支持的格式更多。
但是Excel似乎不支持SaveAs方法,呵呵
----------------
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
                                        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; 
                }
Excel转换方法
private bool Convert(string sourcePath, string targetPath, XlFixedFormatType targetType) 
                { 
                        bool result; 
                        object missing = Type.Missing; 
                        ApplicationClass application = null
                        Workbook workBook = null
                        try 
                        { 
                                application = new 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, truefalse, 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 Convert(string sourcePath, string targetPath, PpSaveAsFileType targetFileType) 
                { 
                        bool result; 
                        object missing = Type.Missing; 
                        ApplicationClass application = null
                        Presentation persentation = null
                        try 
                        { 
                                application = new 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; 
                }
 
如果是word文档或者Excel文档想要转换xps格式,我们还可以有一种方法,那就是利用xps虚拟打印机实现。
安装了.NetFrameWork3.5之后,默认会在系统中安装XPS虚拟打印机,我们将其设置为默认打印机。
Microsoft XPS Document Writer
word文档打印为xps
                public void PrintWord(string wordfile) 
                { 
                        oWord.ApplicationClass word = new oWord.ApplicationClass(); 
                        Type wordType = word.GetType(); 
 
                        //打开WORD文档 
                        oWord.Documents docs = word.Documents; 
                        Type docsType = docs.GetType(); 
                        object objDocName = wordfile; 
                        oWord.Document doc = (oWord.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { objDocName, truetrue }); 
 
                        //打印输出到指定文件 
                        //可以使用 doc.PrintOut();方法,次方法调用中的参数设置较繁琐,建议使用 Type.InvokeMember 来调用时可以不用将PrintOut的参数设置全,只设置4个主要参数 
                        Type docType = doc.GetType(); 
                        object printFileName = wordfile + ".xps"
                        docType.InvokeMember("PrintOut", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { falsefalse, oWord.WdPrintOutRange.wdPrintAllDocument, printFileName }); 
 
                        //退出WORD 
                        wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null); 
                }
Excel利用虚拟打印机转换为xps
                public void PrintExcel(string execlfile) 
                { 
                        Excel.ApplicationClass eapp = new Excel.ApplicationClass(); 
                        Type eType = eapp.GetType(); 
                        Excel.Workbooks Ewb = eapp.Workbooks; 
                        Type elType = Ewb.GetType(); 
                        object objelName = execlfile; 
                        Excel.Workbook ebook = (Excel.Workbook)elType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, Ewb, new Object[] { objelName, truetrue }); 
 
                        object printFileName = execlfile + ".xps"
 
                        Object oMissing = System.Reflection.Missing.Value; 
                        ebook.PrintOut(oMissing, oMissing, oMissing, oMissing, oMissing, true, oMissing, printFileName); 
 
                        eType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, eapp, null); 
                }
,同样,如果系统安装了PDF虚拟打印机,如5D PDF之类的,我们也可以将其设置为默认打印机以达到转换为PDF格式的目的
我们可以将这些方法放在windows service里面,实现文档的后台转换。

本文出自 “Sean Li 技术成就梦想!” 博客,请务必保留此出处http://seanli888.blog.51cto.com/345958/112268

分享到:
评论

相关推荐

    c#语言将office文档转为XPS

    总结来说,使用C#将Office文档转换为XPS,可以通过Word Automation(需安装Office)、第三方库或结合PDF转换实现。每种方法都有其优缺点,具体选择应根据项目需求和资源限制来确定。在实际开发中,确保测试在各种...

    word 文档转为PDF/XPS

    本文将详细探讨如何使用.NET Remoting服务将Word文档转换为PDF或XPS格式。 首先,让我们了解.NET Remoting。它是.NET框架的一个特性,允许不同应用程序域(AppDomain)之间的对象通信,即使这些对象位于不同的进程...

    Office Convert To Pdf/XPS

    "Office Convert To Pdf/XPS" 是一款工具,主要...通过"Office Convert PDF/XPS"这样的工具,用户可以方便地将日常工作中使用的Office文档转化为更便于分享和存档的PDF或XPS格式,从而提高协作效率和文件管理的便利性。

    XPS转PDF的软件两个

    总之,无论是通过WPS Office还是Adobe Acrobat,将XPS文件转换为PDF都能帮助你更好地管理和共享文档,确保它们在不同平台上的一致性。同时,了解不同转换工具的特点和注意事项,能帮助你做出更适合自己的选择。

    SaveAsPDFandXPS微软PDF转换插件

    它的主要功能是帮助用户将Word文档、Excel表格、PowerPoint演示文稿等Office格式的文件转换为PDF(Portable Document Format)或XPS(XML Paper Specification)格式。这两种格式在跨平台分享和打印时具有良好的一致...

    Aspose.Words和Aspose.Pdf的dll文件.rar

    例如,你可以使用Aspose.Words将Word文档转换为PDF,HTML,EPUB,XPS或其他文本格式。以下是一个简单的C#代码示例,演示如何使用Aspose.Words将Word文档转换为PDF: ```csharp using Aspose.Words; using Aspose....

    批量word转pdf源码

    本项目"批量word转pdf源码"提供了一种方法,利用C#编程语言,结合Microsoft Office Interop库,实现了在Windows环境下批量将Word文档转换为PDF格式。下面我们将详细探讨这个项目的实现原理、关键知识点以及多线程...

    WinForm中如何预览Office文件

    通过Office COM组件将Office文档转换为XPS格式后,可以使用WPF中的DocumentViewer控件来展示XPS文档。DocumentViewer控件是WPF提供的用于查看文档的控件,它可以展示多种文档格式,如PDF、XPS等。 5. 在WinForm中...

    免费Spire.Office全套组件

    对文档的操作包括打开,创建,修改,转换,打印,浏览 Word、Excel、PowerPoint® 和 PDF 文档,以及将数据从数据源导出为常用的文档格式,例如:Word,Excel,RTF,Access,PowerPoint,PDF,XPS,HTML,XML,Text...

    SaveAsPDFandXPS

    标题“SaveAsPDFandXPS”以及描述中的关键词暗示了我们正在讨论的是将Word文档转换为PDF或XPS格式的工具和技术。这两种格式在不同场景下各有优势,PDF(Portable Document Format)通常用于确保文件在不同设备上的...

    Free Spire.Office for .NET 文档内容替换

    对文档的操作包括打开,创建,修改,转换,打印,浏览 Word、Excel、PowerPoint 和 PDF 文档,以及将数据从数据源导出为常用的文档格式,例如:Word,Excel,RTF,Access,PowerPoint,PDF,XPS,HTML,XML,Text,...

    .NET 免费Office类库-Free Spire.Office for .NET_4.3.zip

    对文档的操作包括打开,创建,修改,转换,打印,浏览 Word、Excel、PowerPoint® 和 PDF 文档,以及将数据从数据源导出为常用的文档格式,例如:Word,Excel,RTF,Access,PowerPoint,PDF,XPS,HTML,XML,Text...

    SaveAsPDFandXPS与使用说明.rar

    本文将详细解析如何解决在C#环境下使用`ExportAsFixedFormat`方法将Excel(xlsx/xls)文件转换为PDF或XPS时遇到的“值不在预期范围内”异常,并介绍“SaveAsPDFandXPS”的使用方法。 首先,让我们理解问题的根源。`...

    C# 利用Aspose.Words.dll将 Word 转成PDF

    在介绍如何使用C#编程语言和Aspose.Words.dll库将Word文档转换为PDF格式之前,让我们先梳理一下使用传统方法转换文档时遇到的一些问题。传统上,许多开发者倾向于使用Office 2007及更高版本中自带的组件来实现Word到...

    Excel转 Pdf格式

    3. **C#编程环境下Excel转PDF的方法** #### 1. Excel与PDF的基本概念 - **Excel**: Microsoft Office 套件中的电子表格应用程序,广泛用于数据处理、统计分析等领域。 - **PDF (Portable Document Format)**: 一种...

    Aspose.Words

    特别值得一提的是,Aspose.Words在文档转换方面表现出色,可以将Word文档转换为PDF、HTML、XPS等多种格式,同时也支持从这些格式导入到Word文档。在描述中提到的"txt转PDF"和"PPT转PDF"功能,很大程度上依赖于Aspose...

    Aspose.Words.dll免费

    它可以将Word文档转换为PDF、HTML、XPS、图像文件等格式,也可以从这些格式导入数据到Word文档中。这对于需要在不同平台或设备上保持一致显示的项目特别有用,例如在Web应用中提供PDF下载或者在移动设备上查看Word...

    Aspose.Words for .NET

    3. 文档转换:将Word文档转换为其他格式,如PDF、HTML、XPS、EPUB等,以满足不同平台和设备的需求。 4. 文档合并:将多个文档合并为一个,方便管理和分发。 5. 文档比较:检测并突出显示两个文档之间的差异,有助...

Global site tag (gtag.js) - Google Analytics