1 package Excel::Template;
6 use Excel::Template::Base;
7 use vars qw ($VERSION @ISA);
10 @ISA = qw( Excel::Template::Base );
21 my $self = $class->SUPER::new(@_);
23 $self->parse_xml($self->{FILENAME})
24 if defined $self->{FILENAME};
26 my @renderer_classes = ( 'Spreadsheet::WriteExcel' );
27 if (exists $self->{BIG_FILE} && $self->{BIG_FILE})
29 unshift @renderer_classes, 'Spreadsheet::WriteExcel::Big';
32 $self->{RENDERER} = undef;
33 foreach my $class (@renderer_classes)
35 (my $filename = $class) =~ s!::!/!g;
37 require "$filename.pm";
41 warn "Could not find or compile '$class'\n";
43 $self->{RENDERER} = $class;
48 defined $self->{RENDERER} ||
49 die "Could not find a renderer class. Tried:\n\t" .
50 join("\n\t", @renderer_classes) .
60 # Allow an arbitrary number of hashrefs, so long as they're the first things # into param(). Put each one onto the end, de-referenced.
61 push @_, %{shift @_} while UNIVERSAL::isa($_[0], 'HASH');
64 && die __PACKAGE__, "->param() : Odd number of parameters to param()\n";
67 $params{uc $_} = delete $params{$_} for keys %params;
68 @{$self->{PARAM_MAP}}{keys %params} = @params{keys %params};
78 my $xls = $self->{RENDERER}->new($filename)
79 || die "Cannot create XLS in '$filename': $!\n";
81 $self->_prepare_output($xls);
93 tie *XLS, 'IO::Scalar', \$output;
95 $self->write_file(\*XLS);
104 $self->parse_xml(@_);
112 my ($filename, $dirname) = fileparse($fname);
115 my $parser = XML::Parser->new(
123 my $node = Excel::Template::Factory->create_node($name, @_);
124 die "'$name' (@_) didn't make a node!\n" unless defined $node;
126 if ($name eq 'WORKBOOK')
128 push @{$self->{WORKBOOKS}}, $node;
130 elsif ($name eq 'VAR')
132 return unless @stack;
134 if (exists $stack[-1]{TXTOBJ} &&
135 $stack[-1]{TXTOBJ}->isa('TEXTOBJECT'))
137 push @{$stack[-1]{TXTOBJ}{STACK}}, $node;
143 push @{$stack[-1]{ELEMENTS}}, $node
150 return unless @stack;
152 my $parent = $stack[-1];
155 exists $parent->{TXTOBJ}
157 $parent->{TXTOBJ}->isa('TEXTOBJECT')
159 push @{$parent->{TXTOBJ}{STACK}}, @_;
164 return unless @stack;
166 pop @stack if $stack[-1]->isa(uc $_[0]);
172 my $fh = IO::File->new($fname)
173 || die "Cannot open '$fname' for reading: $!\n";
175 $parser->parse(do { local $/ = undef; <$fh> });
188 my $context = Excel::Template::Factory->create(
192 PARAM_MAP => [ $self->{PARAM_MAP} ],
195 $_->render($context) for @{$self->{WORKBOOKS}};
200 sub register { shift; Excel::Template::Factory::register(@_) }
207 Excel::Template - Excel::Template
211 First, make a template. This is an XML file, describing the layout of the
214 For example, test.xml:
217 <worksheet name="tester">
223 Now, create a small program to use it:
228 # Create the Excel template
229 my $template = Excel::Template->new(
230 filename => 'test.xml',
233 # Add a few parameters
239 $template->write_file('test.xls');
241 If everything worked, then you should have a spreadsheet in your work directory
242 that looks something like:
245 +----------------+----------------+----------------
246 1 | /home/me | /bin:/usr/bin |
247 +----------------+----------------+----------------
249 +----------------+----------------+----------------
254 This is a module used for templating Excel files. Its genesis came from the
255 need to use the same datastructure as HTML::Template, but provide Excel files
256 instead. The existing modules don't do the trick, as they require replication
257 of logic that's already been done within HTML::Template.
259 Currently, only a small subset of the planned features are supported. This is
260 meant to be a test of the waters, to see what features people actually want.
264 I do a lot of Perl/CGI for reporting purposes. In nearly every place I've been,
265 I've been asked for HTML, PDF, and Excel. HTML::Template provides the first, and
266 PDF::Template does the second pretty well. But, generating Excel was the
267 sticking point. I already had the data structure for the other templating
268 modules, but I just didn't have an easy mechanism to get that data structure
275 This creates a Excel::Template object. If passed a filename parameter, it will
276 parse the template in the given file. (You can also use the parse() method,
281 This method is exactly like HTML::Template's param() method. Although I will
282 be adding more to this section later, please see HTML::Template's description
285 =head2 parse() / parse_xml()
287 This method actually parses the template file. It can either be called
288 separately or through the new() call. It will die() if it runs into a situation
293 Create the Excel file and write it to the specified filename, if possible. (This
294 is when the actual merging of the template and the parameters occurs.)
298 It will act just like HTML::Template's output() method, returning the resultant
299 file as a stream, usually for output to the web. (This is when the actual
300 merging of the template and the parameters occurs.)
302 =head1 SUPPORTED NODES
304 This is just a list of nodes. See the other classes in this distro for more
305 details on specific parameters and the like.
307 Every node can set the ROW and COL parameters. These are the actual ROW/COL
308 values that the next CELL tag will write into.
334 None, that I know of.
338 This is currently beta-quality software. The featureset is extremely limited,
339 but I expect to be adding on to it very soon.
348 There is a mailing list at http://groups-beta.google.com/group/ExcelTemplate
352 This program is free software; you can redistribute
353 it and/or modify it under the same terms as Perl itself.
355 The full text of the license can be found in the
356 LICENSE file included with this module.
360 perl(1), HTML::Template, Spreadsheet::WriteExcel.