+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"! );