5 my $type = $args{'type'};
6 my $header = $args{'header'};
7 my $rows = $args{'rows'};
8 my %opt = %{ $args{'opt'} };
10 #http_header('Content-Type' => 'application/excel' ); #eww
11 #http_header('Content-Type' => 'application/msexcel' ); #alas
12 #http_header('Content-Type' => 'application/x-msexcel' ); #?
14 #http://support.microsoft.com/kb/199841
15 http_header('Content-Type' => 'application/vnd.ms-excel' );
17 #http://support.microsoft.com/kb/812935
18 #http://support.microsoft.com/kb/323308
19 $HTML::Mason::Commands::r->headers_out->{'Cache-control'} = 'max-age=0';
22 my $XLS = new IO::Scalar \$data;
23 my $workbook = Spreadsheet::WriteExcel->new($XLS)
24 or die "Error opening .xls file: $!";
26 my $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
28 $worksheet->protect();
32 my $header_format = $workbook->add_format(
39 $worksheet->write($r, $c++, $_, $header_format ) foreach @$header;
41 foreach my $row ( @$rows ) {
45 if ( $opt{'fields'} ) {
47 #my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
48 #my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
49 #could also translate color, size, style into xls equivalents?
50 my $formats = $opt{'xls_format'} ? [ @{$opt{'xls_format'}} ] : [];
52 foreach my $field ( @{$opt{'fields'}} ) {
54 my $format = shift @$formats;
55 $format = &{$format}($row) if ref($format) eq 'CODE';
57 my $xls_format = $workbook->add_format(locked=>0, %$format);
59 if ( ref($field) eq 'CODE' ) {
60 foreach my $value ( &{$field}($row) ) {
61 if ( ref($value) eq 'ARRAY' ) {
62 $worksheet->write($r, $c++, '(N/A)' ); #unimplemented
64 $worksheet->write($r, $c++, $value, $xls_format );
68 $worksheet->write($r, $c++, $row->$field(), $xls_format );
73 my $xls_format = $workbook->add_format(locked=>0);
74 $worksheet->write($r, $c++, $_, $xls_format ) foreach @$row;
79 $workbook->close();# or die "Error creating .xls file: $!";
81 http_header('Content-Length' => length($data) );