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 $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
37 $worksheet->protect();
41 my $header_format = $workbook->add_format(
47 my $footer_format = $workbook->add_format(
53 my $default_format = $workbook->add_format(locked => 0);
56 my $money_char = FS::Conf->new->config('money_char') || '$';
64 # Wrapper for $worksheet->write.
65 # Do any massaging of the value/format here.
66 my ($r, $c, $value, $format) = @_;
67 #warn "writer called with format $format\n";
69 if ( $style->[$c] eq 'b' or $value =~ /<b>/i ) { # the only one in common use
70 $value =~ s[</?b>][]ig;
71 if ( !exists($bold_format{$format}) ) {
72 $bold_format{$format} = $workbook->add_format();
73 $bold_format{$format}->copy($format);
74 $bold_format{$format}->set_bold();
76 $format = $bold_format{$format};
79 # convert HTML entities
80 # both Spreadsheet::WriteExcel and Excel::Writer::XLSX accept UTF-8 strings
81 $value = decode_entities($value);
83 if ( $value =~ /^\Q$money_char\E(-?\d+\.?\d*)$/ ) {
84 # Currency: strip the symbol, clone the requested format,
85 # and format it for currency
87 # warn "formatting $value as money\n";
88 if ( !exists($money_format{$format}) ) {
89 $money_format{$format} = $workbook->add_format();
90 $money_format{$format}->copy($format);
91 $money_format{$format}->set_num_format($money_char.'#0.00#');
93 $format = $money_format{$format};
95 elsif ( $value =~ /^([A-Z][a-z]{2}) (\d{2}) (\d{4})$/ ) {
96 # Date: convert the value to an Excel date number and set
98 $value = xl_parse_date($value);
99 # warn "formatting $value as date\n";
100 if ( !exists($date_format{$format}) ) {
101 $date_format{$format} = $workbook->add_format();
102 $date_format{$format}->copy($format);
103 $date_format{$format}->set_num_format('mmm dd yyyy');
105 $format = $date_format{$format};
108 # String: replace line breaks with newlines
109 $value =~ s/<BR>/\n/gi;
111 #warn "writing with format $format\n";
112 $worksheet->write($r, $c, $value, $format);
115 $writer->( $r, $c++, $_, $header_format ) foreach @$header;
117 foreach my $row ( @$rows ) {
121 if ( $opt{'fields'} ) {
123 #my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
124 #my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
125 #could also translate color, size, style into xls equivalents?
126 my $formats = $opt{'xls_format'} ? [ @{$opt{'xls_format'}} ] : [];
128 foreach my $field ( @{$opt{'fields'}} ) {
130 my $xls_format = $default_format;
132 if ( my $format = shift @$formats ) {
133 $format = &{$format}($row) if ref($format) eq 'CODE';
135 $xls_format = $workbook->add_format(locked=>0, %$format);
138 if ( ref($field) eq 'CODE' ) {
139 foreach my $value ( &{$field}($row) ) {
140 if ( ref($value) eq 'ARRAY' ) {
141 $writer->($r, $c++, '(N/A)' ); #unimplemented
143 $writer->($r, $c++, $value, $xls_format );
147 $writer->( $r, $c++, $row->$field(), $xls_format );
152 # no need for each row to need a new format
153 #my $xls_format = $workbook->add_format(locked=>0);
154 $writer->( $r, $c++, $_, $default_format ) foreach @$row;
159 if ( $opt{'footer'} ) {
162 foreach my $item (@{ $opt{'footer'} }) {
163 if ( ref($item) eq 'CODE' ) {
166 $writer->( $r, $c++, $item, $footer_format );
170 $workbook->close();# or die "Error creating .xls file: $!";
172 http_header('Content-Length' => length($data) );