1 package WebPAC::Output::Excel;
7 use base qw/WebPAC::Common WebPAC::Output Class::Accessor/;
8 __PACKAGE__->mk_accessors(qw(
18 use Spreadsheet::WriteExcel;
20 use Encode qw/decode/;
24 WebPAC::Output::Excel - Create binary Excel file
30 Create Excel output for export into other systems from C<csv()> constructs
31 with columns named C<A>..C<Z> (or whatever L<Spreadsheet::WriteExcel> supports)
37 my $out = new WebPAC::Output::Excel({
38 path => '/path/to/file.xls',
52 select name of variable from C<< to('csv','A',...) >> constructs used by
65 my $log = $self->_get_logger;
67 if ( ! $self->path ) {
68 $log->logwarn("need path for ", __PACKAGE__);
72 mk_base_path( $self->path );
74 $self->workbook( Spreadsheet::WriteExcel->new( $self->path ) ) ||
75 $log->logdie("can't open ", $self->path,": $!");
77 $self->worksheet( $self->workbook->add_worksheet() ) ||
78 $log->logdie("can't add_worksheet");
82 $self->filter( 'csv' ) unless $self->filter;
90 Adds one entry to database.
94 Returns number of columns added
101 my ( $id, $ds ) = @_;
103 my $log = $self->_get_logger;
104 $log->logdie("need id") unless defined $id;
105 $log->logdie("need ds") unless $ds;
107 $log->debug("id: $id ds = ",sub { dump($ds) });
113 my $hash = $self->ds_to_hash( $ds, $self->filter, disable_key_mungle => 1, single_values => 1 );
114 $log->debug("hash from ",$self->filter," = ", sub { dump( $hash ) });
116 my $worksheet = $self->worksheet || $log->logconfess("no worksheet?");
117 foreach my $col ( sort grep { /^[A-Z]/ } keys %$hash ) {
118 # FIXME internal WebPAC encoding is ISO-8859-2
119 # my $val = decode('ISO-8859-2', $hash->{$col});
120 my $val = $hash->{$col};
121 # protect data which looks like forula
122 $val = "'$val" if $val =~ m/^=/;
123 $log->debug("$col$l|$val");
124 $worksheet->write( $col . $l , $val );
128 $self->line( $l + 1 ) if $cols;
142 my $log = $self->_get_logger();
144 my $path = $self->path;
146 $self->workbook->close() ||
147 $log->logdie("can't close Excel file $path: $!");
149 $log->info("created $path ", -s $path, " bytes with ", $self->line, " rows");
156 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
158 =head1 COPYRIGHT & LICENSE
160 Copyright 2007 Dobrica Pavlinusic, All Rights Reserved.
162 This program is free software; you can redistribute it and/or modify it
163 under the same terms as Perl itself.
167 1; # End of WebPAC::Output::Excel