c#实现word,excel转pdf代码及部分Office 2007文件格式转换为xps和pdf代码整理
转换功能是通过调用安装了转换XPS和PDF的AddIn的Office2007对象模型完成的. 代码支持Office 2007支持的一切文件格式:
|
Office 2007组件 |
扩展名 |
|
Word |
DOC, DOCX, DOCM, DOTX, DOTM, DOT, TXT, RTP, RTF |
|
Excel |
XLS, XLSX, XLSM, XML |
|
PowerPoint |
PPT, PPTX, PPTM, POTX, PPSX, PPSM, POTM |
添加对三个组件的引用: 


这里使用一个枚举类型来来决定生成文件的类型,包括:
其实可以使用个方法来实现这个功能,这里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 = ;
int paramEndPage = ;
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;
}
c#实现word,excel转pdf代码及部分Office 2007文件格式转换为xps和pdf代码整理的更多相关文章
- VSTO 为Office已有右键菜单添加自己的菜单项(word,Excel)
原文:VSTO 为Office已有右键菜单添加自己的菜单项(word,Excel) private void AddRightMenu() { Microsoft ...
- PDF/WORD/EXCEL/PPT 文档在线阅读
查资料看了2种解决方法: 1.通过办公软件dll转换,用flans去看 2.通过Aspose转换成pdf格式,在用js前台读pdf(我用的pdf.js) 今天我解决的就是WORD/EXCEL/PPT ...
- lucent检索技术之创建索引:使用POI读取txt/word/excel/ppt/pdf内容
在使用lucent检索文档时,必须先为各文档创建索引.索引的创建即读出文档信息(如文档名称.上传时间.文档内容等),然后再经过分词建索引写入到索引文件里.这里主要是总结下读取各类文档内容这一步. 一. ...
- Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享
Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享 在此,先分享下写此文前的经历与感受,我所有的感觉浓缩到一个字,那就是:"坑&qu ...
- java 如何将 word,excel,ppt如何转pdf --openoffice (1)
承上启下,可折叠 上一篇说的是:服务器是windows server时,用jacob将msoffice(指的是word,excel,ppt)转换成pdf. 若被部署项目的服务器是centOS等linu ...
- word/excel/ppt 2 PDF
PHP 实现 word/excel/ppt 转换为 PDF 一般最常见的就是利用OpenOffice来转换,来看看实现的核心代码: class PDFConverter { private $com; ...
- Java通过openOffice实现word,excel,ppt转成pdf实现在线预览
Java通过openOffice实现word,excel,ppt转成pdf实现在线预览 一.OpenOffice 1.1 下载地址 1.2 JodConverter 1.3 新建实体类PDFDemo ...
- Office系列---将Office文件(Word、PPT、Excel)转换为PDF文件,提取Office文件(Word、PPT)中的所有图片
将Office文件转换为PDF文件,提取Office文件中的所有图片 1.Office系列---将Office文件(Word.PPT.Excel)转换为PDF文件 1.1 基于Office实现的解决方 ...
- Atitit.office word excel ppt pdf 的web在线预览方案与html转换方案 attilax 总结
Atitit.office word excel ppt pdf 的web在线预览方案与html转换方案 attilax 总结 1. office word excel pdf 的web预览要求 ...
随机推荐
- C# 多态理解
封装.继承.多态,面向对象的三大特性,前两项理解相对容易,但要理解多态,特别是深入的了解,对于初学者而言可能就会有一定困难了.我一直认为学习OO的最好方法就是结合实践,封装.继承在实际工作中的应用随处 ...
- Beef安装与简单使用
安装 Debian / Ubuntu 安装依赖 sudo apt-get update sudo apt-get install curl git curl -sSL https://raw.gith ...
- Android studio 创建安卓项目hello
Android studio是一个非常好用的软件,只不过在使用的最开始,由于各种问题,会失败,并且新手本身就不懂的情况下,更加的懵逼. 这里我来记录一下我遇到过的一点坑. 首先,Android stu ...
- 【.Net】Thread.Start()与ThreadPool.QueueUserWorkItem()的区别
百度搜到的靠前的几篇文章,都是写了两种API的使用实例,但并没有说清两者的具体差别. 直接上stackoverflow搜才是正确的姿势.(想上谷歌,然而十/九_大|期间VPN各种被墙,就很气) 参考: ...
- mongo批量更新、导入导出脚本
批量更新,一定要加上最后的条件: db.getCollection('cuishou_user').update( {,,,,,]}}, //query {$set:{)}},// update {m ...
- Mongo分区后分片下count记录不准确
问题描述 问题如图,后来上网查了一下,发现了这是正常现象: 官方文档解释了这种现象的原因以及解决方法: 不准确的原因: 操作的是分片的集合(前提): shard分片正在做块迁移,导致有重复数据出现 存 ...
- [看门狗]基于Linux的嵌入式系统全程喂狗策略
转自:http://blog.csdn.net/erickhuang1989/article/details/8721548 在嵌入式系统中,为了使系统在异常情况下能自动恢复,一般都会引入看门狗电路. ...
- Tomcat性能优化(三) Executor配置
http://hello-nick-xu.iteye.com/blog/2113853 http://blog.chinaunix.net/uid-12115233-id-3358004.html
- Android ——利用OnDraw实现自定义View(转)
自定义View的实现方式大概可以分为三种,自绘控件.组合控件.以及继承控件.本文将介绍自绘控件的用法.自绘控件的意思是,这个控件上的内容是用onDraw函数绘制出来的.关于onDraw函数的介绍可参看 ...
- 【转】Graphics 关于呈现质量与合成模式
GDI+相关的作图,每种语言都有其自身的封装类,但本质上是一样的,下面这篇文章写的非常好,对于C#的Graphics类也是使用的,所以转载了,原文在这里: http://www.cnblogs.com ...