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

Microsoft.Office.11.0.Object.Library 里面是office 2003对应的dll,可以操作excell\word 文件 立即下载

 

代码支持任意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, 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 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, true, true });

                         //打印输出到指定文件
                         //可以使用 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[] { false, false, 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, true, true });

                         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里面,实现文档的后台转换。
XGBoost原生接口和Sklearn接口参数详解 阅读详情

相关推荐

某银行大数据分析平台之云原生架构

软考架构师论文

doudoudouy的博客 774

c# 生成pdf文件,经测试,生成的pdf很完美,无损失,比起itext等网上流行的那些生成pdf的更完美。

预准备: 1.安装microsoft office 2. 安装microsoft office生成pdf插件SaveAsPDFandXPS.exe,可以在msdn上免费得到。 3.在您的工程项目里添加引用: COM : Microsoft Office 12.0 Object Library   ;  .NET : Microsoft.Office.Interop.Excel  、M

天涯孤岸 4022

开源ISP实战(1)——基于Zynq7020的Vivado工程移植与调试

本文详细介绍了将开源ISP项目移植到正点原子Zynq7020开发板的实战过程。重点阐述了在Vivado中进行工程创建、IP核导入、Block Design框架搭建、时钟配置与管脚约束修改等关键步骤,并提供了系统联调与常见问题的排查思路,为FPGA图像处理开发者提供了完整的移植与调试指南。

weixin_29191081的博客 211

C# 将Word转为PDF、XPS、Epub、RTF(基于Spire.Cloud.Word.SDK)

本文介绍通过调用Spire.Cloud.Word.SDK提供的ConvertApi接口将Word转换为PDF、XPS、Epub、RTF以及将Docx转为Doc格式等。调用接口方法及步骤参考以下步骤: 步骤一:dll文件获取及导入。通过官网本地下载SDK文件包。(须在e-iceblue中国官网在线编辑板块中注册账号并登录) 下载后,解压文件,将Spire.Cloud.Word.Sdk.dll...

Eiceblue的专栏 833

KNX智能家居工程设计应用

KNX,是一个起源于欧洲的总线标准,主要应用于家居和楼宇中,对末端电气设备进行总线式的控制,比如照明、空调、地暖、电动窗帘和遮阳设备等等。根据2017年英国著名咨询公司BSRIA的数据报告显示,在中国的智能家居和灯光控制市场,KNX产品的市场份额为各类产品中排名第一,占据了42%的市场份额,远远领先于其他的标准协议。设备中一个继电器表示一路输出,某些功能可能需要用到多个输出,比如1个窗帘AC输出需要占用两路继电器输出,一个继电器用于控制正转,一个用于控制反转,而普通开关输出则需要占用一路继电器输出即可。

hhhh5555666的博客 940

忆阻器(二)——忆阻模拟器

除了真实的器件,我们也可以用运放和乘法器来搭忆阻模拟器。这节将介绍数学模型、matlab仿真及multisim仿真。 一、数学模型 忆阻分为磁控和荷控两种,这里介绍三次非线性磁控忆阻模型: {I=W(φ)VW(φ)=a+3bφ2dφdt=V \begin{cases} I=W(\varphi)V \\ W(\varphi)=a+3b \varphi ^2\\ \frac{d \varphi }{dt}=V \end{cases} ⎩⎪⎨⎪⎧​I=W(φ)VW(φ)=a+3bφ2dtdφ​=V​ 这里的WWW

http://www.icfgblog.com/ 1万+

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

代码支持任意office格式 需要安装office 2007 还有一个office2007的插件OfficeSaveAsPDFandXPS 下载地址 [url]http://www.microsoft.com/downloads/details.aspx?FamilyId=4D951911-3E7E-4AE6-B059-A2E79ED87041&d...

acass424022的博客 151

脑影像分析软件推荐 | JuSpace

JuSpace是一种允许基于 MRI 测量与核成像衍生估计的跨模态关联的工具包,它涵盖各种神经递质系统,包括多巴胺能、血清素能、去甲肾上腺素能和 GABA(γ-氨基丁酸) 神经传递。JuSpace 由一组 Matlab 功能以及涵盖各种受体系统的 PET 受体图谱组成。

NHKJ_ZF的博客 1728

C#实现office文档转换为PDF或xps的一些方法(留存备用)

代码支持任意office格式   需要安装office 2007 还有一个office2007的插件OfficeSaveAsPDFandXPS   下载地址   [url]http://www.microsoft.com/downloads/details.aspx?FamilyId=4D951911-3E7E-4AE6-B059-A2E79ED87041&displaylang=en

--某些人-- 1978

【3天快速入门WPF】8-数据绑定基础

以下是一个简单的 WPF 数据绑定示例,使用两个TextBox控件分别表示Name和Age来进行进行数据绑定试验。运行程序后,你会看到初始界面中TextBox 中显示初始名字 “张三”,TextBox 中显示初始年龄 “25”。在 XAML 中创建一个简单的界面,包含一个 TextBox 用于输入名字,一个 TextBox 用于显示年龄。当你在 TextBox 中修改名字时,Person 对象的 Name 属性会被自动更新,反之亦然。它的主要作用是为控件及其子控件提供一个默认的数据源,用于数据绑定。

Zy100Papa的博客 973

华为OD机试双机位C卷-敌情监控 (Py/Java/C/C++/Js/Go)

华为OD机试(机考)双机位C卷 【敌情监控】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为OD机试 - 敌情监控。

qq_45776114的博客 944

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

原创:http://seanli888.blog.51cto.com/345958/112268 这段时间一直在研究office文档转为PDF或xps格式的方法。查找了一些资料。 一下是我所知道的一些方法 代码支持任意office格式 需要安装office 2007 还有一个office2007的插件OfficeSaveAsPDFandXPS 下载地址 http://www.mic...

weixin_34008933的博客 121

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

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

源博客http://blog.csdn.net/kable999/article/details/4786654 代码支持任意office格式 需要安装office 2007 还有一个office2007的插件OfficeSaveAsPDFandXPS 下载地址 [url]http://www.microsoft.com/downloads/details.aspx?FamilyId=4D...

weixin_34362790的博客 138

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

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

<br />这段时间一直在研究office文档转为PDF或xps格式的方法。查找了一些资料。一下是我所知道的一些方法代码支持任意office格式需要安装office 2007 还有一个office2007的插件OfficeSaveAsPDFandXPS下载地址[url]http://www.microsoft.com/downloads/details.aspx?FamilyId=4D951911-3E7E-4AE6-B059-A2E79ED87041&displaylang=en[/url]这是一个微软官

gaosiling的专栏 508

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

上一篇: 用C#制作PDF文件全攻略(转)
下一篇: c#获取路径
kable999
博客等级 码龄20年 15粉丝 263原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值