您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> .NET >> Excel转PDF编程中ExportAsFixedFormat问题

Excel转PDF编程中ExportAsFixedFormat问题

来源:网络整理     时间:2016/6/21 4:52:46     关键词:

关于网友提出的“ Excel转PDF编程中ExportAsFixedFormat问题”问题疑问,本网通过在网上对“ Excel转PDF编程中ExportAsFixedFormat问题”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题: Excel转PDF编程中ExportAsFixedFormat问题
描述:

在MSDN上看Excel2007可以将xlsx文件转换为pdf文件,还有参考编码:
http://msdn.microsoft.com/en-us/library/bb407651(v=office.12).aspx
于是就照着这个编了一通,结果调试时候报错,ex.message:出现异常值不在预期的范围内
代码如下,很简单,就是把网站根目录下的一个.xlsx文件转为pdf,源文件在,打开正常,但是保存时候出错。
期待高手看看,哪里写的不对,先谢谢拉!


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("");
            // 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("");
        }
        catch (Exception ex)
        {
            // Respond to the error.
            Response.Write("");
        }
        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();
        }  
    }
}

解决方案1:

Microsoft.Office.Interop.Excel.XlFixedFormatType paramExportFormat = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;
        Microsoft.Office.Interop.Excel.XlFixedFormatQuality paramExportQuality = Microsoft.Office.Interop.Excel.XlFixedFormatQuality.xlQualityStandard;
应该是enum类型
enum Microsoft.Office.Interop.Excel.XlFixedFormatType paramExportFormat = enum Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;
enum Microsoft.Office.Interop.Excel.XlFixedFormatQuality paramExportQuality = enum Microsoft.Office.Interop.Excel.XlFixedFormatQuality.xlQualityStandard;


以上介绍了“ Excel转PDF编程中ExportAsFixedFormat问题”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/1861425.html

相关图片

相关文章