如何使用Perl将multidimensional array(AoA)添加到Excel?

我想使用Perl在Excel中添加存储在2×2维数组中的数据。 我知道如何打开和添加简单的数据。 这我可以使用for循环。 但是,我怎么能优雅地做呢?

这是我想要做的

$sheet->Range("A1:B"."$size")->{Value} = @$data; or @data; or {@data}; or {\@data}; 

其中@data是二维数组。

 # use existing instance if Excel is already running eval {$ex = Win32::OLE->GetActiveObject('Excel.Application')}; die "Excel not installed" if $@; unless (defined $ex) { $ex = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;}) or die "Oops, cannot start Excel"; } # get a new workbook $book = $ex->Workbooks->Add; # write to a particular cell $sheet = $book->Worksheets(1); print "A1:B"."$size"; # write a 2 rows by 3 columns range $sheet->Range("A1:B"."$size")->{Value} = @$data; 

我发现你正在使用Win32 :: OLE,但这种事情对于Spreadsheet :: WriteExcel来说也是相当简单的:

 #!/usr/bin/perl -w use strict; use Spreadsheet::WriteExcel; my $workbook = Spreadsheet::WriteExcel->new('test.xls'); my $worksheet = $workbook->add_worksheet(); # Get your AoA from somewhere. my $data = [ [ 'Hello', 'world', 123 ], [ 'Bye', 'bye', 4.567 ], ]; # Write the data. $worksheet->write_col( 'A1', $data ); 

根据一些阅读(我没有在我面前的Win32框),它看起来像RangeValue属性处理正确的AoA的引用,所以试着说:

 my $data = [ ["a1", "b1"], ["a2", "b2"], ]; $sheet->Range("A1:B" . @$data)->{Value} = $data;