using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Office.Interop.Excel; using Microsoft.Office.Interop.Word; using Microsoft.Office.Interop.PowerPoint; namespace BatchService.Framework.Utility { public class OfficeHelper { /// /// Excel转PDF /// /// excel路径 /// 目标路径名PDF public static void ExcelToPDF(string sourcePath, string savePath) { try { Microsoft.Office.Interop.Excel.Application office = new Microsoft.Office.Interop.Excel.Application(); office.DisplayAlerts = true; Microsoft.Office.Interop.Excel.Workbook workbook = office.Application.Workbooks.Open(sourcePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); Microsoft.Office.Interop.Excel.Worksheet workheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1]; workbook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, savePath, Type.Missing); workbook.Close(false, Type.Missing, Type.Missing); office.Quit(); } catch (Exception ex) { LogHelper.Debug("转化pdf"+ex.Message); //office.Quit(); } finally { //office.Quit(); } } /// /// Word转PDF /// /// word路径 /// PDF路径 ////public static void WordToPDF(string sourcePath, string savePath) ////{ //// try //// { //// Microsoft.Office.Interop.Word.Application office = new Microsoft.Office.Interop.Word.Application(); //// office.DisplayAlerts = WdAlertLevel.wdAlertsNone; //// object paramMissing = Type.Missing; //// object filePath = sourcePath; //// object readOnly = true; //// object addToRecentFiles = false; //// object confirmConversion = false; //// object revert = true; //// object visible = false; //// object formate = WdSaveFormat.wdFormatFilteredHTML; //// object missing = System.Reflection.Missing.Value; //// object savePathObj = savePath; //// object saveChanges = false; //// object encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8; //// Document doc = office.Documents.Open(ref filePath, ref confirmConversion, ref readOnly, ref addToRecentFiles, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); //// //另存为网页格式 //// doc.SaveEncoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8; //// doc.ExportAsFixedFormat(savePath, WdExportFormat.wdExportFormatPDF); //// doc.Close(ref saveChanges, ref paramMissing, ref paramMissing); //// //doc.SaveAs(ref savePathObj, ref formate, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref encoding, ref missing, ref missing, ref missing, ref missing); //// //office.Quit(ref saveChanges, ref missing, ref missing); //// } //// catch (Exception ex) //// { //// LogHelper.Debug(ex.Message); //// //office.Quit(ref saveChanges, ref paramMissing, ref paramMissing); //// } //// finally //// { //// //office.Quit(); //// } ////} /// /// PPT转pdf /// /// /// //public static void PPtToPDF(string sourcePath, string savePath) //{ // try // { // Microsoft.Office.Interop.PowerPoint.Application office = new Microsoft.Office.Interop.PowerPoint.Application(); // //office.Visible = Microsoft.Office.Core.MsoTriState.msoTrue; // //office.WindowState = PpWindowState.ppWindowMinimized; // office.DisplayAlerts = PpAlertLevel.ppAlertsNone; // office.DisplayDocumentInformationPanel = false; // Presentation presentation = office.Presentations.Open(sourcePath); // presentation.ExportAsFixedFormat(savePath, PpFixedFormatType.ppFixedFormatTypePDF); // presentation.Close(); // } // catch (Exception ex) // { // LogHelper.Debug(ex.Message); // //office.Quit(); // } // finally // { // //office.Quit(); // } //} } }