using Word = Microsoft.Office.Interop.Word; //12.0 word 2007 using Excel = Microsoft.Office.Interop.Excel;//12.0 excel 2007 /// <summary> /// EXCEL文檔轉成PDF文檔 /// 參考 http://msdn.microsoft.com/en-us/library/bb256835%28v=office.12%29.aspx /// Open XML SDK 2.0 for Microsoft Office http://www.microsoft.com/en-us/download/details.aspx?id=5124 /// 塗聚文 20120907 /// </summary> /// <param name="infile"></param> /// <param name="outfile"></param> 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(); } /// <summary> /// WORD文檔轉成PDF文檔 /// 參考 http://msdn.microsoft.com/en-us/library/bb256835%28v=office.12%29.aspx /// /// 塗聚文 20120907 /// </summary> /// <param name="infile"></param> /// <param name="outfile"></param> 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(); }