您的位置:首页 > 编程语言 > C#

c# 操作excel生成pdf

2017-01-06 14:50 519 查看
 //将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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: