当我尝试使用Perl的Win32 :: OLE在Excel中设置单元格的值时,为什么会出现异常?

时间:2010-07-26 20:39:38

标签: perl excel win32ole

我在第109行收到错误Win32::OLE<0.1709> error 0x80020009: "Exception occurred" in PROPERTYPUT "Value"

代码是Perl。

foreach my $ref_array1 (@$array1) {     # loop through 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++;

感谢任何形式的帮助。 三江源

1 个答案:

答案 0 :(得分:3)

以下不完整的示例有效(即,它将5放在单元格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;
}

另请参阅我的Perl Win32::OLE example