}
-=item spreadsheet_format
+=item spreadsheet_format [ OVERRIDE ]
Returns a hashref of this user's Excel spreadsheet download settings:
'extension' (xls or xlsx), 'class' (Spreadsheet::WriteExcel or
-Excel::Writer::XLSX), and 'mime_type'.
+Excel::Writer::XLSX), and 'mime_type'. If OVERRIDE is 'XLS' or 'XLSX',
+use that instead of the user's setting.
=cut
+# is there a better place to put this?
my %formats = (
XLS => {
extension => '.xls',
sub spreadsheet_format {
my $self = shift;
+ my $override = shift;
- my $f = $self->option('spreadsheet_format')
- || $conf->config('spreadsheet_format')
- || 'XLS';
+ my $f = $override
+ || $self->option('spreadsheet_format')
+ || $conf->config('spreadsheet_format')
+ || 'XLS';
$formats{$f};
}
% }
%
% } elsif ( $cgi->param('_type') =~ /(xls)$/ ) {
-%
-% #http_header('Content-Type' => 'application/excel' ); #eww
-% http_header('Content-Type' => 'application/vnd.ms-excel' );
-% #http_header('Content-Type' => 'application/msexcel' ); #alas
-% http_header('Content-Disposition' => "attachment;filename=$filename.xls");
+% #false laziness w/ search/elements/search-xls
+% my $format = $FS::CurrentUser::CurrentUser->spreadsheet_format;
+% $filename .= $format->{extension};
+%
+% http_header('Content-Type' => $format->{mime_type} );
+% http_header('Content-Disposition' => qq!attachment;filename="$filename"! );
%
% my $output = '';
% my $XLS = new IO::Scalar \$output;
-% my $workbook = Spreadsheet::WriteExcel->new($XLS)
+% my $workbook = $format->{class}->new($XLS)
% or die "Error opening .xls file: $!";
%
% my $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
<% include('/elements/footer.html') %>
% }
-<%once>
-
-</%once>
<%init>
my(%opt) = @_;
my $rows = $args{'rows'};
my %opt = %{ $args{'opt'} };
-my $format = $FS::CurrentUser::CurrentUser->spreadsheet_format;
+my $override = scalar(@$rows) >= 65536 ? 'XLSX' : '';
+
+my $format = $FS::CurrentUser::CurrentUser->spreadsheet_format($override);
+
my $filename = $opt{'name'} || PL($opt{'name_singular'});
$filename .= $format->{extension};
<% $data %>
<%init>
+my $htmldoc = include('report_tax.cgi');
+
+my ($title) = ($htmldoc =~ /<title>\s*(.*)\s*<\/title>/i);
+
+# do this first so we can override the format if it's too many rows
+# attribs option: how to locate the table? It's the only one with class="grid".
+my $te = HTML::TableExtract->new(attribs => {class => 'grid'});
+$te->parse($htmldoc);
+my $table = $te->first_table_found;
+
+my $override = ($table->row_count >= 65536 ? 'XLSX' : '');
+my $format = $FS::CurrentUser::CurrentUser->spreadsheet_format($override);
+my $filename = 'report_tax'.$format->{extension};
+
+http_header('Content-Type' => $format->{mime_type});
+http_header('Content-Disposition' => qq!attachment;filename="$filename"! );
my $data = '';
my $XLS = new IO::Scalar \$data;
-my $workbook = Spreadsheet::WriteExcel->new($XLS)
+my $workbook = $format->{class}->new($XLS)
or die "Error opening .xls file: $!";
# hardcoded formats, this could be handled better
}
my $ws = $workbook->add_worksheet('taxreport');
-my $htmldoc = include('report_tax.cgi');
-
-my ($title) = ($htmldoc =~ /<title>\s*(.*)\s*<\/title>/i);
-
-# attribs option: how to locate the table? It's the only one with class="grid".
-my $te = HTML::TableExtract->new(attribs => {class => 'grid'});
-$te->parse($htmldoc);
-my $table = $te->first_table_found;
-
my @sheet;
$sheet[0][0] = {
text => $title,
$workbook->close;
-http_header('Content-Type' => 'application/vnd.ms-excel');
-http_header('Content-Disposition' => 'attachment;filename="report_tax.xls"');
</%init>