nop has asked for the wisdom of the Perl Monks concerning the following question:
Some driver code:package Excel; use strict; use Win32::OLE; use File::Basename; use FindBin qw($Bin); my $ex; my $FIELD_SEP = "\t"; my $RECORD_SEP = "\n"; sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = {}; bless ($self, $class); $self->init(@_); return $self; } sub init { my ($self, $fname) = @_; unlink ($fname); my ($name,$dir,$type) = fileparse($fname, '\..*'); if ($type ne ".xls") {die "invalid name $fname: must have .xls typ +e";} if ($dir eq ".\\") {$dir=$Bin . "/";} $self->{fname} = $dir . $name . ".xls"; eval {$ex = Win32::OLE->GetActiveObject('Excel.Application')}; die "Excel not installed" if $@; unless (defined $ex) { $ex = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;}) or die "Oops, cannot start Excel"; $ex->LetProperty("SheetsInNewWorkbook",1); } $self->{book} = $ex->Workbooks->Add or die "bad add"; } sub print { my ($self, $sheet, $what) = @_; if (!$self->{$sheet}{sheet}) { $self->{$sheet}{sheet} = $self->{book}->Worksheets->Add; $self->{$sheet}{sheet}->{Name} = $sheet; $self->{$sheet}{row} = 1; $self->{$sheet}{col} = 1; } my $row = $self->{$sheet}{row}; my $col = $self->{$sheet}{col}; my $NEWLINE = '_NEWLINE_'; $what =~ s/$RECORD_SEP/$FIELD_SEP$NEWLINE$FIELD_SEP/g; for my $cell (split(/\t/,$what)) { if ($cell eq $NEWLINE) { $row++; $col=1; next; } $self->{$sheet}{sheet}->Cells($row,$col)->{Value} = $cell; $col++; } $self->{$sheet}{row} = $row; $self->{$sheet}{col} = $col; } sub DESTROY { my $self = shift; $self->{book}->SaveAs({Filename=>$self->{fname}}); $self->{book}->Close({SaveChanges=>1, FileName=>$self->{fname}}); } 1;
use Excel; my $s = Excel->new("here.xls"); $s->print("one", "Hello World\nThis\tis\ta\ttest\n"); $s->print("one", join("\t", 1 ..10)); $s->print("one", "bye\n"); $s->print("one", join("\n",3 .. 5) . scalar(localtime)); $s->print("two", join("\n",13 .. 25) . scalar(localtime)); $s->print("eight", join("\n",13 .. 20) . scalar(localtime)); my $t = Excel->new("c:/temp/there.xls"); $t->print("two", join("\n",13 .. 25) . scalar(localtime));
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(jcwren) RE: Win32::OLE and Excel
by jcwren (Prior) on Sep 18, 2000 at 20:25 UTC | |
|
(Guildenstern) Re: Win32::OLE and Excel
by Guildenstern (Deacon) on Sep 18, 2000 at 20:06 UTC | |
by nop (Hermit) on Sep 18, 2000 at 20:22 UTC | |
by Guildenstern (Deacon) on Sep 18, 2000 at 20:54 UTC | |
|
Re: Win32::OLE and Excel
by vladdrak (Monk) on Sep 19, 2000 at 03:20 UTC | |
|
Re: Win32::OLE and Excel
by Anonymous Monk on Sep 20, 2000 at 02:18 UTC |