Excel转PDF编程中ExportAsFixedFormat问题

attidust attidust 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
在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("<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();
}


}
}

...全文

相关推荐

评论 6
收藏 已收藏
分享
返回顶部
attidust
.NET社区 62,275 成员 668,968 内容