Those two modules let you create a spreadsheet. While you might be able to manipulate the data to sort it after you create it, I'd suggest you just sort the data before you construct your spreadsheet.
Normally when I create a spreadsheet in perl, I build the data structures and only when I've got the data I want, do I create the workbook, like this:
#!env perl
use strict;
use warnings;
use Spreadsheet::WriteExcel;
my @data;
# Build some data
for (1 .. 20) {
my $name = "";
$name .= substr(join("",'A'..'Z'),int(26*rand),1) for 1 .. 2+int(5
+*rand);
my $hr = {
price => int(5000*rand),
qty => int(100*rand),
name => $name,
};
push @data, $hr;
}
# Make workbook
my $XL=Spreadsheet::WriteExcel->new("PM1215003.xls");
# Worksheet 1: data sorted by name
my $WB = $XL->add_worksheet("ByName");
add_data_to_sheet($WB, sort { $a->{name} cmp $b->{name} } @data);
# Worksheet 2: same data, but sorted by price
$WB = $XL->add_worksheet("ByPrice");
add_data_to_sheet($WB, sort { $a->{price} <=> $b->{price} } @data);
sub add_data_to_sheet {
my ($WS, @rows) = @_;
my $R=0;
$WB->write($R, 0, 'Name');
$WB->write($R, 1, 'Price');
$WB->write($R, 2, 'Quantity');
for my $hr (@rows) {
++$R;
$WB->write($R, 0, $hr->{name});
$WB->write($R, 1, $hr->{price});
$WB->write($R, 2, $hr->{qty});
}
}
...roboticus
When your only tool is a hammer, all problems look like your thumb. |