码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET VS2013 Office 转 PDF

时间:2015-10-23 10:06:12      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

本文适用于VS2013 项目中的Word转换为PDF、Excel转换为PDF、PPT转换为PDF

1.添加Using


1.在后台添加using

  1. using Microsoft.Office.Interop.Word;
    using Microsoft.Office.Interop.Excel;
    using Microsoft.Office.Core;
    using Microsoft.Office.Interop.PowerPoint;

     

2.添加引用


1.添加引用

技术分享

3.添加代码


1.在后台添加代码

  1. protected void Page_Load(object sender, EventArgs e)
    {
        string wordSource,wordTarget,excelSource,excelTarget,pptSource,pptTarget;
        wordSource = @"E:\FileDownload\Explorer\意向 0.3.docx";
        wordTarget = @"E:\FileDownload\Explorer\W2P.pdf";
        excelSource = @"E:\FileDownload\Explorer\xlsx.xlsx";
        excelTarget = @"E:\FileDownload\Explorer\E2P.pdf";
        pptSource = @"E:\FileDownload\Explorer\质量月活动1509.pptx";
        pptTarget = @"E:\FileDownload\Explorer\P2P.pdf";
    
    
        WordToPdf(wordSource,wordTarget);
        ExcelToPdf(excelSource,excelTarget);
        PPTConvertToPDF(pptSource, pptTarget);
    
    }
    
    public static bool WordToPdf(string sourcePath, string targetPath)
    {
        bool result = false;
        WdExportFormat wdExportFormatPDF = WdExportFormat.wdExportFormatPDF;//转换格式1.wdExportFormatPDF转换成pdf格式 2.wdExportFormatXPS转换成xps格式
        object missing = Type.Missing;
        Microsoft.Office.Interop.Word.ApplicationClass applicationClass = null;
        Document document = null;
        try
        {
            applicationClass = new Microsoft.Office.Interop.Word.ApplicationClass();
            object inputfileName = sourcePath;//需要转格式的文件路径
            string outputFileName = targetPath;//转换完成后PDF或XPS文件的路径和文件名名称
            WdExportFormat exportFormat = wdExportFormatPDF;//导出文件所使用的格式
            bool openAfterExport = false;//转换完成后是否打开
            WdExportOptimizeFor wdExportOptimizeForPrint = WdExportOptimizeFor.wdExportOptimizeForPrint;//导出方式1.wdExportOptimizeForPrint针对打印进行导出,质量较高,生成的文件大小较大。2.wdExportOptimizeForOnScreen 针对屏幕显示进行导出,质量较差,生成的文件大小较小。
            WdExportRange wdExportAllDocument = WdExportRange.wdExportAllDocument;//导出全部内容(枚举)
            int from = 0;//起始页码
            int to = 0;//结束页码
            WdExportItem wdExportDocumentContent = WdExportItem.wdExportDocumentContent;//指定导出过程中是否只包含文本或包含文本的标记.1.wdExportDocumentContent:导出文件没有标记,2.导出文件有标记
            bool includeDocProps = true;//指定是否包含新导出的文件在文档属性
            bool keepIRM = true;//
            WdExportCreateBookmarks wdExportCreateWordBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;//1.wdExportCreateNoBookmarks:不要在导出文件中创建书签,2.wdExportCreateHeadingBookmarks:标题和文本框导出的文件中创建一个书签,3.wdExportCreateWordBookmarks每个字的书签,其中包括除包含页眉和页脚中的所有书签导出的文件中创建一个书签。
            bool docStructureTags = true;
            bool bitmapMissingFonts = true;
            bool UseISO19005_1 = false;//生成的文档是否符合 ISO 19005-1 (PDF/A)
            document = applicationClass.Documents.Open(ref inputfileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            if (document != null)
            {
                document.ExportAsFixedFormat(outputFileName, exportFormat, openAfterExport, wdExportOptimizeForPrint, wdExportAllDocument, from, to, wdExportDocumentContent, includeDocProps, keepIRM, wdExportCreateWordBookmarks, docStructureTags, bitmapMissingFonts, UseISO19005_1, ref missing);
            }
            result = true;
        }
        catch
        {
            result = false;
        }
        finally
        {
            if (document != null)
            {
                document.Close(ref missing, ref missing, ref missing);
                document = null;
            }
            if (applicationClass != null)
            {
                applicationClass.Quit(ref missing, ref missing, ref missing);
                applicationClass = null;
            }
        }
        return result;
    }
    
    public static bool ExcelToPdf(string sourcePath, string targetPath)
    {
        bool result = false;
        XlFixedFormatType xlTypePDF = XlFixedFormatType.xlTypePDF;//转换成pdf
        object missing = Type.Missing;
        Microsoft.Office.Interop.Excel.ApplicationClass applicationClass = null;
        Workbook workbook = null;
        try
        {
            applicationClass = new Microsoft.Office.Interop.Excel.ApplicationClass();
            string inputfileName = sourcePath;//需要转格式的文件路径
            string outputFileName = targetPath;//转换完成后PDF文件的路径和文件名名称
            XlFixedFormatType xlFixedFormatType = xlTypePDF;//导出文件所使用的格式
            XlFixedFormatQuality xlFixedFormatQuality = XlFixedFormatQuality.xlQualityStandard;//1.xlQualityStandard:质量标准,2.xlQualityMinimum;最低质量
            bool includeDocProperties = true;//如果设置为True,则忽略在发布时设置的任何打印区域。
            bool openAfterPublish = false;//发布后不打开
            workbook = applicationClass.Workbooks.Open(inputfileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
            if (workbook!=null)
            {
                workbook.ExportAsFixedFormat(xlFixedFormatType, outputFileName, xlFixedFormatQuality, includeDocProperties, openAfterPublish, missing, missing, missing, missing);
            }
            result = true;
        }
        catch
        {
            result = false;
        }
        finally
        {
            if (workbook != null)
            {
                workbook.Close(true, missing, missing);
                workbook = null;
            }
            if (applicationClass != null)
            {
                applicationClass.Quit();
                applicationClass = null;
            }
        }
        return result;
    }
    
    public static bool PPTConvertToPDF(string sourcePath, string targetPath)
    {
       bool result;
       PpSaveAsFileType ppSaveAsFileType = PpSaveAsFileType.ppSaveAsPDF;//转换成pdf
       object missing = Type.Missing;
       Microsoft.Office.Interop.PowerPoint.ApplicationClass application = null;
       Presentation persentation = null;
       try
       {
           application = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
           persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
           if (persentation!=null)
           {
               persentation.SaveAs(targetPath, ppSaveAsFileType, MsoTriState.msoTrue);
           }
           result = true;
       }
       catch
       {
           result = false;
       }
       finally
       {
           if (persentation != null)
           {
               persentation.Close();
               persentation = null;
           }
           if (application != null)
           {
               application.Quit();
               application = null;
           }
       }
       return result;
    }

     

 

2.单独的Excel2PDF(Word2PDF同理,不过PPT2PDF就要采用上面的方法)


1.在后台添加using

 

using Microsoft.Office.Interop.Excel;
2.在项目中添加引用
技术分享
3.添加如下代码
  1. public static bool ExcelToPdf(string sourcePath, string targetPath)
    {
        bool result = false;
        XlFixedFormatType xlTypePDF = XlFixedFormatType.xlTypePDF;//转换成pdf
        object missing = Type.Missing;
        Microsoft.Office.Interop.Excel.ApplicationClass applicationClass = null;
        Workbook workbook = null;
        try
        {
            applicationClass = new Microsoft.Office.Interop.Excel.ApplicationClass();
            string inputfileName = sourcePath;//需要转格式的文件路径
            string outputFileName = targetPath;//转换完成后PDF文件的路径和文件名名称
            XlFixedFormatType xlFixedFormatType = xlTypePDF;//导出文件所使用的格式
            XlFixedFormatQuality xlFixedFormatQuality = XlFixedFormatQuality.xlQualityStandard;//1.xlQualityStandard:质量标准,2.xlQualityMinimum;最低质量
            bool includeDocProperties = true;//如果设置为True,则忽略在发布时设置的任何打印区域。
            bool openAfterPublish = false;//发布后不打开
            workbook = applicationClass.Workbooks.Open(inputfileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
            if (workbook!=null)
            {
                workbook.ExportAsFixedFormat(xlFixedFormatType, outputFileName, xlFixedFormatQuality, includeDocProperties, openAfterPublish, missing, missing, missing, missing);
            }
            result = true;
        }
        catch
        {
            result = false;
        }
        finally
        {
            if (workbook != null)
            {
                workbook.Close(true, missing, missing);
                workbook = null;
            }
            if (applicationClass != null)
            {
                applicationClass.Quit();
                applicationClass = null;
            }
        }
        return result;
    }

     


4.如果遇到”无法嵌入互操作类型“……”,请改用适用的接口“
选中项目中引入的dll,鼠标右键,选择属性,把“嵌入互操作类型”设置为False。
 

 

 





ASP.NET VS2013 Office 转 PDF

标签:

原文地址:http://www.cnblogs.com/moonache/p/4903503.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!