How to add multidimensional array (AoA) in Excel using Perl?

I want to add my data stored in a 2x2 array in Excel using Perl. I know how to open and add simple data. I can use this for a loop. But how can I do it elegantly?

This is what I'm trying to do.

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

where @datais a two-dimensional array.

# 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;
+3
source share
2 answers

I see that you are using Win32 :: OLE, but it is also quite easy with 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 );
+3
source

( Win32 ), , Value Range AoA , :

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

Source: https://habr.com/ru/post/1706107/


All Articles