private void x() { Excel.Application app = new ApplicationClass(); try { //app.ActivePrinter = "Acrobat Distiller"; Workbooks workBooks = app.Workbooks; Workbook workBook = (Workbook )workBooks.GetType().InvokeMember("Open", BindingFlags.InvokeMethod, null, workBooks, new object[] { @"C:\Documents and Settings\raghunandanr\Desktop\Template Format for PDF Transcription1.xls" }); workBook.PrintOut(1, Type.Missing, 1, false, "Acrobat Distiller", true, true, @"C:\Documents and Settings\raghunandanr\Desktop\Template Format for PDF Transcription1.ps"); workBook.Close(false, Type.Missing, Type.Missing); PdfDistiller dist = new PdfDistillerClass(); //dist.bSpoolJobs = 1; dist.bShowWindow = 0; dist.OnJobFail += new _PdfEvents_OnJobFailEventHandler(Fail); dist.FileToPDF(@"C:\Documents and Settings\raghunandanr\Desktop\Template Format for PDF Transcription1.ps", null, null); Marshal.ReleaseComObject(dist); dist = null; } catch (Exception ex) { Response.Write(ex.Message); } finally { app.Quit(); Marshal.ReleaseComObject(app); app = null; GC.Collect(); GC.WaitForPendingFinalizers(); } }
public static String ConvertToPDF(String filePath, String destPath) { if (!filePath.IsNormalized() || !File.Exists(filePath)) { throw new Exception("未指定文件"); } if (!filePath.EndsWith("xls") && !filePath.EndsWith("xlsx")) { throw new Exception("文件非 Excel 类型"); } ApplicationClass eapp = new ApplicationClass(); Type eType = eapp.GetType(); Workbooks Ewb = eapp.Workbooks; Type elType = Ewb.GetType(); Workbook ebook = (Workbook)elType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, Ewb, new Object[] { filePath, true, true }); Object oMissing = System.Reflection.Missing.Value; ebook.PrintOut(oMissing, oMissing, oMissing, oMissing, oMissing, true, oMissing, destPath); ebook.Close(false); eType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, eapp, null); return(destPath); }
//打印整个workbook /// <summary> /// 打印整个workbook,使用默认pdf打印机转换为pdf /// </summary> /// <param name="fromExcelPath">源excel路径</param> /// <returns>成功或失败</returns> public bool ConvertExcelWorkbookPDF(string fromExcelPath) { bool flag = true; try { if (fromExcelPath.Length == 0) { flag = false; throw new Exception("需要转换的源文件路径不能为空。"); } Microsoft.Office.Interop.Excel.ApplicationClass applicationClass = new Microsoft.Office.Interop.Excel.ApplicationClass(); applicationClass.GetType(); Workbooks workbooks = applicationClass.Workbooks;//.get_Workbooks(); Type type = workbooks.GetType(); object obj = fromExcelPath; object[] objArray = new object[] { obj, true, true }; Microsoft.Office.Interop.Excel.Workbook workbook = (Microsoft.Office.Interop.Excel.Workbook)type.InvokeMember("Open", BindingFlags.InvokeMethod, null, workbooks, objArray); workbook.GetType(); object value = Missing.Value; //目标路径仅在打印失败时写入,成功时都默认在打印机路径下 //故不使用目标路径,直接使用打印机默认路径 workbook.PrintOutEx(value, value, value, value, value, false, value, value, value); //item.PrintOut(value, value, value, value, value, false, value, value); if (workbook != null) { workbook.Close(false, Type.Missing, Type.Missing); System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook); //Marshal.FinalReleaseComObject(workBook); workbook = null; } if (workbooks != null) { workbooks.Close(); System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks); workbooks = null; } if (applicationClass != null) { applicationClass.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(applicationClass); applicationClass = null; } GC.Collect(); GC.WaitForPendingFinalizers(); return(flag); } catch (Exception exception) { classLims_NPOI.WriteLog(exception, ""); throw exception; } finally { } }
/// <summary> /// 按索引查找sheet,使用默认pdf打印机转换为pdf /// </summary> /// <param name="fromExcelPath">源excel路径</param> /// <param name="worksheetIndex">sheet索引,注意从1开始</param> /// <returns>成功或失败</returns> public bool ConvertExcelWorkSheetPDF_index(string fromExcelPath, int worksheetIndex) { bool flag = true; try { if (fromExcelPath.Length == 0) { flag = false; throw new Exception("需要转换的源文件路径不能为空。"); } Microsoft.Office.Interop.Excel.ApplicationClass applicationClass = new Microsoft.Office.Interop.Excel.ApplicationClass(); applicationClass.GetType(); Workbooks workbooks = applicationClass.Workbooks;//.get_Workbooks(); Type type = workbooks.GetType(); object obj = fromExcelPath; object[] objArray = new object[] { obj, true, true }; Microsoft.Office.Interop.Excel.Workbook workbook = (Microsoft.Office.Interop.Excel.Workbook)type.InvokeMember("Open", BindingFlags.InvokeMethod, null, workbooks, objArray); //workbooks.Open(this._sourcePath); //Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Item[0]; workbook.GetType(); //object obj1 = "c:\\temp.ps"; //obj1 = toPdfPath; object obj2 = "-4142"; //Microsoft.Office.Interop.Excel.Worksheet item = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.Item[worksheetIndex]; Microsoft.Office.Interop.Excel.Worksheet item = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(worksheetIndex); if (item == null) { flag = false; throw new Exception("需要转换的WorkSheet名称不存在。"); } //item.get_Cells().get_Interior().set_ColorIndex(obj2); item.Cells.Interior.ColorIndex = obj2;//.get_Interior().set_ColorIndex(obj2); object value = Missing.Value; //item.PrintOut(value, value, value, value, value, true, value, obj1); //Microsoft.Office.Interop.Excel.XlFixedFormatType targetType = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF; //item.ExportAsFixedFormat(targetType, obj1, Microsoft.Office.Interop.Excel.XlFixedFormatQuality.xlQualityStandard,true,false, value, value, value, value); //目标路径仅在打印失败时写入,成功时都默认在打印机路径下 //故不使用目标路径,直接使用打印机默认路径 item.PrintOut(value, value, value, value, value, false, value, value); if (item != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(item); //Marshal.FinalReleaseComObject(sheet); item = null; } if (workbook != null) { workbook.Close(false, Type.Missing, Type.Missing); System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook); //Marshal.FinalReleaseComObject(workBook); workbook = null; } if (workbooks != null) { workbooks.Close(); System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks); workbooks = null; } if (applicationClass != null) { applicationClass.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(applicationClass); applicationClass = null; } GC.Collect(); GC.WaitForPendingFinalizers(); return(flag); } catch (Exception exception) { classLims_NPOI.WriteLog(exception, ""); throw exception; } finally { } }