博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#实现office文档转换为PDF或xps的一些方法--zt
阅读量:4507 次
发布时间:2019-06-08

本文共 6756 字,大约阅读时间需要 22 分钟。

原创:

这段时间一直在研究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转换方法
InBlock.gif
private 
bool Convert(
string sourcePath, 
string targetPath, Word.WdExportFormat exportFormat) 
InBlock.gif                { 
InBlock.gif                        
bool result; 
InBlock.gif                        
object paramMissing = Type.Missing; 
InBlock.gif                        Word.ApplicationClass wordApplication = 
new Word.ApplicationClass(); 
InBlock.gif                        Word.Document wordDocument = 
null
InBlock.gif                        
try 
InBlock.gif                        { 
InBlock.gif                                
object paramSourceDocPath = sourcePath; 
InBlock.gif                                
string paramExportFilePath = targetPath; 
InBlock.gif 
InBlock.gif                                Word.WdExportFormat paramExportFormat = exportFormat; 
InBlock.gif                                
bool paramOpenAfterExport = 
false
InBlock.gif                                Word.WdExportOptimizeFor paramExportOptimizeFor = 
InBlock.gif                                        Word.WdExportOptimizeFor.wdExportOptimizeForPrint; 
InBlock.gif                                Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument; 
InBlock.gif                                
int paramStartPage = 0; 
InBlock.gif                                
int paramEndPage = 0; 
InBlock.gif                                Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent; 
InBlock.gif                                
bool paramIncludeDocProps = 
true
InBlock.gif                                
bool paramKeepIRM = 
true
InBlock.gif                                Word.WdExportCreateBookmarks paramCreateBookmarks = 
InBlock.gif                                        Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks; 
InBlock.gif                                
bool paramDocStructureTags = 
true
InBlock.gif                                
bool paramBitmapMissingFonts = 
true
InBlock.gif                                
bool paramUseISO19005_1 = 
false
InBlock.gif 
InBlock.gif                                wordDocument = wordApplication.Documents.Open( 
InBlock.gif                                        
ref paramSourceDocPath, 
ref paramMissing, 
ref paramMissing, 
InBlock.gif                                        
ref paramMissing, 
ref paramMissing, 
ref paramMissing, 
InBlock.gif                                        
ref paramMissing, 
ref paramMissing, 
ref paramMissing, 
InBlock.gif                                        
ref paramMissing, 
ref paramMissing, 
ref paramMissing, 
InBlock.gif                                        
ref paramMissing, 
ref paramMissing, 
ref paramMissing, 
InBlock.gif                                        
ref paramMissing); 
InBlock.gif 
InBlock.gif                                
if (wordDocument != 
null
InBlock.gif                                        wordDocument.ExportAsFixedFormat(paramExportFilePath, 
InBlock.gif                                                paramExportFormat, paramOpenAfterExport, 
InBlock.gif                                                paramExportOptimizeFor, paramExportRange, paramStartPage, 
InBlock.gif                                                paramEndPage, paramExportItem, paramIncludeDocProps, 
InBlock.gif                                                paramKeepIRM, paramCreateBookmarks, paramDocStructureTags, 
InBlock.gif                                                paramBitmapMissingFonts, paramUseISO19005_1, 
InBlock.gif                                                
ref paramMissing); 
InBlock.gif                                result = 
true
InBlock.gif                        } 
InBlock.gif                        
finally 
InBlock.gif                        { 
InBlock.gif                                
if (wordDocument != 
null
InBlock.gif                                { 
InBlock.gif                                        wordDocument.Close(
ref paramMissing, 
ref paramMissing, 
ref paramMissing); 
InBlock.gif                                        wordDocument = 
null
InBlock.gif                                } 
InBlock.gif                                
if (wordApplication != 
null
InBlock.gif                                { 
InBlock.gif                                        wordApplication.Quit(
ref paramMissing, 
ref paramMissing, 
ref paramMissing); 
InBlock.gif                                        wordApplication = 
null
InBlock.gif                                } 
InBlock.gif                                GC.Collect(); 
InBlock.gif                                GC.WaitForPendingFinalizers(); 
InBlock.gif                                GC.Collect(); 
InBlock.gif                                GC.WaitForPendingFinalizers(); 
InBlock.gif                        } 
InBlock.gif                        
return result; 
InBlock.gif                }
Excel转换方法
InBlock.gif
private 
bool Convert(
string sourcePath, 
string targetPath, XlFixedFormatType targetType) 
InBlock.gif                { 
InBlock.gif                        
bool result; 
InBlock.gif                        
object missing = Type.Missing; 
InBlock.gif                        ApplicationClass application = 
null
InBlock.gif                        Workbook workBook = 
null
InBlock.gif                        
try 
InBlock.gif                        { 
InBlock.gif                                application = 
new ApplicationClass(); 
InBlock.gif                                
object target = targetPath; 
InBlock.gif                                
object type = targetType; 
InBlock.gif                                workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing, 
InBlock.gif                                        missing, missing, missing, missing, missing, missing, missing, missing, missing); 
InBlock.gif 
InBlock.gif                                workBook.ExportAsFixedFormat(targetType, target, XlFixedFormatQuality.xlQualityStandard, 
true
false, missing, missing, missing, missing); 
InBlock.gif                                result = 
true
InBlock.gif                        } 
InBlock.gif                        
catch 
InBlock.gif                        { 
InBlock.gif                                result = 
false
InBlock.gif                        } 
InBlock.gif                        
finally 
InBlock.gif                        { 
InBlock.gif                                
if (workBook != 
null
InBlock.gif                                { 
InBlock.gif                                        workBook.Close(
true, missing, missing); 
InBlock.gif                                        workBook = 
null
InBlock.gif                                } 
InBlock.gif                                
if (application != 
null
InBlock.gif                                { 
InBlock.gif                                        application.Quit(); 
InBlock.gif                                        application = 
null
InBlock.gif                                } 
InBlock.gif                                GC.Collect(); 
InBlock.gif                                GC.WaitForPendingFinalizers(); 
InBlock.gif                                GC.Collect(); 
InBlock.gif                                GC.WaitForPendingFinalizers(); 
InBlock.gif                        } 
InBlock.gif                        
return result; 
InBlock.gif                }
PowerPoint转换方法
InBlock.gif                
private 
bool Convert(
string sourcePath, 
string targetPath, PpSaveAsFileType targetFileType) 
InBlock.gif                { 
InBlock.gif                        
bool result; 
InBlock.gif                        
object missing = Type.Missing; 
InBlock.gif                        ApplicationClass application = 
null
InBlock.gif                        Presentation persentation = 
null
InBlock.gif                        
try 
InBlock.gif                        { 
InBlock.gif                                application = 
new ApplicationClass(); 
InBlock.gif                                persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse); 
InBlock.gif                                persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue); 
InBlock.gif 
InBlock.gif                                result = 
true
InBlock.gif                        } 
InBlock.gif                        
catch 
InBlock.gif                        { 
InBlock.gif                                result = 
false
InBlock.gif                        } 
InBlock.gif                        
finally 
InBlock.gif                        { 
InBlock.gif                                
if (persentation != 
null
InBlock.gif                                { 
InBlock.gif                                        persentation.Close(); 
InBlock.gif                                        persentation = 
null
InBlock.gif                                } 
InBlock.gif                                
if (application != 
null
InBlock.gif                                { 
InBlock.gif                                        application.Quit(); 
InBlock.gif                                        application = 
null
InBlock.gif                                } 
InBlock.gif                                GC.Collect(); 
InBlock.gif                                GC.WaitForPendingFinalizers(); 
InBlock.gif                                GC.Collect(); 
InBlock.gif                                GC.WaitForPendingFinalizers(); 
InBlock.gif                        } 
InBlock.gif                        
return result; 
InBlock.gif                }
 
如果是word文档或者Excel文档想要转换xps格式,我们还可以有一种方法,那就是利用xps虚拟打印机实现。
安装了.NetFrameWork3.5之后,默认会在系统中安装XPS虚拟打印机,我们将其设置为默认打印机。
Microsoft XPS Document Writer
word文档打印为xps
InBlock.gif                
public 
void PrintWord(
string wordfile) 
InBlock.gif                { 
InBlock.gif                        oWord.ApplicationClass word = 
new oWord.ApplicationClass(); 
InBlock.gif                        Type wordType = word.GetType(); 
InBlock.gif 
InBlock.gif                        
//打开WORD文档 
InBlock.gif                        oWord.Documents docs = word.Documents; 
InBlock.gif                        Type docsType = docs.GetType(); 
InBlock.gif                        
object objDocName = wordfile; 
InBlock.gif                        oWord.Document doc = (oWord.Document)docsType.InvokeMember(
"Open", System.Reflection.BindingFlags.InvokeMethod, 
null, docs, 
new Object[] { objDocName, 
true
true }); 
InBlock.gif 
InBlock.gif                        
//打印输出到指定文件 
InBlock.gif                        
//可以使用 doc.PrintOut();方法,次方法调用中的参数设置较繁琐,建议使用 Type.InvokeMember 来调用时可以不用将PrintOut的参数设置全,只设置4个主要参数 
InBlock.gif                        Type docType = doc.GetType(); 
InBlock.gif                        
object printFileName = wordfile + 
".xps"
InBlock.gif                        docType.InvokeMember(
"PrintOut", System.Reflection.BindingFlags.InvokeMethod, 
null, doc, 
new 
object[] { 
false
false, oWord.WdPrintOutRange.wdPrintAllDocument, printFileName }); 
InBlock.gif 
InBlock.gif                        
//退出WORD 
InBlock.gif                        wordType.InvokeMember(
"Quit", System.Reflection.BindingFlags.InvokeMethod, 
null, word, 
null); 
InBlock.gif                }
Excel利用虚拟打印机转换为xps
InBlock.gif                
public 
void PrintExcel(
string execlfile) 
InBlock.gif                { 
InBlock.gif                        Excel.ApplicationClass eapp = 
new Excel.ApplicationClass(); 
InBlock.gif                        Type eType = eapp.GetType(); 
InBlock.gif                        Excel.Workbooks Ewb = eapp.Workbooks; 
InBlock.gif                        Type elType = Ewb.GetType(); 
InBlock.gif                        
object objelName = execlfile; 
InBlock.gif                        Excel.Workbook ebook = (Excel.Workbook)elType.InvokeMember(
"Open", System.Reflection.BindingFlags.InvokeMethod, 
null, Ewb, 
new Object[] { objelName, 
true
true }); 
InBlock.gif 
InBlock.gif                        
object printFileName = execlfile + 
".xps"
InBlock.gif 
InBlock.gif                        Object oMissing = System.Reflection.Missing.Value; 
InBlock.gif                        ebook.PrintOut(oMissing, oMissing, oMissing, oMissing, oMissing, 
true, oMissing, printFileName); 
InBlock.gif 
InBlock.gif                        eType.InvokeMember(
"Quit", System.Reflection.BindingFlags.InvokeMethod, 
null, eapp, 
null); 
InBlock.gif                }
,同样,如果系统安装了PDF虚拟打印机,如5D PDF之类的,我们也可以将其设置为默认打印机以达到转换为PDF格式的目的
我们可以将这些方法放在windows service里面,实现文档的后台转换。

本文出自 “” 博客,请务必保留此出处

转载于:https://www.cnblogs.com/Nina-piaoye/archive/2011/01/20/1939815.html

你可能感兴趣的文章
SQL结构化查询语言
查看>>
(六)标准及其他
查看>>
标准I/O库函数的缺陷
查看>>
django思维导图
查看>>
C#综合揭秘——通过修改注册表建立Windows自定义协议
查看>>
折半插入排序
查看>>
linux查看是否能访问外网及拥有的公网IP
查看>>
Js中String转int
查看>>
Python3.x:代理ip刷点赞
查看>>
ETL
查看>>
oracle分析函数
查看>>
MDX members使用
查看>>
啤酒和饮料 蓝桥杯
查看>>
解决evernote更新报错问题
查看>>
JAVA面试——数据结构&算法
查看>>
二分快速幂
查看>>
Java——Java线程安全的集合详解
查看>>
[转载]iOS Provisioning Profile(Certificate)与Code Signing详解
查看>>
sublime text3 函数追踪插件
查看>>
ios xmpp文件传输
查看>>