asp.net将Excel文档转换成pdf

asp.net将Excel文档转换成pdf

一、添加引用

using Microsoft.Office.Interop.Excel;

二、转换方法

 

1、方法

  •  
  • C# 代码   复制
  • 
        /// <summary>
        /// 把Excel文件转换成pdf文件
        /// </summary>
        /// <param name="sourcePath">需要转换的文件路径和文件名称</param>
        /// <param name="targetPath">转换完成后的文件的路径和文件名名称</param>
        /// <returns></returns>
        public static bool ExcelToPdf(string sourcePath, string targetPath)
        {
            bool result = false;
            XlFixedFormatType xlTypePDF = XlFixedFormatType.xlTypePDF;//转换成pdf
            object missing = Type.Missing;
            Microsoft.Office.Interop.Excel.ApplicationClass applicationClass = null;
            Workbook workbook = null;
            try
            {
                applicationClass = new Microsoft.Office.Interop.Excel.ApplicationClass();
                string inputfileName = sourcePath;//需要转格式的文件路径
                string outputFileName = targetPath;//转换完成后PDF文件的路径和文件名名称
                XlFixedFormatType xlFixedFormatType = xlTypePDF;//导出文件所使用的格式
                XlFixedFormatQuality xlFixedFormatQuality = XlFixedFormatQuality.xlQualityStandard;//1.xlQualityStandard:质量标准,2.xlQualityMinimum;最低质量
                bool includeDocProperties = true;//如果设置为True,则忽略在发布时设置的任何打印区域。
                bool openAfterPublish = false;//发布后不打开
                workbook = applicationClass.Workbooks.Open(inputfileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
                if (workbook!=null)
                {
                    workbook.ExportAsFixedFormat(xlFixedFormatType, outputFileName, xlFixedFormatQuality, includeDocProperties, openAfterPublish, missing, missing, missing, missing);
                }
                result = true;
            }
            catch
            {
                result = false;
            }
            finally
            {
                if (workbook != null)
                {
                    workbook.Close(true, missing, missing);
                    workbook = null;
                }
                if (applicationClass != null)
                {
                    applicationClass.Quit();
                    applicationClass = null;
                }
            }
            return result;
        }
    
    		
  • 2、简洁方法

  •  
  • C# 代码   复制
  • 
    /// <summary>
        /// 把Excel文件转换成pdf文件
        /// </summary>
        /// <param name="sourcePath">需要转换的文件路径和文件名称</param>
        /// <param name="targetPath">转换完成后的文件的路径和文件名名称</param>
        /// <returns></returns>
        public static bool ExcelToPdf(string sourcePath, string targetPath)
        {
            bool result = false;
            XlFixedFormatType xlTypePDF = XlFixedFormatType.xlTypePDF;//转换成pdf
            object missing = Type.Missing;
            Microsoft.Office.Interop.Excel.ApplicationClass applicationClass = null;
            Workbook workbook = null;
            try
            {
                applicationClass = new Microsoft.Office.Interop.Excel.ApplicationClass();
                workbook = applicationClass.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
                if (workbook != null)
                {
                    workbook.ExportAsFixedFormat(xlTypePDF, targetPath, 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 (applicationClass != null)
                {
                    applicationClass.Quit();
                    applicationClass = null;
                }
            }
            return result;
        }
    
    		
  • 三、调用

    OfficeToPdf.ExcelToPdf("d:\\1234.xls", "d:\\1234.pdf");

    标签: