Excel转PDF编程中ExportAsFixedFormat问题
在MSDN上看Excel2007可以将xlsx文件转换为pdf文件,还有参考编码:
http://msdn.microsoft.com/en-us/library/bb407651(v=office.12).aspx
于是就照着这个编了一通,结果调试时候报错,ex.message:出现异常值不在预期的范围内
代码如下,很简单,就是把网站根目录下的一个.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();
}
}
}
...全文
相关推荐
Excel/Word 转PDF
Excel文档转换PDF格式,Word文档转换PDF格式,C#代码,附带源码
C#实现Excel转PDF:三大方案对比与实战代码详解
在软件开发中,文档格式转换是高频需求,尤其将Excel转为PDF常用于报表导出、存档与打印。理解其底层原理,即通过打印引擎或渲染引擎将工作簿内容重绘为PDF页面,是选型的基础。技术价值在于实现自动化、批量处理并保障格式一致,广泛应用于OA系统、数据报表平台及服务端定时任务。本文从环境准备、核心代码到格式处理与性能优化,系统梳理C#环境下实现Excel转PDF的三种主流方案:依赖本机Office的Interop、纯托管库Aspose.Cells与Spire.XLS,并结合实际踩坑经验给出选型建议,帮助开发人员
EXCEL转PDF(vba)
1.Excel所有sheet转换为单个PDF 2.Excel只是转换第一个sheet为PDF 3.Excel转换指定sheet为PDF
VBA转PDF_VBA转PDF_ExcelVBA_vbapdf_VBa_源码
VBA转PDF excel文档转化到pdf
Excel转 Pdf格式
Excel转 Pdf格式Excel转 Pdf格式Excel转 Pdf格式


