3
###############################################################################
5
# Example of fictional sales sheet to demonstrate several different features.
6
# Also uses functions from the Spreadsheet::WriteExcel::Utility module.
8
# reverse('�'), October 2001, John McNamara, jmcnamara@cpan.org
12
use Spreadsheet::WriteExcel;
13
use Spreadsheet::WriteExcel::Utility;
15
# Create a new workbook and add a worksheet
16
my $workbook = Spreadsheet::WriteExcel->new("sales.xls");
17
my $worksheet = $workbook->add_worksheet('May Sales');
32
num_format => '$#,##0.00'
35
my $heading = $workbook->add_format(%heading);
36
my $total_format = $workbook->add_format(%total);
37
my $price_format = $workbook->add_format(num_format => '$#,##0.00');
38
my $date_format = $workbook->add_format(num_format => 'mmm d yyy');
41
# Write the main headings
42
$worksheet->freeze_panes(1); # Freeze the first row
43
$worksheet->write('A1', 'Item', $heading);
44
$worksheet->write('B1', 'Quantity', $heading);
45
$worksheet->write('C1', 'Price', $heading);
46
$worksheet->write('D1', 'Total', $heading);
47
$worksheet->write('E1', 'Date', $heading);
49
# Set the column widths
50
$worksheet->set_column('A:A', 25);
51
$worksheet->set_column('B:B', 10);
52
$worksheet->set_column('C:E', 16);
55
# Extract the sales data from the __DATA__ section at the end of the file.
56
# In reality this information would probably come from a database
59
foreach my $line (<DATA>) {
62
# Simple-minded processing of CSV data. Refer to the Text::CSV_XS
63
# and Text::xSV modules for a more complete CVS handling.
64
my @items = split /,/, $line;
69
# Write out the items from each row
71
foreach my $sale (@sales) {
73
$worksheet->write($row, 0, @$sale[0]);
74
$worksheet->write($row, 1, @$sale[1]);
75
$worksheet->write($row, 2, @$sale[2], $price_format);
77
# Create a formula like '=B2*C2'
79
. xl_rowcol_to_cell($row, 1)
81
. xl_rowcol_to_cell($row, 2);
83
$worksheet->write($row, 3, $formula, $price_format);
86
my $date = xl_decode_date_US(@$sale[3]);
87
$worksheet->write($row, 4, $date, $date_format);
91
# Create a formula to sum the totals, like '=SUM(D2:D6)'
92
my $total = '=SUM(D2:'
93
. xl_rowcol_to_cell($row-1, 3)
96
$worksheet->write($row, 3, $total, $total_format);
101
586 card,20,125.50,5/12/01
102
Flat Screen Monitor,1,1300.00,5/12/01
103
64 MB dimms,45,49.99,5/13/01
104
15 GB HD,12,300.00,5/13/01
105
Speakers (pair),5,15.50,5/14/01