转换功能是通过调用安装了转换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 = 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, 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;
        }

感谢同事Hong的协助,把这部分功能实现,现在share给大家,希望为需要的朋友节省时间.
另外浏览xps文件有一个不错的小工具XPS Viewer EP.

常用Office 2007文件格式转换为xps和pdf代码整理相关推荐

  1. word中将空格替换为_如何在Office 2007中将Word保存为PDF

    word中将空格替换为 How to save a Word document to be a PDF in Office 2007? 如何在Office 2007中将Word 文档保存为PDF? I ...

  2. Office 2003打开Office 2007文件格式的兼容软件包

    软件大小:27.55MB 软件语言:简体中文 软件类别:国产软件/文字处理 授权方式:特殊软件 应用平台:Win2003/WinXP/Win2000 更新时间:2007-08-28 OFFICE200 ...

  3. Microsoft office 2007 word PPT 转pdf的插件(转)

    大家肯定常常office转pdf.特别是Word和PowerPoint,窃以为,foxit什么的软件并不方便. 其实微软官方开发了插件,并在网站发布:SaveAsPDFandXPS. 随office ...

  4. Microsoft office 2007 word PPT 转pdf的插件

    大家肯定常常office转pdf.特别是Word和PowerPoint,窃以为,foxit什么的软件并不方便. 其实微软官方开发了插件,并在网站发布:SaveAsPDFandXPS. 随office ...

  5. Office 2007必将掀起办公软件行业的一场飓风

    Office 2007必将掀起办公软件行业的一场飓风 微软的Office 办公软件现在在中国可以是说是家喻户晓,特别是最新版Office2007问世以来,一直受到众多网民的关注.新版本不仅功能更加强大 ...

  6. Office 2007兼容工具包

    Office 2007兼容工具包   -------------------------------------------------------------------------------- ...

  7. Microsoft Office (2007) Open XML 文件格式

    适用于: 2007 Microsoft Office 套件 Microsoft Office Excel 2007 Microsoft Office PowerPoint 2007 Microsoft ...

  8. C#实现office文档转换为PDF格式

    C#实现office文档转换为PDF格式 1.安装组件OfficeSaveAsPDFandXPS 需要安装office 2007 还有一个office2007的插件OfficeSaveAsPDFand ...

  9. 仿百度文库方案[openoffice.org 3+swftools+flexpaper](三) 之 使用JODConverter将office文档转换为pdf...

    第三步,使用JODConverter将office文档转换为pdf JODConverter是一个java的OpenDucument文件转换器,可以进行许多文件格式的转换,它利用 OpenOffice ...

最新文章

  1. 2018-3-27 专家系统
  2. 【微信小程序】数组操作
  3. 【ZOJ - 2724】【HDU - 1509】Windows Message Queue(优先队列)
  4. java订单超时取消设计_PHP如何实现处理过期或者超时订单的,并还原库存
  5. php返回原界面,thinkphp实现登录后返回原界面
  6. Robot Framework(十二) javascript基础
  7. OKB突破7.2USDT,再创12月新高
  8. 8.10 NOIP模拟测试16 Blue+Weed+Drink
  9. 理解Promise的3种姿势
  10. 电脑数据格式化如何快速简单恢复数据?
  11. ubuntu server 20 开启无线热点
  12. wp友情链接php代码,wordpress友情链接函数详解
  13. B站陈睿团队以内容为王,百万UP主共同成长
  14. 码元,数据传输速率,带宽,信噪比,信道容量
  15. 7-3 是不是太胖了
  16. 【北大青鸟天府校区的Java专业怎么样?】
  17. 我的世界神奇宝贝服务器注册指令,《我的世界》神奇宝贝MOD召唤指令大全
  18. jzoj2702. 探险jzoj3917. 【NOIP2014模拟11.2A组】福慧双修
  19. ORACLE 11g 修改db_unique_name参数
  20. how2j:JAVA学习笔记——DAY 9

热门文章

  1. linux查找文件夹命令_如何在Linux中使用命令行查找文件和文件夹
  2. ATT 和Verizon 谁才是美国移动运营商老大?
  3. Modbus在Android上的应用之Modbus TCP Slave
  4. python公司一般都是什么样的公司-人们对Python在企业级开发中的10大误解
  5. 安装Windows、Ubuntu双系统超简单实用教程
  6. 「数智双BUFF」关键业务加速篇
  7. ER图学习笔记(附各个图型的举例,实战案例)
  8. 数据降维几种方法,主成分分析学习和代码实现
  9. 【C++】什么是RAII?
  10. 华为云开发者联盟助力培养数字化人才,加速应用构建质效提升