Subject: | [PATCH] Add support for getting excel data w/o printing to STDOUT |
I'd like to be able to get the Excel data in a scalar instead of always printing to STDOUT. Here's a patch that adds a output_as_string() method which accomplishes it. It does add the requirement of IO::Scalar, but I believe that is included in later perl distributions. I don't have a more recent perl than 5.005 handy so this patch is mostly untested. :-) That said, I believe it should work.
diff -u Spreadsheet-SimpleExcel.pm.orig Spreadsheet-SimpleExcel.pm
--- Spreadsheet-SimpleExcel.pm.orig Tue Nov 2 13:56:51 2004
+++ Spreadsheet-SimpleExcel.pm Tue Nov 2 14:09:49 2004
@@ -4,6 +4,7 @@
use strict;
use warnings;
use Spreadsheet::WriteExcel;
+use IO::Scalar;
require Exporter;
@@ -101,11 +102,25 @@
return 1;
}# end _is_numeric
-sub output{
+sub output_as_string{
+ my ($self) = @_;
+ $self->_make_excel;
+}
+
+sub output {
+ my ($self) = @_;
+ print "Content-type: ".$self->{type}."\n\n";
+ my $xls = $self->_make_excel;
+ print $$xls;
+}
+
+sub _make_excel{
my ($self) = @_;
- print "Content-type: ".$self->{type}."\n\n";
- my $EXCEL = new Spreadsheet::WriteExcel(\*STDOUT);
+ my $output;
+ tie(*XLS, 'IO::Scalar', \$output);
+ my $EXCEL = Spreadsheet::WriteExcel->new(\*XLS)
+ or die("Error creating spreadsheet object");
foreach my $worksheet(@{$self->{worksheets}}){
my $sheet = $EXCEL->addworksheet($worksheet->[0]);
@@ -126,6 +141,8 @@
}
}
$EXCEL->close();
+
+ return $output;
}# end output
@@ -185,6 +202,8 @@
$excel2->add_row('NAME',[qw/new row/]);
$excel2->output();
+ # OR
+ my $xls = $excel2->output_as_string();
=head1 DESCRIPTION
@@ -259,9 +278,15 @@
prints the worksheet to the STDOUT.
+=head2 output_as_string
+
+ my $xls = $excel2->output_as_string();
+
+returns the excel data as a scalar reference
+
=head1 DEPENDENCIES
-This module requires Spreadsheet::WriteExcel
+This module requires Spreadsheet::WriteExcel and IO::Scalar
=head1 BUGS