将execl转换成pdf文件

上次将EXECL导出到指定模板中后,需求又出更改,哎....废话不多说。。

使用的wind的 AdobeReader  ,因此需要添加引用

 1    /// <summary>
 2         /// 把Excel文件转换成PDF格式文件
 3         /// </summary>
 4         /// <param name="sourcePath">源文件路径</param>
 5         /// <param name="targetPath">目标文件路径</param>
 6         /// <returns>true=转换成功</returns>
 7         public bool XLSConvertToPDF(string sourcePath, string targetPath)
 8         {
 9             bool result = false;
10             XlFixedFormatType targetType = XlFixedFormatType.xlTypePDF;
11             object missing = Type.Missing;
12             Microsoft.Office.Interop.Excel.Application application = null;
13             Workbook workBook = null;
14             try
15             {
16                 application = new Microsoft.Office.Interop.Excel.Application();
17                 object target = targetPath;
18                 object type = targetType;
19                 workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
20                         missing, missing, missing, missing, missing, missing, missing, missing, missing);
21 
22                 workBook.ExportAsFixedFormat(targetType, target, XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
23                 result = true;
24             }
25             catch
26             {
27                 result = false;
28             }
29             finally
30             {
31                 if (workBook != null)
32                 {
33                     workBook.Close(true, missing, missing);
34                     workBook = null;
35                 }
36                 if (application != null)
37                 {
38                     application.Quit();
39                     application = null;
40                 }
41                 GC.Collect();
42                 GC.WaitForPendingFinalizers();
43                 GC.Collect();
44                 GC.WaitForPendingFinalizers();
45             }
46             return result;
47         }
原文地址:https://www.cnblogs.com/li-lun/p/5040098.html