1 package Excel::Template;
6 use Excel::Template::Base;
7 use vars qw ($VERSION @ISA);
10 @ISA = qw( Excel::Template::Base );
20 my $self = $class->SUPER::new(@_);
22 $self->parse_xml($self->{FILENAME})
23 if defined $self->{FILENAME};
25 my @renderer_classes = ( 'Spreadsheet::WriteExcel' );
26 if (exists $self->{BIG_FILE} && $self->{BIG_FILE})
28 unshift @renderer_classes, 'Spreadsheet::WriteExcel::Big';
31 $self->{RENDERER} = undef;
32 foreach my $class (@renderer_classes)
34 (my $filename = $class) =~ s!::!/!g;
36 require "$filename.pm";
40 warn "Could not find or compile '$class'\n";
42 $self->{RENDERER} = $class;
47 defined $self->{RENDERER} ||
48 die "Could not find a renderer class. Tried:\n\t" .
49 join("\n\t", @renderer_classes) .
59 # Allow an arbitrary number of hashrefs, so long as they're the first things # into param(). Put each one onto the end, de-referenced.
60 push @_, %{shift @_} while UNIVERSAL::isa($_[0], 'HASH');
63 && die __PACKAGE__, "->param() : Odd number of parameters to param()\n";
66 $params{uc $_} = delete $params{$_} for keys %params;
67 @{$self->{PARAM_MAP}}{keys %params} = @params{keys %params};
77 my $xls = $self->{RENDERER}->new($filename)
78 || die "Cannot create XLS in '$filename': $!\n";
80 $self->_prepare_output($xls);
92 tie *XLS, 'IO::Scalar', \$output;
94 $self->write_file(\*XLS);
104 my ($filename, $dirname) = fileparse($fname);
107 my $parser = XML::Parser->new(
115 my $node = Excel::Template::Factory->create_node($name, @_);
116 die "'$name' (@_) didn't make a node!\n" unless defined $node;
118 if ( $node->isa( 'WORKBOOK' ) )
120 push @{$self->{WORKBOOKS}}, $node;
122 elsif ( $node->is_embedded )
124 return unless @stack;
126 if (exists $stack[-1]{TXTOBJ} &&
127 $stack[-1]{TXTOBJ}->isa('TEXTOBJECT'))
129 push @{$stack[-1]{TXTOBJ}{STACK}}, $node;
135 push @{$stack[-1]{ELEMENTS}}, $node
142 return unless @stack;
144 my $parent = $stack[-1];
147 exists $parent->{TXTOBJ}
149 $parent->{TXTOBJ}->isa('TEXTOBJECT')
151 push @{$parent->{TXTOBJ}{STACK}}, @_;
156 return unless @stack;
158 pop @stack if $stack[-1]->isa(uc $_[0]);
164 open( INFILE, "<$fname" )
165 || die "Cannot open '$fname' for reading: $!\n";
167 $parser->parse(do { local $/ = undef; <INFILE> });
174 *parse = \&parse_xml;
181 my $context = Excel::Template::Factory->create(
185 PARAM_MAP => [ $self->{PARAM_MAP} ],
188 $_->render($context) for @{$self->{WORKBOOKS}};
193 sub register { shift; Excel::Template::Factory::register(@_) }
200 Excel::Template - Excel::Template
204 First, make a template. This is an XML file, describing the layout of the
207 For example, test.xml:
210 <worksheet name="tester">
216 Now, create a small program to use it:
221 # Create the Excel template
222 my $template = Excel::Template->new(
223 filename => 'test.xml',
226 # Add a few parameters
232 $template->write_file('test.xls');
234 If everything worked, then you should have a spreadsheet in your work directory
235 that looks something like:
238 +----------------+----------------+----------------
239 1 | /home/me | /bin:/usr/bin |
240 +----------------+----------------+----------------
242 +----------------+----------------+----------------
247 This is a module used for templating Excel files. Its genesis came from the
248 need to use the same datastructure as HTML::Template, but provide Excel files
249 instead. The existing modules don't do the trick, as they require replication
250 of logic that's already been done within HTML::Template.
252 Currently, only a small subset of the planned features are supported. This is
253 meant to be a test of the waters, to see what features people actually want.
257 I do a lot of Perl/CGI for reporting purposes. In nearly every place I've been,
258 I've been asked for HTML, PDF, and Excel. HTML::Template provides the first, and
259 PDF::Template does the second pretty well. But, generating Excel was the
260 sticking point. I already had the data structure for the other templating
261 modules, but I just didn't have an easy mechanism to get that data structure
268 This creates a Excel::Template object. If passed a filename parameter, it will
269 parse the template in the given file. (You can also use the parse() method,
274 This method is exactly like HTML::Template's param() method. Although I will
275 be adding more to this section later, please see HTML::Template's description
278 =head2 parse() / parse_xml()
280 This method actually parses the template file. It can either be called
281 separately or through the new() call. It will die() if it runs into a situation
286 Create the Excel file and write it to the specified filename, if possible. (This
287 is when the actual merging of the template and the parameters occurs.)
291 It will act just like HTML::Template's output() method, returning the resultant
292 file as a stream, usually for output to the web. (This is when the actual
293 merging of the template and the parameters occurs.)
295 =head1 SUPPORTED NODES
297 This is just a list of nodes. See the other classes in this distro for more
298 details on specific parameters and the like.
300 Every node can set the ROW and COL parameters. These are the actual ROW/COL
301 values that the next CELL tag will write into.
327 None, that I know of.
331 This is currently beta-quality software. The featureset is extremely limited,
332 but I expect to be adding on to it very soon.
341 There is a mailing list at http://groups-beta.google.com/group/ExcelTemplate
347 =item * Finishing formats
349 =item * Fixing several bugs in worksheet naming
355 This program is free software; you can redistribute
356 it and/or modify it under the same terms as Perl itself.
358 The full text of the license can be found in the
359 LICENSE file included with this module.
363 perl(1), HTML::Template, Spreadsheet::WriteExcel.