5 my $XLS = new IO::Scalar \$data;
6 my $workbook = Spreadsheet::WriteExcel->new($XLS)
7 or die "Error opening .xls file: $!";
9 # hardcoded formats, this could be handled better
10 my $light_gray = $workbook->set_custom_color(63, '#eeeeee');
55 bg_color => $light_gray,
59 18, (10.5, 3) x 6, 10.5, 10.5, 3, 10.5, 3, 10.5, 3, 10.5
61 foreach (keys(%format)) {
62 my %f = (%default, %{$format{$_}});
63 $format{$_} = $workbook->add_format(%f);
64 $format{"m_$_"} = $workbook->add_format(%f); # for merged cells
65 $format{"t_$_"} = $workbook->add_format(%f, bg_color => 'yellow'); # totals
67 my $ws = $workbook->add_worksheet('taxreport');
69 my $htmldoc = include('report_tax.cgi');
71 my ($title) = ($htmldoc =~ /<title>\s*(.*)\s*<\/title>/i);
73 # attribs option: how to locate the table? It's the only one with class="grid".
74 my $te = HTML::TableExtract->new(attribs => {class => 'grid'});
76 my $table = $te->first_table_found;
87 foreach my $row ($table->rows()) {
90 foreach my $cell (@$row) {
91 if ($cell and ref($cell) eq 'HTML::ElementTable::DataElement') {
93 if ( $cell->as_HTML =~ /font/i ) {
94 my ($el) = $cell->content_list;
95 $f = 'size'.$el->attr('size') if $el->attr('size');
97 elsif ( $cell->as_text =~ /^\$/ ) {
100 elsif ( $cell->tag eq 'th' ) {
107 text => $cell->as_text,
109 rowspan => $cell->attr('rowspan'),
110 colspan => $cell->attr('colspan'),
119 foreach my $row (@sheet) {
121 my $t_row = 1 if($row->[0]->{'text'} eq 'Total');
122 foreach my $cell (@$row) {
124 my $f = $cell->{format};
125 if ($cell->{rowspan} > 1 or $cell->{colspan} > 1) {
126 my $range = xl_range_formula(
129 $y - 1 + ($cell->{rowspan} || 1),
131 $x - 1 + ($cell->{colspan} || 1)
133 $ws->merge_range($range, $cell->{text}, $format{"m_$f"});
136 $f = "t_$f" if $t_row;
137 $ws->write($y, $x, $cell->{text}, $format{$f});
145 for my $x (0..scalar(@widths)-1) {
146 $ws->set_column($x, $x, $widths[$x]);
151 http_header('Content-Type' => 'application/vnd.ms-excel');
152 http_header('Content-Disposition' => 'attachment;filename="report_tax.xls"');