| 1 | package App::AutoCRUD::View::Xlsx; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use Moose; | ||
| 8 | extends 'App::AutoCRUD::View'; | ||
| 9 | |||
| 10 | use Excel::Writer::XLSX; | ||
| 11 | use namespace::clean -except => 'meta'; | ||
| 12 | |||
| 13 | sub render { | ||
| 14 | my ($self, $data, $context) = @_; | ||
| 15 | |||
| 16 | # pseudo-filehandle to memory buffer | ||
| 17 | open my $fh, '>', \my $str | ||
| 18 | or die "Failed to open filehandle: $!"; | ||
| 19 | |||
| 20 | # open excel file in memory | ||
| 21 | my $workbook = Excel::Writer::XLSX->new($fh); | ||
| 22 | my $worksheet = $workbook->add_worksheet(); | ||
| 23 | |||
| 24 | # global Excel settings | ||
| 25 | my $title_fmt = $workbook->add_format(bold => 1, size => 13); | ||
| 26 | my $sql_fmt = $workbook->add_format(size => 9); | ||
| 27 | my $colgroup_fmt = $workbook->add_format(bold => 1, align => 'center', | ||
| 28 | border => 1, border_color => 'blue', | ||
| 29 | ); | ||
| 30 | $worksheet->outline_settings(1, # visible | ||
| 31 | 0, # symbols_below, | ||
| 32 | 0, # symbols_right, | ||
| 33 | 1, # auto_style | ||
| 34 | ); | ||
| 35 | |||
| 36 | # initial Excel rows (title and SQL request) | ||
| 37 | my $table = $data->{table}; | ||
| 38 | $worksheet->write(0, 0, "Selection from $table", $title_fmt); | ||
| 39 | $worksheet->write(1, 0, $data->{criteria}, $sql_fmt); | ||
| 40 | |||
| 41 | # handling column groups (header row and Excel outlines) | ||
| 42 | my @headers; | ||
| 43 | my $colgroup_row = 2; | ||
| 44 | my $first_col = 0; | ||
| 45 | my $last_col; | ||
| 46 | foreach my $colgroup (@{$data->{colgroups}}) { | ||
| 47 | my $cols = $colgroup->{columns}; | ||
| 48 | push @headers, map {$_->{COLUMN_NAME}} @$cols; | ||
| 49 | $last_col = $first_col + @$cols - 1; | ||
| 50 | if (@$cols > 1) { # this group contains several columns | ||
| 51 | # create a merged cell containing the colgroup name | ||
| 52 | $worksheet->merge_range($colgroup_row, $first_col, | ||
| 53 | $colgroup_row, $last_col, | ||
| 54 | $colgroup->{name}, $colgroup_fmt); | ||
| 55 | # create an outline group | ||
| 56 | $worksheet->set_column($first_col+1, $last_col, undef, undef, undef, | ||
| 57 | 1, # outline level 1 | ||
| 58 | ); | ||
| 59 | } | ||
| 60 | else { # this group contains just one column | ||
| 61 | # just write the colgroup name into a single cell | ||
| 62 | $worksheet->write($colgroup_row, $first_col, | ||
| 63 | $colgroup->{name}, $colgroup_fmt); | ||
| 64 | } | ||
| 65 | # prepare $first_col for next colgroup iteration | ||
| 66 | $first_col = $last_col + 1; | ||
| 67 | } | ||
| 68 | |||
| 69 | # generate data table | ||
| 70 | my $rows = $data->{rows}; | ||
| 71 | my $n_rows = @$rows; | ||
| 72 | my $n_cols = @headers; | ||
| 73 | $worksheet->add_table(3, 0, $n_rows + 3, $n_cols-1, { | ||
| 74 | data => [ map {[@{$_}{@headers}]} @$rows ], | ||
| 75 | columns => [ map { {header => $_}} @headers ], | ||
| 76 | autofilter => 1, | ||
| 77 | }); | ||
| 78 | $worksheet->freeze_panes(4, 0); | ||
| 79 | |||
| 80 | # finalize the workbook | ||
| 81 | $workbook->close(); | ||
| 82 | |||
| 83 | # return Plack response | ||
| 84 | my @http_headers = ( | ||
| 85 | 'Content-type' => 'application/xlsx', | ||
| 86 | 'Content-disposition' => qq{attachment; filename="$table.xlsx"}, | ||
| 87 | ); | ||
| 88 | return [200, \@http_headers, [$str] ]; | ||
| 89 | } | ||
| 90 | |||
| 91 | |||
| 92 | 1; | ||
| 93 | |||
| 94 | |||
| 95 | __END__ |