Difference between revisions of "Contribute: Plugins/ExportPluginsExcel"
m (→Excel.pm) |
(→In More Detail: Initial code copy.) |
||
Line 75: | Line 75: | ||
= In More Detail = | = In More Detail = | ||
+ | <pre> | ||
+ | package EPrints::Plugin::Export::MyPlugins::Excel; | ||
+ | |||
+ | @ISA = ("EPrints::Plugin::Export"); | ||
+ | |||
+ | use strict; | ||
+ | use Spreadsheet::WriteExcel; | ||
+ | use IO::File; | ||
+ | use IO::String; | ||
+ | |||
+ | sub new | ||
+ | { | ||
+ | my ($class, %opts) = @_; | ||
+ | my $self = $class->SUPER::new(%opts); | ||
+ | |||
+ | $self->{name} = "Excel"; | ||
+ | $self->{accept} = ['list/eprint']; | ||
+ | $self->{visible} = "all"; | ||
+ | $self->{suffix} = ".xls"; | ||
+ | $self->{mimetype} = "application/vnd.ms-excel"; | ||
+ | |||
+ | return $self; | ||
+ | } | ||
+ | |||
+ | sub output_list | ||
+ | { | ||
+ | my ($plugin, %opts) = @_; | ||
+ | my $workbook; | ||
+ | |||
+ | my $output; | ||
+ | my $FH = IO::String->new(\$output); | ||
+ | |||
+ | if (defined $opts{"fh"}) | ||
+ | { | ||
+ | $workbook = Spreadsheet::WriteExcel->new(\*{$opts{"fh"}}); | ||
+ | die("Unable to create spreadsheet: $!")unless defined $workbook; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | $workbook = Spreadsheet::WriteExcel->new($FH); | ||
+ | die("Unable to create spreadsheet: $!")unless defined $workbook; | ||
+ | } | ||
+ | |||
+ | foreach my $dataobj ($opts{"list"}->get_records) | ||
+ | { | ||
+ | my $worksheet = $workbook->add_worksheet(); | ||
+ | my $i = 0; | ||
+ | foreach my $field ($dataobj->get_dataset->get_fields) | ||
+ | { | ||
+ | my $name = $field->get_name; | ||
+ | next unless $dataobj->exists_and_set($name); | ||
+ | $worksheet->write($i, 0, $name); | ||
+ | $worksheet->write_string($i, 1, $dataobj->get_value($name)); | ||
+ | $i++; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | $workbook->close; | ||
+ | |||
+ | if (defined $opts{"fh"}) | ||
+ | { | ||
+ | return undef; | ||
+ | } | ||
+ | |||
+ | return $output; | ||
+ | } | ||
+ | |||
+ | 1; | ||
+ | </pre> | ||
= Testing Your Plugin = | = Testing Your Plugin = | ||
Restart your webserver and test the plugin as in [[User:Tom/Export_Plugins/HTML | the previous tutorial]]. | Restart your webserver and test the plugin as in [[User:Tom/Export_Plugins/HTML | the previous tutorial]]. |
Revision as of 10:29, 30 August 2007
Export Plugin Tutorial 4: Excel
Excel.pm
package EPrints::Plugin::Export::MyPlugins::Excel; @ISA = ("EPrints::Plugin::Export"); use strict; use Spreadsheet::WriteExcel; use IO::File; use IO::String; sub new { my ($class, %opts) = @_; my $self = $class->SUPER::new(%opts); $self->{name} = "Excel"; $self->{accept} = ['list/eprint']; $self->{visible} = "all"; $self->{suffix} = ".xls"; $self->{mimetype} = "application/vnd.ms-excel"; return $self; } sub output_list { my ($plugin, %opts) = @_; my $workbook; my $output; my $FH = IO::String->new(\$output); if (defined $opts{"fh"}) { $workbook = Spreadsheet::WriteExcel->new(\*{$opts{"fh"}}); die("Unable to create spreadsheet: $!")unless defined $workbook; } else { $workbook = Spreadsheet::WriteExcel->new($FH); die("Unable to create spreadsheet: $!")unless defined $workbook; } foreach my $dataobj ($opts{"list"}->get_records) { my $worksheet = $workbook->add_worksheet(); my $i = 0; foreach my $field ($dataobj->get_dataset->get_fields) { my $name = $field->get_name; next unless $dataobj->exists_and_set($name); $worksheet->write($i, 0, $name); $worksheet->write_string($i, 1, $dataobj->get_value($name)); $i++; } } $workbook->close; if (defined $opts{"fh"}) { return undef; } return $output; } 1;
In More Detail
package EPrints::Plugin::Export::MyPlugins::Excel; @ISA = ("EPrints::Plugin::Export"); use strict; use Spreadsheet::WriteExcel; use IO::File; use IO::String; sub new { my ($class, %opts) = @_; my $self = $class->SUPER::new(%opts); $self->{name} = "Excel"; $self->{accept} = ['list/eprint']; $self->{visible} = "all"; $self->{suffix} = ".xls"; $self->{mimetype} = "application/vnd.ms-excel"; return $self; } sub output_list { my ($plugin, %opts) = @_; my $workbook; my $output; my $FH = IO::String->new(\$output); if (defined $opts{"fh"}) { $workbook = Spreadsheet::WriteExcel->new(\*{$opts{"fh"}}); die("Unable to create spreadsheet: $!")unless defined $workbook; } else { $workbook = Spreadsheet::WriteExcel->new($FH); die("Unable to create spreadsheet: $!")unless defined $workbook; } foreach my $dataobj ($opts{"list"}->get_records) { my $worksheet = $workbook->add_worksheet(); my $i = 0; foreach my $field ($dataobj->get_dataset->get_fields) { my $name = $field->get_name; next unless $dataobj->exists_and_set($name); $worksheet->write($i, 0, $name); $worksheet->write_string($i, 1, $dataobj->get_value($name)); $i++; } } $workbook->close; if (defined $opts{"fh"}) { return undef; } return $output; } 1;
Testing Your Plugin
Restart your webserver and test the plugin as in the previous tutorial.