Beispiel #1
0
        /// <summary>
        /// 把Excel文件转换成PDF格式文件
        /// </summary>
        /// <param name="sourcePath">源文件路径</param>
        /// <param name="targetPath">目标文件路径</param>
        /// <returns>true=转换成功</returns>
        private Boolean XLSConvertToPDF(String sourcePath, String targetPath)
        {
            Boolean result = false;

            OExcel.XlFixedFormatType targetType = OExcel.XlFixedFormatType.xlTypePDF;
            object missing = Type.Missing;

            OExcel.ApplicationClass application = null;
            OExcel.Workbook         workBook    = null;
            try
            {
                application = new OExcel.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);
                if (workBook.IsNullOrEmpty())
                {
                    throw new System.ArgumentException("未能实例化");
                }
                workBook.ExportAsFixedFormat(targetType, target, OExcel.XlFixedFormatQuality.xlQualityMinimum, true, true, missing, missing, false, missing);
                result = true;
            }
            catch (Exception)
            {
                result = false;
                throw;
            }
            finally
            {
                if (workBook != null)
                {
                    workBook.Close(true, missing, missing);
                    workBook = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                if (application != null)
                {
                    application.Quit();
                    application = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return(result);
        }