Thanks to John the set_column() method is now much easier to use. Nonetheless
I discarded all approaches I developed in this thread and found the solution below which is much easier to comprehend, I believe.
#!perl -l015
use strict;
use Spreadsheet::WriteExcel;
my $arr = [
[ "a11", "12", "a13" ],
[ "a21", "22", "a23" ],
[ "a31", "32", "a33" ],
];
my $outputfile = "output.xls";
my $workbook = Spreadsheet::WriteExcel->new($outputfile);
my $worksheet = $workbook->addworksheet("a test");
my $number = $workbook->addformat(
font => "Helvetica",
size => 18,
num_format => "0.00",
align => "right",
);
my $string = $workbook->addformat(
font => "Helvetica",
size => 18,
align => "left",
bold => 1,
);
my $format =
[ [ $string, $number, $string ], [ 16, 12, 16 ] ];
my $line;
for my $row (@$arr) {
$worksheet->write_formatted_row( 3 + $line++,
0, $row, $format );
}
$workbook->close();
chomp( my $pwd = `pwd` );
my $file = $pwd . $outputfile;
$^O =~ /Mac/ and MacPerl::DoAppleScript( <<eos );
tell application "Microsoft Excel"
open "$file"
activate
end tell
eos
package Spreadsheet::WriteExcel::Worksheet;
my $width_is_set = 0;
sub write_formatted_row {
my ( $self, $rowstart, $colstart, $ref, $format ) = @_;
my @arr = @$ref;
unless ($width_is_set) {
for my $col ( 0 .. $#{ @$format->[1] } ) {
my $colwidth = $format->[1][$col];
$self->set_column(
$colstart + $col,
$colstart + $col,
$colwidth, undef
);
}
$width_is_set = 1;
}
for my $col ( 0 .. $#arr ) {
my $colformat = $format->[0][$col];
die "parameter error: no format objects specified\n"
unless ref $colformat eq
'Spreadsheet::WriteExcel::Format';
$self->write(
$rowstart, $colstart + $col,
$arr[$col], $colformat
);
}
}