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' );
16 http_header('Content-Disposition' =>
17 'attachment;filename="'.($opt{'name'} || PL($opt{'name_singular'}) ).'.xls"');
19 #http://support.microsoft.com/kb/812935
20 #http://support.microsoft.com/kb/323308
21 $HTML::Mason::Commands::r->headers_out->{'Cache-control'} = 'max-age=0';
24 my $XLS = new IO::Scalar \$data;
25 my $workbook = Spreadsheet::WriteExcel->new($XLS)
26 or die "Error opening .xls file: $!";
28 my $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
30 $worksheet->protect();
34 my $header_format = $workbook->add_format(
40 my $default_format = $workbook->add_format(locked => 0);
43 my $money_char = FS::Conf->new->config('money_char') || '$';
49 # Wrapper for $worksheet->write.
50 # Do any massaging of the value/format here.
51 my ($r, $c, $value, $format) = @_;
52 if ( $value =~ /^\Q$money_char\E(-?\d+\.?\d*)$/ ) {
53 # Currency: strip the symbol, clone the requested format,
54 # and format it for currency
56 # warn "formatting $value as money\n";
57 if ( !exists($money_format{$format}) ) {
58 $money_format{$format} = $workbook->add_format();
59 $money_format{$format}->copy($format);
60 $money_format{$format}->set_num_format($money_char.'#0.00#');
62 $format = $money_format{$format};
64 elsif ( $value =~ /^([A-Z][a-z]{2}) (\d{2}) (\d{4})$/ ) {
65 # Date: convert the value to an Excel date number and set
67 $value = xl_parse_date($value);
68 # warn "formatting $value as date\n";
69 if ( !exists($date_format{$format}) ) {
70 $date_format{$format} = $workbook->add_format();
71 $date_format{$format}->copy($format);
72 $date_format{$format}->set_num_format('mmm dd yyyy');
74 $format = $date_format{$format};
77 # String: replace line breaks with newlines
78 $value =~ s/<BR>/\n/gi;
80 $worksheet->write($r, $c, $value, $format);
83 $writer->( $r, $c++, $_, $header_format ) foreach @$header;
85 foreach my $row ( @$rows ) {
89 if ( $opt{'fields'} ) {
91 #my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
92 #my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
93 #could also translate color, size, style into xls equivalents?
94 my $formats = $opt{'xls_format'} ? [ @{$opt{'xls_format'}} ] : [];
96 foreach my $field ( @{$opt{'fields'}} ) {
98 my $xls_format = $default_format;
100 if ( my $format = shift @$formats ) {
101 $format = &{$format}($row) if ref($format) eq 'CODE';
103 $xls_format = $workbook->add_format(locked=>0, %$format);
106 if ( ref($field) eq 'CODE' ) {
107 foreach my $value ( &{$field}($row) ) {
108 if ( ref($value) eq 'ARRAY' ) {
109 $writer->($r, $c++, '(N/A)' ); #unimplemented
111 $writer->($r, $c++, $value, $xls_format );
115 $writer->( $r, $c++, $row->$field(), $xls_format );
120 # no need for each row to need a new format
121 #my $xls_format = $workbook->add_format(locked=>0);
122 $writer->( $r, $c++, $_, $default_format ) foreach @$row;
127 $workbook->close();# or die "Error creating .xls file: $!";
129 http_header('Content-Length' => length($data) );