Why am I getting an exception when I try to set the value of a cell in Excel using Perl's Win32::OLE?
I am getting the error Win32::OLE<0.1709> error 0x80020009: "Exception occurred" in PROPERTYPUT "Value"
at line 109.
The code in is Perl.
foreach my $ref_array1 (@$array1) { # loop thr开发者_运维问答ough the array
foreach my $col1 (@$ref_array1) {
foreach my $ref_array2 (@$array2) { # loop through the array
foreach my $col2 (@$ref_array2) {
if ($col1 eq $col2)
{
this is line 109: **$worksheet1->Cells($j,1)->{'Value'} = $col1;**
$j++;
Any kind of help is appreciated. Thankyou
The following incomplete example works (i.e., it puts 5 in cell A1
):
#!/usr/bin/perl
use strict; use warnings;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';
$Win32::OLE::Warn = 3;
my $excel = get_excel();
$excel->{Visible} = 1;
my $book = $excel->Workbooks->Add;
my $sheet = $book->Worksheets->Add;
$sheet->{Name} = 'Perl Win32-OLE Example';
my $range = $sheet->Cells(1,1);
$range->{Value} = 5;
$range->AutoFormat;
sub get_excel {
my $excel;
unless ( eval {
$excel = Win32::OLE->GetActiveObject('Excel.Application')
}) {
die $@, "\n";
}
unless(defined $excel) {
$excel = Win32::OLE->new('Excel.Application', sub { $_[0]->Quit })
or die "Oops, cannot start Excel: ",
Win32::OLE->LastError, "\n";
}
return $excel;
}
See also my Perl Win32::OLE example.
精彩评论