我想使用 Perl 在 Excel 中添加存储在 2x2 维数组中的数据。我知道如何打开和添加简单数据。我可以使用 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 );
关于perl - 如何使用 Perl 将多维数组 (AoA) 添加到 Excel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/729482/