博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
csharp: word or excel Convert to PDF
阅读量:7170 次
发布时间:2019-06-29

本文共 4255 字,大约阅读时间需要 14 分钟。

1 using Word = Microsoft.Office.Interop.Word; //12.0 word 2007 2 using Excel = Microsoft.Office.Interop.Excel;//12.0 excel 2007 3  4  5 ///  6         /// EXCEL文檔轉成PDF文檔 7         ///  參考 http://msdn.microsoft.com/en-us/library/bb256835%28v=office.12%29.aspx 8         ///  Open XML SDK 2.0 for Microsoft Office http://www.microsoft.com/en-us/download/details.aspx?id=5124 9         ///  塗聚文 2012090710         /// 11         /// 12         /// 13        public  void ExportExcel2PDF(string infile, string outfile)14         {15             object objOpt = Missing.Value;16 17             Excel.Application excelApp = null;18             try19             {20                 excelApp = new Excel.Application();21                 excelApp.Workbooks.Open(infile, objOpt, objOpt, objOpt, objOpt, objOpt, true, objOpt, objOpt, true, objOpt, objOpt, objOpt, objOpt, objOpt);22                 excelApp.ActiveWorkbook.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, (object)outfile, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt);23             }24             catch (Exception ex)25             {26                 throw ex;27             }28             finally29             {30                 if (excelApp != null)31                     excelApp.Quit();32             }33             GC.Collect();34             GC.WaitForPendingFinalizers();35             GC.Collect();36             GC.WaitForPendingFinalizers();37         }38         /// 39         /// WORD文檔轉成PDF文檔40         /// 參考 http://msdn.microsoft.com/en-us/library/bb256835%28v=office.12%29.aspx        41         /// 42         ///  塗聚文 2012090743         /// 44         /// 45         /// 46         public void ExportWord2PDF(string infile, string outfile)47         {48             object objOpt = Missing.Value;49             object readOnly = true;50             object missing=Missing.Value;51             object file=(object)infile;52             object SavePDFFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;53 54             Word.WdExportFormat paramExportFormat = Word.WdExportFormat.wdExportFormatPDF;55             bool paramOpenAfterExport = false;56             Word.WdExportOptimizeFor paramExportOptimizeFor =57                 Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForPrint;58             Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;59             int paramStartPage = 0;60             int paramEndPage = 0;61             Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;62             bool paramIncludeDocProps = true;63             bool paramKeepIRM = true;64             Word.WdExportCreateBookmarks paramCreateBookmarks =65                 Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;66             bool paramDocStructureTags = true;67             bool paramBitmapMissingFonts = true;68             bool paramUseISO19005_1 = false;69 70             Word.Application wordApp = null;71             try72             {73                 wordApp = new Word.Application();74                 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);75                 wordApp.ActiveDocument.ExportAsFixedFormat(outfile, paramExportFormat, paramOpenAfterExport, paramExportOptimizeFor, paramExportRange, paramStartPage,76                             paramEndPage, paramExportItem, paramIncludeDocProps,77                             paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,78                             paramBitmapMissingFonts, paramUseISO19005_1,79                             ref missing);80             }81             catch (Exception ex)82             {83                 throw ex;84             }85             finally86             {87                 if (wordApp != null)88                     wordApp.Quit(ref missing, ref missing, ref missing);89                     90             }91             GC.Collect();92             GC.WaitForPendingFinalizers();93             GC.Collect();94             GC.WaitForPendingFinalizers();95         }

 

转载地址:http://hhmwm.baihongyu.com/

你可能感兴趣的文章