4 my $type = $args{'type'};
5 my $header = $args{'header'};
6 my $rows = $args{'rows'};
7 my %opt = %{ $args{'opt'} };
9 my $style = $opt{'style'};
11 my $override = scalar(@$rows) >= 65536 ? 'XLSX' : '';
13 my $format = $FS::CurrentUser::CurrentUser->spreadsheet_format($override);
15 my $filename = $opt{'name'} || PL($opt{'name_singular'});
16 $filename .= $format->{extension};
18 #http_header('Content-Type' => 'application/excel' ); #eww
19 #http_header('Content-Type' => 'application/msexcel' ); #alas
20 #http_header('Content-Type' => 'application/x-msexcel' ); #?
22 #http://support.microsoft.com/kb/199841
23 http_header('Content-Type' => $format->{mime_type} );
24 http_header('Content-Disposition' => qq!attachment;filename="$filename"! );
26 #http://support.microsoft.com/kb/812935
27 #http://support.microsoft.com/kb/323308
28 $HTML::Mason::Commands::r->headers_out->{'Cache-control'} = 'max-age=0';
31 my $XLS = new IO::Scalar \$data;
32 my $workbook = $format->{class}->new($XLS)
33 or die "Error opening Excel file: $!";
35 my $title = $opt{'title'};
36 $title =~ s/[\[\]\:\*\?\/\/]//g;
37 $title = substr($title, 0, 31);
38 my $worksheet = $workbook->add_worksheet($title);
40 $worksheet->protect();
44 my $header_format = $workbook->add_format(
50 my $footer_format = $workbook->add_format(
56 my $default_format = $workbook->add_format(locked => 0);
59 my $money_char = FS::Conf->new->config('money_char') || '$';
67 # Wrapper for $worksheet->write.
68 # Do any massaging of the value/format here.
69 my ($r, $c, $value, $format) = @_;
70 #warn "writer called with format $format\n";
72 if ( $style->[$c] eq 'b' or $value =~ /<b>/i ) { # the only one in common use
73 $value =~ s[</?b>][]ig;
74 if ( !exists($bold_format{$format}) ) {
75 $bold_format{$format} = $workbook->add_format();
76 $bold_format{$format}->copy($format);
77 $bold_format{$format}->set_bold();
79 $format = $bold_format{$format};
82 # convert HTML entities
83 # both Spreadsheet::WriteExcel and Excel::Writer::XLSX accept UTF-8 strings
84 $value = decode_entities($value);
86 if ( $value =~ /^\Q$money_char\E(-?\d+\.?\d*)$/ ) {
87 # Currency: strip the symbol, clone the requested format,
88 # and format it for currency
90 # warn "formatting $value as money\n";
91 if ( !exists($money_format{$format}) ) {
92 $money_format{$format} = $workbook->add_format();
93 $money_format{$format}->copy($format);
94 $money_format{$format}->set_num_format($money_char.'#0.00#');
96 $format = $money_format{$format};
98 elsif ( $value =~ /^([A-Z][a-z]{2}) (\d{2}) (\d{4})$/ ) {
99 # Date: convert the value to an Excel date number and set
101 $value = xl_parse_date($value);
102 # warn "formatting $value as date\n";
103 if ( !exists($date_format{$format}) ) {
104 $date_format{$format} = $workbook->add_format();
105 $date_format{$format}->copy($format);
106 $date_format{$format}->set_num_format('mmm dd yyyy');
108 $format = $date_format{$format};
111 # String: replace line breaks with newlines
112 $value =~ s/<BR>/\n/gi;
114 #warn "writing with format $format\n";
115 $worksheet->write($r, $c, $value, $format);
118 $writer->( $r, $c++, $_, $header_format ) foreach @$header;
120 foreach my $row ( @$rows ) {
124 if ( $opt{'fields'} ) {
126 #my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
127 #my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
128 #could also translate color, size, style into xls equivalents?
129 my $formats = $opt{'xls_format'} ? [ @{$opt{'xls_format'}} ] : [];
131 foreach my $field ( @{$opt{'fields'}} ) {
133 my $xls_format = $default_format;
135 if ( my $format = shift @$formats ) {
136 $format = &{$format}($row) if ref($format) eq 'CODE';
138 $xls_format = $workbook->add_format(locked=>0, %$format);
141 if ( ref($field) eq 'CODE' ) {
142 foreach my $value ( &{$field}($row) ) {
143 if ( ref($value) eq 'ARRAY' ) {
144 $writer->($r, $c++, '(N/A)' ); #unimplemented
146 $writer->($r, $c++, $value, $xls_format );
150 $writer->( $r, $c++, $row->$field(), $xls_format );
155 # no need for each row to need a new format
156 #my $xls_format = $workbook->add_format(locked=>0);
157 $writer->( $r, $c++, $_, $default_format ) foreach @$row;
162 if ( $opt{'footer'} ) {
165 foreach my $item (@{ $opt{'footer'} }) {
166 if ( ref($item) eq 'CODE' ) {
169 $writer->( $r, $c++, $item, $footer_format );
173 $workbook->close();# or die "Error creating .xls file: $!";
175 http_header('Content-Length' => length($data) );