c# 操作excel生成pdf

来源:互联网 发布:知乎 怪异故事和acfun 编辑:程序博客网 时间:2024/05/22 11:57
 //将excel文档转换成PDF格式
          private bool Convert2PDF(string sourcePath, string targetPath, XlFixedFormatType targetType)
        {
              bool result;
            object missing = Type.Missing;
            Excel.ApplicationClass application = null;          
              Workbook workBook = null;
            try
             {
                 application = new 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;

         }



调用

  XlFixedFormatType parameter = XlFixedFormatType.xlTypePDF;
                
                  Convert2PDF(@"D://1.xls", @"D://1.pdf",parameter);



附大神原地址:

http://www.cnblogs.com/bayes/articles/DocToPdfCode.html

0 0
原创粉丝点击