4 use Spreadsheet::WriteExcel;
5 use Spreadsheet::WriteExcel::Utility 'xl_range_formula';
7 use HTML::TableExtract 'tree';
10 my $XLS = new IO::Scalar \$data;
11 my $workbook = Spreadsheet::WriteExcel->new($XLS)
12 or die "Error opening .xls file: $!";
14 # hardcoded formats, this could be handled better
15 my $light_gray = $workbook->set_custom_color(63, '#eeeeee');
60 bg_color => $light_gray,
64 18, (10.5, 3) x 6, 10.5, 10.5, 3, 10.5, 3, 10.5, 3, 10.5
66 foreach (keys(%format)) {
67 my %f = (%default, %{$format{$_}});
68 $format{$_} = $workbook->add_format(%f);
69 $format{"m_$_"} = $workbook->add_format(%f); # for merged cells
70 $format{"t_$_"} = $workbook->add_format(%f, bg_color => 'yellow'); # totals
72 my $ws = $workbook->add_worksheet('taxreport');
74 my $htmldoc = include('report_tax.cgi');
76 my ($title) = ($htmldoc =~ /<title>\s*(.*)\s*<\/title>/i);
78 # attribs option: how to locate the table? It's the only one with class="grid".
79 my $te = HTML::TableExtract->new(attribs => {class => 'grid'});
81 my $table = $te->first_table_found;
92 foreach my $row ($table->rows()) {
95 foreach my $cell (@$row) {
96 if ($cell and ref($cell) eq 'HTML::ElementTable::DataElement') {
98 if ( $cell->as_HTML =~ /font/i ) {
99 my ($el) = $cell->content_list;
100 $f = 'size'.$el->attr('size') if $el->attr('size');
102 elsif ( $cell->as_text =~ /^\$/ ) {
105 elsif ( $cell->tag eq 'th' ) {
112 text => $cell->as_text,
114 rowspan => $cell->attr('rowspan'),
115 colspan => $cell->attr('colspan'),
124 foreach my $row (@sheet) {
126 my $t_row = 1 if($row->[0]->{'text'} eq 'Total');
127 foreach my $cell (@$row) {
129 my $f = $cell->{format};
130 if ($cell->{rowspan} > 1 or $cell->{colspan} > 1) {
131 my $range = xl_range_formula(
134 $y - 1 + ($cell->{rowspan} || 1),
136 $x - 1 + ($cell->{colspan} || 1)
138 $ws->merge_range($range, $cell->{text}, $format{"m_$f"});
141 $f = "t_$f" if $t_row;
142 $ws->write($y, $x, $cell->{text}, $format{$f});
150 for my $x (0..scalar(@widths)-1) {
151 $ws->set_column($x, $x, $widths[$x]);
156 http_header('Content-Type' => 'application/vnd.ms-excel');
157 http_header('Content-Disposition' => 'attachment;filename="report_tax.xls"');