2 my $htmldoc = include('report_tax.cgi');
4 my ($title) = ($htmldoc =~ /<title>\s*(.*)\s*<\/title>/i);
6 # do this first so we can override the format if it's too many rows
7 # attribs option: how to locate the table? It's the only one with class="grid".
8 my $te = HTML::TableExtract->new(attribs => {class => 'grid'});
10 my $table = $te->first_table_found;
12 my $override = ($table->row_count >= 65536 ? 'XLSX' : '');
13 my $format = $FS::CurrentUser::CurrentUser->spreadsheet_format($override);
14 my $filename = 'report_tax'.$format->{extension};
16 http_header('Content-Type' => $format->{mime_type});
17 http_header('Content-Disposition' => qq!attachment;filename="$filename"! );
20 my $XLS = new IO::Scalar \$data;
21 my $workbook = $format->{class}->new($XLS)
22 or die "Error opening .xls file: $!";
24 # hardcoded formats, this could be handled better
25 my $light_gray = $workbook->set_custom_color(63, '#eeeeee');
70 bg_color => $light_gray,
74 18, (10.5, 3) x 6, 10.5, 10.5, 3, 10.5, 3, 10.5, 3, 10.5
76 foreach (keys(%format)) {
77 my %f = (%default, %{$format{$_}});
78 $format{$_} = $workbook->add_format(%f);
79 $format{"m_$_"} = $workbook->add_format(%f); # for merged cells
80 $format{"t_$_"} = $workbook->add_format(%f, bg_color => 'yellow'); # totals
82 my $ws = $workbook->add_worksheet('taxreport');
93 foreach my $row ($table->rows()) {
96 foreach my $cell (@$row) {
97 if ($cell and ref($cell) eq 'HTML::ElementTable::DataElement') {
99 if ( $cell->as_HTML =~ /font/i ) {
100 my ($el) = $cell->content_list;
101 $f = 'size'.$el->attr('size') if $el->attr('size');
103 elsif ( $cell->as_text =~ /^\$/ ) {
106 elsif ( $cell->tag eq 'th' ) {
113 text => $cell->as_text,
115 rowspan => $cell->attr('rowspan'),
116 colspan => $cell->attr('colspan'),
125 foreach my $row (@sheet) {
127 my $t_row = 1 if($row->[0]->{'text'} eq 'Total');
128 foreach my $cell (@$row) {
130 my $f = $cell->{format};
131 if ($cell->{rowspan} > 1 or $cell->{colspan} > 1) {
132 my $range = xl_range_formula(
135 $y - 1 + ($cell->{rowspan} || 1),
137 $x - 1 + ($cell->{colspan} || 1)
139 $ws->merge_range($range, $cell->{text}, $format{"m_$f"});
142 $f = "t_$f" if $t_row;
143 $ws->write($y, $x, $cell->{text}, $format{$f});
151 for my $x (0..scalar(@widths)-1) {
152 $ws->set_column($x, $x, $widths[$x]);
157 http_header('Content-Length' => length($data));