I’d like to use the function “autofit_columns” as found here:CPAN
Here’s my program so far(I skipped the DB connect and query part)
my $workbook = Spreadsheet::WriteExcel->new("TEST.xls");
my $bold = $workbook->add_format();
$bold->set_bold();
my $number = $workbook->add_format();
$number->set_num_format(0x01);
$worksheet = $workbook->add_worksheet('Sheet1');
my @headings = ('Blabla...');
foreach $i (@headings){
$worksheet->write(0, $col++, $i, $bold);
};
$col=0;
$lrow=1;
while (@row = $sth->fetchrow_array()) {
$worksheet->write($lrow,$col,\@row);
$lrow++;
};
$sth->finish;
$dbh->disconnect;
autofit_columns($worksheet);
$workbook->close();
sub autofit_columns {
my $worksheet = shift;
my $col = 0;
for my $width (@{$worksheet->{__col_widths}}) {
$worksheet->set_column($col, $col, $width) if $width;
$col++;
}
}
PROBLEM: My columns are not autofitted in the xls file… Any idea why?
I don’t get the peice of code:
for my $width (@{$worksheet->{__col_widths}}) {
$worksheet->set_column($col, $col, $width) if $width;
$col++;
}
You need to look at that example again and implement
add_write_handlerpart too before you write anything to your worksheet.Please take a look at
line and then at
store_string_widthssubroutine implementation.Answer is that you need to store absolute width of the string at each write. Then, after you wrote all data to your worksheet, you need to walk through rows and find the biggest string’s ‘length’ for each column – that would be desired column width.
Wish you luck.