加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

csharp: word or excel Convert to PDF

(2012-09-07 15:47:30)
标签:

excel

word

pdf

it

分类: 技术共享
using Word = Microsoft.Office.Interop.Word; //12.0 word 2007
using Excel = Microsoft.Office.Interop.Excel;//12.0 exel 2007


///
        /// EXCEL文檔轉成PDF文檔
        ///  參考 http://msdn.microsoft.com/en-us/library/bb256835(v=office.12).aspx
        ///  Open XML SDK 2.0 for Microsoft Office http://www.microsoft.com/en-us/download/details.aspx?id=5124
        ///  塗聚文 20120907
        ///
        ///
        ///
       public  void ExportExcel2PDF(string infile, string outfile)
        {
            object objOpt = Missing.Value;

            Excel.Application excelApp = null;
            try
            {
                excelApp = new Excel.Application();
                excelApp.Workbooks.Open(infile, objOpt, objOpt, objOpt, objOpt, objOpt, true, objOpt, objOpt, true, objOpt, objOpt, objOpt, objOpt, objOpt);
                excelApp.ActiveWorkbook.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, (object)outfile, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (excelApp != null)
                    excelApp.Quit();
            }
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        ///
        /// WORD文檔轉成PDF文檔
        /// 參考 http://msdn.microsoft.com/en-us/library/bb256835(v=office.12).aspx        
        ///
        ///  塗聚文 20120907
        ///
        ///
        ///
        public void ExportWord2PDF(string infile, string outfile)
        {
            object objOpt = Missing.Value;
            object readOnly = true;
            object missing=Missing.Value;
            object file=(object)infile;
            object SavePDFFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;

            Word.WdExportFormat paramExportFormat = Word.WdExportFormat.wdExportFormatPDF;
            bool paramOpenAfterExport = false;
            Word.WdExportOptimizeFor paramExportOptimizeFor =
                Microsoft.Office.Interop.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;

            Word.Application wordApp = null;
            try
            {
                wordApp = new Word.Application();
                wordApp.Documents.Open(ref file, ref missing, ref readOnly, 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);
                wordApp.ActiveDocument.ExportAsFixedFormat(outfile, paramExportFormat, paramOpenAfterExport, paramExportOptimizeFor, paramExportRange, paramStartPage,
                            paramEndPage, paramExportItem, paramIncludeDocProps,
                            paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
                            paramBitmapMissingFonts, paramUseISO19005_1,
                            ref missing);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (wordApp != null)
                    wordApp.Quit(ref missing, ref missing, ref missing);
                    
            }
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有