无法使用PERL脚本打开工作簿

我正在使用PERL脚本通过OLE在现有的Excel文件上创build一个工作簿。 我想用这个指令添加一个新的工作簿 –

$Book = $Excel->Workbooks->Add(); 

然后用它进行一定的操作。 然而,在执行这个代码时,我遇到$ Book总是等于零的情况,而且之后excel变得没有响应。 我包括下面的代码片段:

 use File::Spec; use Win32::OLE; use File::Basename; use Cwd 'abs_path'; use Win32::OLE::Const 'Microsoft Excel'; use Win32::OLE qw(in with); use Win32::OLE::Variant; use Win32::OLE::NLS qw(:LOCALE :DATE); use constant false => 0; use constant true => 1; use constant NULL => 0; my $Excel = ""; my $Book = 0; my $Sheet_1 = ""; my $excelfile = " "; print "execl"; my $RC = false; $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit'); $Excel->{DisplayAlerts}=1; $Excel->{Visible} =1; $excelfile=E:\script\workbook.xlsx; $Book = $Excel->Workbooks->Add(); print "\n".$excelfile."\n"; if ($Book==0) {print "close any open Excel file\n If problem persist close Excel application from task manager"; #return $RC; $Excel->Close(); } $Sheet_1 = $Book->Worksheets(1); $Sheet_1->Activate(); $Sheet_1->{Name} = "testing_sheet"; 

任何指针,我在做什么错?

免责声明:我是一个总noob在这个:P

这是一个很好的教程: http : //www.perlmonks.org/? node_id= 153486

你可以尝试一下这些debugging:

 use strict; use warnings; $Win32::OLE::Warn = 3; # Die on Errors. my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit'); $Excel->{DisplayAlerts}=1; $Excel->{Visible} =1; my $excelfile=E:\script\workbook.xlsx; print "Creating new workbook\n"; my $Book = $Excel->Workbooks->Add(); print "Saving to excelfile: $excelfile\n"; $Book->SaveAs($excelfile); if (! defined $Book){ print "close any open Excel file\n If problem persist close Excel application from task manager"; $Excel->Close(); return $RC; } 

也许你错过了引号?

  $excelfile='E:\script\workbook.xlsx';