#!/usr/bin/perl use strict; use warnings; use Spreadsheet::WriteExcel(); my $book = Spreadsheet::WriteExcel->new('example.xls') or die "Couldn't create spreadsheet : $!"; my $sheet = $book->add_worksheet('Example sheet'); ## Create the title format 'bold' my $bold = $book->add_format(); $bold->set_bold(); my @months = qw (Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); my @max_col_width; my $row = my $col = 0; ## Write titles $sheet->write( $row, $col++, $_, $bold ) for ('Year',@months); ## Move to beginning of next row $col = 0; $row++; ## Write arbitrary values my $val = 0; for my $year ( 2003 .. 2007 ) { ## Decide whether to increase the max width for the year column my $width = length($val); $max_col_width[$col] = $width if !defined $max_col_width[$col] || $width > $max_col_width[$col]; $sheet->write($row,$col++,$year); for ( 1..@months) { ## Decide whether to increase the max width for this column my $width = length($val); $max_col_width[$col] = $width if !defined $max_col_width[$col] || $width > $max_col_width[$col]; $sheet->write( $row, $col++, $val ); $val = $val + 50; } ## Move to the beginning of the next row $row++; $col = 0; } ## Set the width of each column to the width of the longest value foreach my $column (0..$#max_col_width) { $sheet->set_column($column,$column,$max_col_width[$column]); } $book->close or die "Couldn't close spreadsheet : $!";