4 my $type = $args{'type'};
5 my $header = $args{'header'};
6 my $rows = $args{'rows'};
7 my %opt = %{ $args{'opt'} };
9 my $override = scalar(@$rows) >= 65536 ? 'XLSX' : '';
11 my $format = $FS::CurrentUser::CurrentUser->spreadsheet_format($override);
13 my $filename = $opt{'name'} || PL($opt{'name_singular'});
14 $filename .= $format->{extension};
16 #http_header('Content-Type' => 'application/excel' ); #eww
17 #http_header('Content-Type' => 'application/msexcel' ); #alas
18 #http_header('Content-Type' => 'application/x-msexcel' ); #?
20 #http://support.microsoft.com/kb/199841
21 http_header('Content-Type' => $format->{mime_type} );
22 http_header('Content-Disposition' => qq!attachment;filename="$filename"! );
24 #http://support.microsoft.com/kb/812935
25 #http://support.microsoft.com/kb/323308
26 $HTML::Mason::Commands::r->headers_out->{'Cache-control'} = 'max-age=0';
29 my $XLS = new IO::Scalar \$data;
30 my $workbook = $format->{class}->new($XLS)
31 or die "Error opening Excel file: $!";
33 my $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
35 $worksheet->protect();
39 my $header_format = $workbook->add_format(
45 my $default_format = $workbook->add_format(locked => 0);
48 my $money_char = FS::Conf->new->config('money_char') || '$';
54 # Wrapper for $worksheet->write.
55 # Do any massaging of the value/format here.
56 my ($r, $c, $value, $format) = @_;
57 # convert HTML entities
58 # both Spreadsheet::WriteExcel and Excel::Writer::XLSX accept UTF-8 strings
59 $value = decode_entities($value);
61 if ( $value =~ /^\Q$money_char\E(-?\d+\.?\d*)$/ ) {
62 # Currency: strip the symbol, clone the requested format,
63 # and format it for currency
65 # warn "formatting $value as money\n";
66 if ( !exists($money_format{$format}) ) {
67 $money_format{$format} = $workbook->add_format();
68 $money_format{$format}->copy($format);
69 $money_format{$format}->set_num_format($money_char.'#0.00#');
71 $format = $money_format{$format};
73 elsif ( $value =~ /^([A-Z][a-z]{2}) (\d{2}) (\d{4})$/ ) {
74 # Date: convert the value to an Excel date number and set
76 $value = xl_parse_date($value);
77 # warn "formatting $value as date\n";
78 if ( !exists($date_format{$format}) ) {
79 $date_format{$format} = $workbook->add_format();
80 $date_format{$format}->copy($format);
81 $date_format{$format}->set_num_format('mmm dd yyyy');
83 $format = $date_format{$format};
86 # String: replace line breaks with newlines
87 $value =~ s/<BR>/\n/gi;
89 $worksheet->write($r, $c, $value, $format);
92 $writer->( $r, $c++, $_, $header_format ) foreach @$header;
94 foreach my $row ( @$rows ) {
98 if ( $opt{'fields'} ) {
100 #my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
101 #my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
102 #could also translate color, size, style into xls equivalents?
103 my $formats = $opt{'xls_format'} ? [ @{$opt{'xls_format'}} ] : [];
105 foreach my $field ( @{$opt{'fields'}} ) {
107 my $xls_format = $default_format;
109 if ( my $format = shift @$formats ) {
110 $format = &{$format}($row) if ref($format) eq 'CODE';
112 $xls_format = $workbook->add_format(locked=>0, %$format);
115 if ( ref($field) eq 'CODE' ) {
116 foreach my $value ( &{$field}($row) ) {
117 if ( ref($value) eq 'ARRAY' ) {
118 $writer->($r, $c++, '(N/A)' ); #unimplemented
120 $writer->($r, $c++, $value, $xls_format );
124 $writer->( $r, $c++, $row->$field(), $xls_format );
129 # no need for each row to need a new format
130 #my $xls_format = $workbook->add_format(locked=>0);
131 $writer->( $r, $c++, $_, $default_format ) foreach @$row;
136 if ( $opt{'footer'} ) {
139 foreach my $item (@{ $opt{'footer'} }) {
140 if ( ref($item) eq 'CODE' ) {
143 $writer->( $r, $c++, $item, $header_format );
147 $workbook->close();# or die "Error creating .xls file: $!";
149 http_header('Content-Length' => length($data) );