File Coverage

blib/lib/Catalyst/View/XLSX.pm
Criterion Covered Total %
statement 47 49 95.9
branch 3 8 37.5
condition 2 5 40.0
subroutine 9 9 100.0
pod 2 2 100.0
total 63 73 86.3


line stmt bran cond sub pod time code
1             package Catalyst::View::XLSX;
2              
3 2     2   2354028 use Moose;
  2         505152  
  2         20  
4             extends 'Catalyst::View';
5              
6             our $VERSION = '1.2';
7              
8 2     2   17693 use File::Temp;
  2         21085  
  2         138  
9 2     2   287 use URI::Escape;
  2         1269  
  2         100  
10 2     2   245 use Path::Class;
  2         22390  
  2         122  
11 2     2   18 use File::Spec;
  2         8  
  2         55  
12 2     2   2229 use Excel::Writer::XLSX;
  2         367983  
  2         1351  
13              
14             has 'stash_key' => (
15             is => 'rw',
16             isa => 'Str',
17             lazy => 1,
18             default => sub { 'xlsx' }
19             );
20              
21             has 'tmpdir' => (
22             is => 'rw',
23             isa => 'Str',
24             lazy => 1,
25             default => sub { File::Spec->tmpdir() }
26             );
27              
28             has 'filename' => (
29             is => 'rw',
30             isa => 'Str',
31             lazy => 1,
32             default => sub { 'output.xlsx' }
33             );
34              
35             # ========================================================================== #
36             # Process to Respond Excel File
37             # ========================================================================== #
38             sub process {
39 4     4 1 105970 my ( $self, $c ) = @_;
40              
41 4         18 my $xlsx = $c->stash->{ $self->stash_key };
42 4         22 my $content = $self->render($c, $xlsx);
43 4   50     1194 my $disposition = $xlsx->{disposition} || 'inline';
44              
45 4   33     28 my $filename = uri_escape_utf8( $xlsx->{filename} || $self->filename );
46 4         140 $c->res->header(
47             'Content-Disposition' => "$disposition; filename=$filename",
48             'Content-type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
49             );
50 4         1387 $c->res->body($content);
51             }
52              
53             # ========================================================================== #
54             # This method will create and return Excel Sheet content from the stashed data
55             # ========================================================================== #
56             sub render {
57 4     4 1 13 my ( $self, $c, $args ) = @_;
58              
59 4 50       19 if ( $args->{file} ) {
60             #Respond the file content
61 0 0       0 die "XLSX File Error : Invalid file ". $args->{file} unless -f $args->{file};
62              
63 0         0 return $self->_get_content($args->{file});
64             }
65              
66              
67             #Generate a XLSX file using the data
68              
69             # Create a temporary file
70 4         126 my $temp = File::Temp->new(
71             DIR => $self->tmpdir,
72             SUFFIX => '.xlsx',
73             UNLINK => 1,
74             );
75 4         1930 binmode $temp, ':utf8';
76 4         17 my $xlsxfn = $temp->filename;
77 4         57 my $workbook = Excel::Writer::XLSX->new( $xlsxfn );
78 4 50       1979 die "Problems creating new Excel file: $!" unless defined $workbook;
79 4         20 my $worksheet = $workbook->add_worksheet();
80 4         1398 my $format = $workbook->add_format();
81              
82 4         193 my $data = $args->{data};
83 4         12 for (@$data) {
84 12         654 my $cell_format = $format;
85 12 50       39 $cell_format->set_format_properties(%{$_->{format}}) if $_->{format};
  12         75  
86 12         2039 $worksheet->write( $_->{row},$_->{col},$_->{data},$cell_format,$_->{value} );
87             }
88 4         347 $workbook->close();
89 4         107634 return $self->_get_content($xlsxfn);
90             }
91              
92             # ========================================================================== #
93             # This method will return content
94             # ========================================================================== #
95             sub _get_content {
96 4     4   15 my ($self,$file) = @_;
97             # Read the output and return it
98 4         44 my $xlsxc = Path::Class::File->new($file);
99 4         465 my $content = $xlsxc->slurp();
100 4         868 $xlsxc->remove();
101 4         402 return $content;
102              
103             }
104              
105             1;
106              
107              
108             __END__
109             =pod
110              
111             =head1 NAME
112              
113             Catalyst::View::XLSX - Catalyst View for Microsoft Excel file
114              
115             =head1 VERSION
116              
117             version 1.2
118              
119             =head1 SYNOPSIS
120              
121             # Create MyApp::View::XLSX using the helper:
122              
123             `script/create.pl view XLSX XLSX`
124              
125             In your controller
126              
127             package MyApp::Controller::MyController;
128             use Moose;
129             use namespace::autoclean;
130              
131             BEGIN { extends 'Catalyst::Controller' }
132              
133             sub download_excel : Local :Args(0) {
134             my ( $self, $c ) = @_;
135              
136             my $format = {
137             font => 'Times New Roman',
138             size => '15',
139             color => 'Black',
140             bold => 1,
141             italic => 0,
142             underline => 0,
143             font_strikeout => 0,
144             font_script => 0,
145             font_outline => 0,
146             font_shadow => 0,
147             num_format => '0.00'
148             };
149              
150             my $xlsx_data = {
151             data => [
152             {
153             row => 0,
154             col => 0,
155             data => 10,
156             format => $format,
157             value => '10'
158             },
159             {
160             row => 0,
161             col => 1,
162             data => 20,
163             format => $format,
164             value => '20'
165             },
166             {
167             row => 0,
168             col => 2,
169             data => '=SUM(A1:B1)',
170             format => $format,
171             value => '30'
172             }
173             ],
174             filename => "ExcelFile.xlsx"
175             };
176            
177             $c->stash(xlsx => $xlsx_data, current_view => 'XLSX');
178             }
179              
180             1;
181              
182             =head1 SUMMARY
183              
184             This Catalyst::View::XLSX provides a Catalyst view that generates Microsoft Excel (.xlsx) files.
185              
186             =head1 DESCRIPTION
187              
188             This is a very simple module which uses few methods of L<Excel::Writer::XLSX> and creates an Excel file based on the stashed parameters. It also respond the file that has been readily available.
189              
190             =head2 STASH PARAMETERS
191              
192              
193             $c->stash->{xlsx} = {
194             data => [
195             { row => 0, col => 0, data => 'Hey! Look at me. I am A1', format => undef, value => undef },
196             { row => 0, col => 1, data => 'People call me as B1', format => undef, value => undef }
197             ],
198             filename => 'ExcelFile.xlsx'
199             };
200              
201             #row,col -> represents the position on the Excel sheet
202             #data -> represents the content of the field
203             #value -> (OPTIONAL) it will hold the actual value when data has formula
204             #format -> (OPTIONAL) format of the cell, supports the following properties
205            
206             #font => 'Times New Roman',
207             #size => '15',
208             #color => 'Black',
209             #bold => 1,
210             #italic => 0,
211             #underline => 0,
212             #font_strikeout => 0,
213             #font_script => 0,
214             #font_outline => 0,
215             #font_shadow => 0,
216             #num_format => '0.00'
217              
218             #Please refer L<Excel::Writer::XLSX> for more properties.
219              
220             or
221              
222             $c->stash->{xlsx} = {
223             file => '/opt/git/Files/GeneratedRank.xlsx',
224             filename => 'Resulst.xlsx'
225             };
226              
227              
228             =head2 METHODS
229              
230             =head3 process
231              
232             This will respond the Excel file with Content-Type `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet`
233            
234             =head3 render
235              
236             This will generate the Excel file based on the stashed parameters using L<Excel::Writer::XLSX> module.
237            
238             =head1 REPOSITORY
239              
240             L<https://github.com/Virendrabaskar/Catalyst-View-XLSX>
241              
242             =head1 SEE ALSO
243              
244             =over 4
245              
246             =item *
247              
248             L<Excel::Writer::XLSX>
249              
250             =item *
251              
252             L<Catalyst::View>
253              
254             =item *
255              
256             L<Catalyst>
257              
258             =back
259              
260             =head1 AUTHOR
261              
262             Baskar Nallathambi <baskarmusiri@gmail.com>
263              
264             =head1 COPYRIGHT AND LICENSE
265              
266             This is free module.You can do anything to this module under
267             the same terms as the Perl 5 programming language system itself.
268              
269             =cut