我正在使用PERL脚本通过OLE在现有excel文件上创建工作簿。 我想用这条指令添加一个新的工作簿 -
$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";
关于我做错了什么的指示?
免责声明:我这是一个完全的菜鸟:P
答案 0 :(得分:0)
这是一个很好的教程:http://www.perlmonks.org/?node_id=153486
您可以尝试使用以下方法进行调试:
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;
}
答案 1 :(得分:0)
也许你错过了引号?
$excelfile='E:\script\workbook.xlsx';