XLSX format for spreadsheet download, #17971
[freeside.git] / httemplate / search / elements / search-xls.html
1 <% $data %>
2 <%init>
3
4 my %args = @_;
5 my $type   = $args{'type'};
6 my $header = $args{'header'};
7 my $rows   = $args{'rows'};
8 my %opt    = %{ $args{'opt'} };    
9
10 my $format = $FS::CurrentUser::CurrentUser->spreadsheet_format;
11 my $filename = $opt{'name'} || PL($opt{'name_singular'});
12 $filename .= $format->{extension};
13
14 #http_header('Content-Type' => 'application/excel' ); #eww
15 #http_header('Content-Type' => 'application/msexcel' ); #alas
16 #http_header('Content-Type' => 'application/x-msexcel' ); #?
17
18 #http://support.microsoft.com/kb/199841
19 http_header('Content-Type' => $format->{mime_type} );
20 http_header('Content-Disposition' => qq!attachment;filename="$filename"! );
21  
22 #http://support.microsoft.com/kb/812935
23 #http://support.microsoft.com/kb/323308
24 $HTML::Mason::Commands::r->headers_out->{'Cache-control'} = 'max-age=0';
25
26 my $data = '';
27 my $XLS = new IO::Scalar \$data;
28 my $workbook = $format->{class}->new($XLS)
29   or die "Error opening Excel file: $!";
30
31 my $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
32
33 $worksheet->protect();
34
35 my($r,$c) = (0,0);
36
37 my $header_format = $workbook->add_format(
38   bold     => 1,
39   locked   => 1,
40   bg_color => 55, #22,
41   bottom   => 3,
42 );
43 my $default_format = $workbook->add_format(locked => 0);
44
45 my %money_format;
46 my $money_char = FS::Conf->new->config('money_char') || '$';
47
48 my %date_format;
49 xl_parse_date_init();
50
51 my $writer = sub {
52   # Wrapper for $worksheet->write.
53   # Do any massaging of the value/format here.
54   my ($r, $c, $value, $format) = @_;
55   if ( $value =~ /^\Q$money_char\E(-?\d+\.?\d*)$/ ) {
56     # Currency: strip the symbol, clone the requested format,
57     # and format it for currency
58     $value = $1;
59 #    warn "formatting $value as money\n";
60     if ( !exists($money_format{$format}) ) {
61       $money_format{$format} = $workbook->add_format();
62       $money_format{$format}->copy($format);
63       $money_format{$format}->set_num_format($money_char.'#0.00#');
64     }
65     $format = $money_format{$format};
66   }
67   elsif ( $value =~ /^([A-Z][a-z]{2}) (\d{2}) (\d{4})$/ ) {
68     # Date: convert the value to an Excel date number and set 
69     # the format
70     $value = xl_parse_date($value);
71 #    warn "formatting $value as date\n";
72     if ( !exists($date_format{$format}) ) {
73       $date_format{$format} = $workbook->add_format();
74       $date_format{$format}->copy($format);
75       $date_format{$format}->set_num_format('mmm dd yyyy');
76     }
77     $format = $date_format{$format};
78   }
79   else {
80     # String: replace line breaks with newlines
81     $value =~ s/<BR>/\n/gi;
82   }
83   $worksheet->write($r, $c, $value, $format);
84 };
85
86 $writer->( $r, $c++, $_, $header_format ) foreach @$header;
87
88 foreach my $row ( @$rows ) {
89   $r++;
90   $c = 0;
91
92   if ( $opt{'fields'} ) {
93
94     #my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
95     #my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
96     #could also translate color, size, style into xls equivalents?
97     my $formats = $opt{'xls_format'} ? [ @{$opt{'xls_format'}} ] : [];
98
99     foreach my $field ( @{$opt{'fields'}} ) {
100
101       my $xls_format = $default_format;
102
103       if ( my $format = shift @$formats ) {
104         $format = &{$format}($row) if ref($format) eq 'CODE';
105         $format ||= {};
106         $xls_format = $workbook->add_format(locked=>0, %$format);
107       }
108
109       if ( ref($field) eq 'CODE' ) {
110         foreach my $value ( &{$field}($row) ) {
111           if ( ref($value) eq 'ARRAY' ) { 
112             $writer->($r, $c++, '(N/A)' ); #unimplemented
113           } else {
114             $writer->($r, $c++, $value, $xls_format );
115           }
116         }
117       } else {
118         $writer->( $r, $c++, $row->$field(), $xls_format );
119       }
120     }
121
122   } else {
123     # no need for each row to need a new format
124     #my $xls_format = $workbook->add_format(locked=>0);
125     $writer->( $r, $c++, $_, $default_format ) foreach @$row;
126   }
127
128 }
129
130 $workbook->close();# or die "Error creating .xls file: $!";
131
132 http_header('Content-Length' => length($data) );
133
134 </%init>