当前位置:软件学习 > Powerpoint >>

C#实现MS-Office文档转Pdf(Word、Execel、PowerPoint、Visio、Project)

直接奉上源代码,运行环境:Office2010,Project2010(如果需要),Visio2010(如果需要),.net framework2.0
 
[csharp]  
using System;  
using Microsoft.Office.Core;  
  
namespace Office  
{  
    class Util  
    {  
        private Util() { }  
        /// <summary>  
        /// 把Word文件转换成为PDF格式文件  
        /// </summary>  
        /// <param name="sourcePath">源文件路径</param>  
        /// <param name="targetPath">目标文件路径</param>   
        /// <returns>true=转换成功</returns>  
        public static bool WordToPDF(string sourcePath, string targetPath)  
        {  
            bool result = false;  
            Microsoft.Office.Interop.Word.WdExportFormat exportFormat = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;  
            Microsoft.Office.Interop.Word.ApplicationClass application = null;  
  
            Microsoft.Office.Interop.Word.Document document = null;  
            try  
            {  
                application = new Microsoft.Office.Interop.Word.ApplicationClass();  
                application.Visible = false;  
                document = application.Documents.Open(sourcePath);  
                document.SaveAs2();  
                document.ExportAsFixedFormat(targetPath, exportFormat);  
                result = true;  
            }  
            catch (Exception e)  
            {  
                Console.WriteLine(e.Message);  
                result = false;  
            }  
            finally  
            {  
                if (document != null)  
                {  
                    document.Close();  
                    document = null;  
                }  
                if (application != null)  
                {  
                    application.Quit();  
                    application = null;  
                }  
                GC.Collect();  
                GC.WaitForPendingFinalizers();  
                GC.Collect();  
                GC.WaitForPendingFinalizers();  
            }  
            return result;  
        }  
  
        /// <summary>  
        /// 把Microsoft.Office.Interop.Excel文件转换成PDF格式文件  
        /// </summary>  
        /// <param name="sourcePath">源文件路径</param>  
        /// <param name="targetPath">目标文件路径</param>   
        /// <returns>true=转换成功</returns>  
        public static bool ExcelToPDF(string sourcePath, string targetPath)  
        {  
            bool result = false;  
            Microsoft.Office.Interop.Excel.XlFixedFormatType targetType = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;  
            object missing = Type.Missing;  
            Microsoft.Office.Interop.Excel.ApplicationClass application = null;  
            Microsoft.Office.Interop.Excel.Workbook workBook = null;  
            try  
            {  
                application = new Microsoft.Office.Interop.Excel.ApplicationClass();  
                application.Visible = false;  
                workBook = application.Workbooks.Open(sourcePath);  
                workBook.SaveAs();  
                workBook.ExportAsFixedFormat(targetType, targetPath);  
                result = true;  
            }  
            catch (Exception e)  
            {  
                Console.WriteLine(e.Message);  
                result = false;  
            }  
            finally  
            {  
                if (workBook != null)  
                {  
                    workBook.Close(true, missing, missing);  
                    workBook = null;  
                }  
                if (application != null)  
                {  
<
补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,