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();
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(Form mainForm)
在 Excel.Program.Main() 位置 D:\data\asp\Offic 2007 套件应用\Excel\Program.cs:行号 18
在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
InnerException:
[img=http://m1.mail.sina.com.cn/apps/netdisk/download.php?id=da1d7578de200ea7c8973004d7b9f981&d=d][/img]
[解决办法]
Office的哪个版本?
[解决办法]
哦,2007是吧
[解决办法]
(1)需要安装Excel
(2)检查是否存在Sheet[1],Sheet是从下标0开始编号的。
[解决办法]
你试试excel2000或excel2003,很多excel源码都是以这两个为模板
[解决办法]
我是用的Offic2003,这个版本下的Sheet对象没有ExportAsFixedFormat函数。
Excel2003好像没有导出PDF的功能吧。