操作word,Excel,PPT

//C#编程,将Word转PDF,该方法有一定弊端,但是比起网路上的其他方法来说,还算比较好的,弊端是需要客户机上安装有WORD 2007或更高的版本。
      //1、添加引用: Microsoft.Office.Interop.Word版本12.0.0.0;
      // 2、在开头添加命名空间引用:using Microsoft.Office.Interop.Word;
      //3、具体实现方法如下:
      //Word转换成pdf
      ///<summary>
      /// 把Word文件转换成为PDF格式文件 ///</summary>
      ///<param name="sourcePath">源文件路径</param> ///<param name="targetPath">目标文件路径</param> ///<returns>true=转换成功</returns>
      public bool WordToPDF(string sourcePath, string targetPath)
      {
          //sourcePath = Server.MapPath("/Content/file/吴波.docx");
          //targetPath = Server.MapPath("/Content/file/吴波.pdf");
          bool result = false;
          WdExportFormat exportFormat = WdExportFormat.wdExportFormatPDF;
          object paramMissing = Type.Missing;
          ApplicationClass wordApplication = new ApplicationClass();
          _Document wordDocument = null;
          try
          {
              object paramSourceDocPath = sourcePath;
              string paramExportFilePath = targetPath;
              WdExportFormat paramExportFormat = exportFormat;
              bool paramOpenAfterExport = false;
              WdExportOptimizeFor paramExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint;
              WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;
              int paramStartPage = 0;
              int paramEndPage = 0;
              WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;
              bool paramIncludeDocProps = true;
              bool paramKeepIRM = true;
              WdExportCreateBookmarks paramCreateBookmarks = 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;
          }
          catch
          {
              result = false;
          }
          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;
      }

 

      /// <summary>
      /// 把Excel文件转换成PDF格式文件 
      /// </summary>
      /// <param name="sourcePath">源文件路径</param>
      /// <param name="targetPath">目标文件路径</param>
      /// <returns>true=转换成功</returns>
      public bool XLSConvertToPDF(string sourcePath, string targetPath)
      {
          bool result = false;
          Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF;
          object missing = Type.Missing;
          Excel.ApplicationClass application = null;
          Excel.Workbook workBook = null;
          try
          {
              application = new Excel.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, Excel.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;
      }
      ///<summary>       
      /// 把PowerPoint文件转换成PDF格式文件      
      ///</summary>       
      ///<param name="sourcePath">源文件路径</param>    
      ///<param name="targetPath">目标文件路径</param>
      ///<returns>true=转换成功</returns>
      public bool PPTConvertToPDF(string sourcePath, string targetPath)
      {
          bool result;
          PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
          object missing = Type.Missing;
          PowerPoint.ApplicationClass application = null;
          PowerPoint.Presentation persentation = null;
          try
          {
              application = new PowerPoint.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)
              {

  //C#编程,将Word转PDF,该方法有一定弊端,但是比起网路上的其他方法来说,还算比较好的,弊端是需要客户机上安装有WORD 2007或更高的版本。
        //1、添加引用: Microsoft.Office.Interop.Word版本12.0.0.0;
        // 2、在开头添加命名空间引用:using Microsoft.Office.Interop.Word;
        //3、具体实现方法如下:
        //Word转换成pdf
        ///<summary>
        /// 把Word文件转换成为PDF格式文件 ///</summary>
        ///<param name="sourcePath">源文件路径</param> ///<param name="targetPath">目标文件路径</param> ///<returns>true=转换成功</returns>
        public bool WordToPDF(string sourcePath, string targetPath)
        {
            //sourcePath = Server.MapPath("/Content/file/吴波.docx");
            //targetPath = Server.MapPath("/Content/file/吴波.pdf");
            bool result = false;
            WdExportFormat exportFormat = WdExportFormat.wdExportFormatPDF;
            object paramMissing = Type.Missing;
            ApplicationClass wordApplication = new ApplicationClass();
            _Document wordDocument = null;
            try
            {
                object paramSourceDocPath = sourcePath;
                string paramExportFilePath = targetPath;
                WdExportFormat paramExportFormat = exportFormat;
                bool paramOpenAfterExport = false;
                WdExportOptimizeFor paramExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint;
                WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;
                int paramStartPage = 0;
                int paramEndPage = 0;
                WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;
                bool paramIncludeDocProps = true;
                bool paramKeepIRM = true;
                WdExportCreateBookmarks paramCreateBookmarks = 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;
            }
            catch
            {
                result = false;
            }
            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;
        }

        /// <summary>
        /// 把Excel文件转换成PDF格式文件
        /// </summary>
        /// <param name="sourcePath">源文件路径</param>
        /// <param name="targetPath">目标文件路径</param>
        /// <returns>true=转换成功</returns>
        public bool XLSConvertToPDF(string sourcePath, string targetPath)
        {
            bool result = false;
            Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF;
            object missing = Type.Missing;
            Excel.ApplicationClass application = null;
            Excel.Workbook workBook = null;
            try
            {
                application = new Excel.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, Excel.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;
        }
        ///<summary>
        /// 把PowerPoint文件转换成PDF格式文件
        ///</summary>
        ///<param name="sourcePath">源文件路径</param>
        ///<param name="targetPath">目标文件路径</param>
        ///<returns>true=转换成功</returns>
        public bool PPTConvertToPDF(string sourcePath, string targetPath)
        {
            bool result;
            PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
            object missing = Type.Missing;
            PowerPoint.ApplicationClass application = null;
            PowerPoint.Presentation persentation = null;
            try
            {
                application = new PowerPoint.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;
        }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

                  persentation.Close();

                  persentation = null;

              }

              if (application != null)

              {

                  application.Quit();

                  application = null;

              }

              GC.Collect();

              GC.WaitForPendingFinalizers();

              GC.Collect();

              GC.WaitForPendingFinalizers();

          }

          return result;

      }

时间: 2024-10-29 09:07:52

操作word,Excel,PPT的相关文章

Aspose是一个很强大的控件,可以用来操作word,excel,ppt等文件

Aspose是一个很强大的控件,可以用来操作word,excel,ppt等文件,用这个控件来导入.导出数据非常方便.其中Aspose.Cells就是用来操作Excel的,功能有很多.我所用的是最基本的功能,读取Excel的数据并导入到Dataset或数据库中.读取Excel表格数据的代码如下: 首先要引入命名空间:using Aspose.Cells; Workbook workbook = new Workbook(); workbook.Open("C:\\test.xlsx");

Atitit.office&#160;word&#160;&#160;excel&#160;&#160;ppt&#160;pdf&#160;的web在线预览方案与html转换方案&#160;attilax&#160;总结

Atitit.office word  excel  ppt pdf 的web在线预览方案与html转换方案 attilax 总结 1. office word  excel pdf 的web预览要求1 1.1. 显示效果要好1 1.2. 可以自定义显示界面1 1.3. 不需要控件,兼容性好1 1.4. 支持编辑操作1 2. 纯html预览解决之道(自由的格式)1 3. 转换swf flash方案2 4. 转换pdf方式..更多的浏览器已经直接支持pdf格式查看2 5. 控件方式2 6. Hyb

java 如果将 word,excel,ppt如何转pdf --openoffice (1)

承上启下,可折叠 上一篇说的是:服务器是windows server时,用jacob将msoffice(指的是word,excel,ppt)转换成pdf. 若被部署项目的服务器是centOS等linux server时,就不能用之前的上述说的那种方式了. 在上一篇说到openoffice将msoffice转成pdf的时候会存在排版错位的问题,或者有的内容消失了,这是因为msoffice中的一些特有格式,openoffice不识别解析不了导致的.当然大部分的普通msoffice文档转换成pdf时,

python操作word、ppt的详解

python使用win32com的心得 python可以使用一个第三方库叫做win32com达到操作com的目的, 我是安装了ActivePython的第三方库,从官网下载了安装包,该第三方库几乎封装了所有python下面的win32相关的操作,例如win32api,win32gui等等,可以说是比较齐全的了,下载地址可以自行百度获取.         主要是有个项目可能要用到ppt转换成视频的功能. 之后在想使用com操作excel还有word,ppt的时候,相信大部分人跟我一样,都是搜索py

在Java中如何操作word, excel, pdf文件

java操作word,excel,pdf 在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下java对word.excel.pdf文件的读取.本篇博客只是讲解简单应用.如果想深入了解原理.请读者自行研究一些相关源码. 首先我们来认识一下读取相关文档的jar包: 1. 引用POI包读取word文档内容 poi.jar 下载地址 http://apache.freelamp.com/poi/release/bin/poi-

主题:jacob操作word excel

项目开发过程中,需求涉及到了各种文档转换为HTML或者网页易显示格式,现在将实现方式整理如下:  一.了解Jacob 先了解一下概念,JACOB 就是 JAVA-COM Bridge的缩写,提供自动化的访问com的功能,也是通过JNI功能访问windows平台下的com组件或者win32系统库的.这是一个开始于1999年的开源项目的成果,有很多使用者对该项目进行了修改,做出了自己的贡献. 下载地址:http://sourceforge.net/project/showfiles.php?grou

Microsoft word/Excel/PPT打开报错问题

很多时候我们会发现我们辛辛苦苦从网上.论坛或QQ群等地下载下来的文档.表格.PPT竟然打不开,许多人以为是文件的问题,直接删除了,而身边很多朋友会将此类问题发于我让我一一解答,其实这些问题,网上都有相关的解决方法的,所以今天我在这里再为大家综合整理一番,供身边的同事.朋友及大家更好的参考学习. 一.EXCEL问题: 报错信息如下: 内存或磁盘空间不足,MICROSOFT EXCEL 无法再次打开或保存任何文档. 要想获得更多的可用内存,请关闭不再使用的工作薄或程序. 要想释放磁盘空间,请删除相应

lucent检索技术之创建索引:使用POI读取txt/word/excel/ppt/pdf内容

在使用lucent检索文档时,必须先为各文档创建索引.索引的创建即读出文档信息(如文档名称.上传时间.文档内容等),然后再经过分词建索引写入到索引文件里.这里主要是总结下读取各类文档内容这一步. 一.之前做过一个小工具也涉及到读取word和excel内容,采用的是com组件的方式来读取.即导入COM库,引入命名空间(using Microsoft.Office.Interop.Word;using Microsoft.Office.Interop.Excel;),然后读代码如下: 读取word

word,excel,ppt,txt转换为 PDF

/// <summary> /// 将word文档转换成PDF格式 /// </summary> /// <param name="sourcePath"></param> /// <param name="targetPath"></param> /// <returns></returns> public static bool ConvertWord2Pdf(str