Skip to content

Instantly share code, notes, and snippets.

@yasu47b
Last active March 16, 2017 05:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yasu47b/b790b9a84211c59c90b345e69ef6189e to your computer and use it in GitHub Desktop.
Save yasu47b/b790b9a84211c59c90b345e69ef6189e to your computer and use it in GitHub Desktop.
Mojolciious Excel Writer sample
```
#!/usr/bin/env perl
use Mojolicious::Lite;
use Excel::Writer::XLSX;
use Cwd;
app->static->paths->[0] = getcwd;
# Documentation browser under "/perldoc"
plugin 'PODRenderer';
get '/' => sub {
my $c = shift;
my ($workbook, $formats, $filename) = _create_xlsx();
my $worksheet = $workbook->add_worksheet('test');
my @data = (
[1 .. 10],
[-10 .. 1],
[0 .. 10]
);
say $c->dumper(\@data);
for my $row (0 .. $#data) {
for my $col (0 .. $#{$data[$row]}) {
my $value = $data[$row]->[$col];
$worksheet->write($row, $col,$value, $formats->{num_border_bottom});
}
}
$workbook->close;
$c->res->headers->content_disposition("attachment; filename=test.xlsx");
$c->reply->static('./'.$filename)
};
app->start;
sub _create_xlsx {
my $filename = '';
my @chars = (0..9,'A'..'Z','a'..'z');
$filename .= $chars[int(rand($#chars+1))] for(0 .. 32);
$filename .= '.xlsx';
my $dir = './';
my $workbook = Excel::Writer::XLSX->new($dir.$filename);
unless(defined $workbook){
die "Can't locate workbook ".$dir.$filename."\n";
}
my $formats = {
num_border_bottom => $workbook->add_format(
size => 9,
align => 'right',
bg_color => 'white',
border_color => 55,
bottom_color => 'black',
top => 1,
left => 1,
right => 1,
bottom => 1,
num_format => '#,##0',
)
};
return ($workbook, $formats,$dir . $filename);
}
__DATA__
@@ index.html.ep
% layout 'default';
% title 'Welcome';
<h1>Welcome to the Mojolicious real-time web framework!</h1>
To learn more, you can browse through the documentation
<%= link_to 'here' => '/perldoc' %>.
@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
<head><title><%= title %></title></head>
<body><%= content %></body>
</html>
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment