Some days ago, I've written a short example on how to access Excel with Win32::OLE. Maybe it might help you, although there's hardly any error handling:
#!perl -w use strict; use Win32::OLE; use Win32::OLE::Const 'Microsoft Excel'; $| = 1; # get actual path of script use FindBin qw($Bin); my $actualPath = $Bin; $actualPath =~ s|/|\\|g; # where shell excel file be saved to? my $file = "$actualPath\\test.xls"; # names of the sheets in ListReference my $sheets = ['Sheet1', 'Sheet2', 'Sheet3', 'Sheet4' ]; print "Creating new Excel-file: $file\n"; my ($excel, $book) = &ConnectToExcel(); $book->CreateSheets($sheets); $book->SaveAsFile($file); $book->Close; # ------------------------------------------------------------ sub ConnectToExcel { # get already open excel or open new my $excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit'); my $book = $excel->Workbooks->Add; # get new workbook $excel->{Visible} = 1; # to be able to see something; # set to 0 in production return ($excel, $book); # excel and the current workbook } # ConnectToExcel # ------------------------------------------------------------ package Win32::OLE; # additional methods for OLE... # ------------------------------------------------------------ sub CreateSheets { my ($book, $sheets) = @_; my @sheets = @$sheets; # how many worksheets are already available ? my $sheetsCount = $book->Worksheets->{Count}; print "$sheetsCount sheets available\n"; # Add some worksheets ( $#sheets - $sheetsCount ) for (0..($#sheets - $sheetsCount)){ print "Add new worksheet: $_\n"; $book->Worksheets->Add(); sleep(1); } # count again $sheetsCount = $book->Worksheets->{Count}; print "Now $sheetsCount Worksheets available\n"; foreach (0..$sheetsCount-1){ my $sheet = $book->Worksheets($_ + 1); my $oldName = $sheet->{Name}; print "Renaming worksheet from $oldName to $sheets[$_]\n"; $sheet->{Name} = $sheets[$_]; # rename it $sheet->Select(); # display it sleep(1); # pause } # write some values to somewhere ... print "write some stuff to Sheet 2\n"; $book->Worksheets(2)->Select; sleep(1); $book->Worksheets(2)->Range("A1")->{'Value'} = 'Hello'; sleep(1); $book->Worksheets(2)->Range("A2:D2")->{'Value'} = [ qw(I am a test) ]; sleep(1); print "write some stuff to sheet 1\n"; $book->Worksheets(1)->Select; $book->Worksheets(1)->Range("B2")->{'Value'} = 'Hallo'; sleep 1; $book->Worksheets(1)->Range("B2")->{'Font'}->{'Bold'} = 1; #bold sleep(3); # pause bevore saving } # CreateSheets # ------------------------------------------------------------ sub SaveAsFile { my ($book, $file) = @_; unlink ($file); $book->SaveAs($file); print "Saved as $file\n"; } # SaveAsFile # ------------------------------------------------------------
When I started automating excel, the Macrorecorder helped me quite a lot finding out about methods, properties and so on.

Best regards,
perl -e "print a|r,p|d=>b|p=>chr 3**2 .7=>t and t"


In reply to Re: Perl to Excel? by strat
in thread Perl to Excel? by Dinosaur

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.