/// <summary> /// 把Excel文件转换成PDF格式文件 /// </summary> /// <param name="sourcePath">源文件路径</param> /// <param name="targetPath">目标文件路径</param> /// <returns>true:转换成功;false:转换失败</returns> public static bool XLSConvertToPDF(string sourcePath, string targetPath) { bool result = false; XlFixedFormatType targetType = XlFixedFormatType.xlTypePDF; object missing = Type.Missing; Microsoft.Office.Interop.Excel.ApplicationClass application = null; Workbook workBook = null; try { application = new Microsoft.Office.Interop.Excel.ApplicationClass(); object target = targetPath; object type = targetType; workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); workBook.ExportAsFixedFormat(targetType, target, XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing); result = true; } catch { result = false; } finally { if (workBook != null) { workBook.Close(true, missing, missing); workBook = null; } if (application != null) { application.Quit(); application = null; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } return(result); }
/// <summary> /// Excel转XPS /// </summary> /// <param name="filename">Excel文件位置</param> /// <returns>xps文件位置</returns> private string ConvertExcelToXps(string filename) { var app = new Microsoft.Office.Interop.Excel.ApplicationClass { Visible = false, DisplayAlerts = false }; try { Workbook wk = app.Workbooks.Open(filename); try { //app.ActivePrinter = GetXpsPrinter()["name"].ToString(); string xpsFile = filename.Substring(0, filename.LastIndexOf('.')) + ".xps"; wk.ExportAsFixedFormat(XlFixedFormatType.xlTypeXPS, xpsFile); wk.Close(); wk = null; app.Quit(); return(GetFileName(xpsFile)); } finally { if (wk != null) { wk.Close(); wk = null; } } } catch (Exception) { } finally { app = null; GC.Collect(); GC.WaitForPendingFinalizers(); ClearProcesses("EXCEL"); } return(""); }
/// <summary> /// Excel格式转换(Excel文件不能为空,默认转成PDF) /// </summary> /// <param name="sourcePath">源路径</param> /// <param name="targetPath">目标路径</param> /// <param name="targetFileType">转换类型</param> /// <returns></returns> public static bool ExcelConvert(string sourcePath, string targetPath, XlFixedFormatType targetFileType = XlFixedFormatType.xlTypePDF) { Microsoft.Office.Interop.Excel.ApplicationClass excelApplication = null; Workbook excelWorkBook = null; try { excelApplication = new Microsoft.Office.Interop.Excel.ApplicationClass(); excelWorkBook = excelApplication.Workbooks.Open(sourcePath); excelWorkBook.ExportAsFixedFormat(targetFileType, targetPath, XlFixedFormatQuality.xlQualityStandard); } catch (Exception e) { Console.WriteLine(e.Message); throw; } finally { if (excelWorkBook != null) { excelWorkBook.Close(); } if (excelApplication != null) { int k; var t = new IntPtr(excelApplication.Hwnd); GetWindowThreadProcessId(t, out k); var p = System.Diagnostics.Process.GetProcessById(k); excelApplication.Quit(); p.Kill(); } GC.Collect(); GC.WaitForPendingFinalizers(); } return(true); }
/// <summary> /// 把Excel文件转换成PDF格式文件 /// </summary> /// <param name="sourcePath">源文件路径</param> /// <param name="targetPath">目标文件路径</param> /// <returns>true:转换成功;false:转换失败</returns> public static bool XLSConvertToPDF(string sourcePath, string targetPath) { bool result = false; XlFixedFormatType targetType = XlFixedFormatType.xlTypePDF; object missing = Type.Missing; Microsoft.Office.Interop.Excel.ApplicationClass application = null; Workbook workBook = null; try { application = new Microsoft.Office.Interop.Excel.ApplicationClass(); object target = targetPath; object type = targetType; workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); workBook.ExportAsFixedFormat(targetType, target, XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing); result = true; } catch { result = false; } finally { if (workBook != null) { workBook.Close(true, missing, missing); workBook = null; } if (application != null) { application.Quit(); application = null; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } return result; }