I often use perl to write to tab-delimited files named with ".xls" extensions. Under Windows, you can click on these files and they open in Excel. This isn't a perfect solution: they're really just text files so Excel complains when you try to save or close them later, and you can't put data on multiple "tabs" on the worksheet.

I've tried to build a module using OLE to send Perl output to Excel in a cleaner fashion (code below). As with the text file approach, "\t" delimit fields (cols) and "\n" delimit records (rows). The module seems to work... more or less. One strange thing is that the resulting files can be opened OK from within Excel, but you can't click on them from the desktop. If you do, under NT they open but yield a mangled display.

Any ideas on why the excel files that result from this code don't click open properly from the desktop?

Thanks!

The module:
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;
Some driver code:
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));

In reply to Win32::OLE and Excel by nop

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.