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

Excel 文件转换成 PDF 文件
书上说下面的代码可以把 Excel 文件转换成 PDF 文件  
可是,
  为什么,我转换失败了呢
后来,我网上下载了一些代码,还是不行,都是 这里出错啊!!!


//先引入:Microsoft.Office.Interop 
//再 using Microsoft.Office.Interop.Excel; 


ApplicationClass cExcel = new ApplicationClass();
cExcel.Visible = true;
object missing = Type.Missing;
//excel 文件名:
string excelFileName = System.Windows.Forms.Application.StartupPath + @"\客户.xls";
//pdf 文件名:
string PDFFileName = System.Windows.Forms.Application.StartupPath + @"\客户.pdf";
Workbook book = cExcel.Workbooks.Open(excelFileName, missing, missing, missing, missing
, missing, missing, missing, missing, missing, missing
, missing, missing, missing, missing);
Worksheet sheet = (Worksheet)book.Worksheets[1];
//参数1
var formatType = XlFixedFormatType.xlTypePDF;
//参数3
var quarlity = XlFixedFormatQuality.xlQualityStandard;
sheet.ExportAsFixedFormat(formatType,PDFFileName,quarlity,true,false,missing,missing,true,missing);
cExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(cExcel);
cExcel = null;
GC.Collect();


下面是异常info
未处理 System.ArgumentException
  Message="值不在预期的范围内。"
  Source="Microsoft.Office.Interop.Excel"
  StackTrace:
  在 Microsoft.Office.Interop.Excel._Worksheet.ExportAsFixedFormat(XlFixedFormatType Type, Object Filename, Object Quality, Object IncludeDocProperties, Object IgnorePrintAreas, Object From, Object To, Object OpenAfterPublish, Object FixedFormatExtClassPtr)
  在 Excel.winForm1.btnExcelToPDF_Click(Object sender, EventArgs e) 位置 D:\data\asp\Offic 2007 套件应用\Excel\winForm1.cs:行号 351
  在 System.Windows.Forms.Control.OnClick(EventArgs e)
  在 System.Windows.Forms.Button.OnClick(EventArgs e)
  在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
  在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
  在 System.Windows.Forms.Control.WndProc(Message& m)
  在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
  在 System.Windows.Forms.Button.WndProc(Message& m)
  在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)