日期:2014-05-17  浏览次数:21220 次

Excel转PDF编程中ExportAsFixedFormat问题
在MSDN上看Excel2007可以将xlsx文件转换为pdf文件,还有参考编码:

http://msdn.microsoft.com/en-us/library/bb407651(v=office.12).aspx

于是就照着这个编了一通,结果调试时候报错,ex.message:出现异常值不在预期的范围内
代码如下,很简单,就是把网站根目录下的一个.xlsx文件转为pdf,源文件在,打开正常,但是保存时候出错。

期待高手看看,哪里写的不对,先谢谢拉!

C# code

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using Microsoft.Office.Interop.Excel;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //Creating an Instance of the ApplicationClass Object
        Microsoft.Office.Interop.Excel.ApplicationClass excelApplication = new Microsoft.Office.Interop.Excel.ApplicationClass();
        Microsoft.Office.Interop.Excel.Workbook excelWorkBook = null;

        //Declaring the Appropriate Variables for open
        string paramSourceBookPath = Server.MapPath("MBAP.xlsx");
        object paramMissing = Type.Missing;

        //Declaring the Appropriate Variables for ExportAsFixedFormat
        string paramExportFilePath = Server.MapPath("test.pdf") ;
        Microsoft.Office.Interop.Excel.XlFixedFormatType paramExportFormat = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;
        Microsoft.Office.Interop.Excel.XlFixedFormatQuality paramExportQuality = Microsoft.Office.Interop.Excel.XlFixedFormatQuality.xlQualityStandard;
        bool paramOpenAfterPublish = false;
        bool paramIncludeDocProps = true;
        bool paramIgnorePrintAreas = true;
        object paramFromPage = Type.Missing;
        object paramToPage = Type.Missing;

        try
        {
            // Open the source workbook.
            excelWorkBook = excelApplication.Workbooks.Open(paramSourceBookPath,
                paramMissing, paramMissing, paramMissing, paramMissing,
                paramMissing, paramMissing, paramMissing, paramMissing,
                paramMissing, paramMissing, paramMissing, paramMissing,
                paramMissing, paramMissing);
            
            //set alert to monitor program
            Response.Write("<script>alert('完成打开xlsx')</script>");

            // Save it in the target format.
            if (excelWorkBook != null)
                excelWorkBook.ExportAsFixedFormat(paramExportFormat,
                    paramExportFilePath, paramExportQuality,
                    paramIncludeDocProps, paramIgnorePrintAreas, paramFromPage,
                    paramToPage, paramOpenAfterPublish,
                    paramMissing);
            
            //set alert to monitor program
            Response.Write("<script>alert('已经保存Pdf')</script>");

        }
        catch (Exception ex)
        {
            // Respond to the error.
            Response.Write("<script>alert('运行出错"+ex.Message+"')</script>");

        }
        finally
        {
            // Close the workbook object.
            if (excelWorkBook != null)
            {
                excelWorkBook.Close(false, paramMissing, paramMissing);
                excelWorkBook = null;
            }

            // Quit Excel and release the ApplicationClass object.
            if (excelApplication != null)
            {
                excelApplication.Quit();
                excelApplication = null;
            }

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }  


    }
}