in reply to Perl to Excel?
When I started automating excel, the Macrorecorder helped me quite a lot finding out about methods, properties and so on.#!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 # ------------------------------------------------------------
Best regards,
perl -e "print a|r,p|d=>b|p=>chr 3**2 .7=>t and t"
|
|---|