Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Perl to Excel?

by Dinosaur (Beadle)
on Dec 13, 2001 at 22:42 UTC ( [id://131699]=perlquestion: print w/replies, xml ) Need Help??

Dinosaur has asked for the wisdom of the Perl Monks concerning the following question:

I process a lot of system instrumentation data with perl, to generate reports containing summaries, trends, errors and so on. I'm happy enough scanning columns of numbers, but a lot of my audience favour graphs of some of that data. At present I output .csv files and import them into Excel, but the report structures are getting a bit complex, and I prefer to do the heavy lifting in perl, rather than on the Excel side.

I've rummaged about CPAN looking for a module which gives me hands-on control of the contents of a spreadsheet, but found nothing.

I'd like to be able to create the whole spreadsheet from scratch starting from open/new, lay out its internal structures, then populate it with my data.

Any pointers would be warmly received.

--Dinosaur

Replies are listed 'Best First'.
Re: Perl to Excel?
by Beatnik (Parson) on Dec 13, 2001 at 22:45 UTC
Re: Perl to Excel?
by cacharbe (Curate) on Dec 13, 2001 at 22:52 UTC
    Please go have a look at my scratchpad. It's a tutorial I'm working on for PM.

    It does assume, however, that you are running on Win32.

    C-.

Re: Perl to Excel?
by jmcnamara (Monsignor) on Dec 13, 2001 at 23:06 UTC

    If you want to use Perl to generate graphs in Excel or use pre-prepared templates then you will have to use the Win32::OLE module on Windows. You will also require an installed copy of Excel.

    See here and here for some examples.

    Spreadsheet::WriteExcel allows you to create new Excel files on a variety of platforms but you cannot use it to extend an existing file or to create graphs.

    --
    John.

Re: Perl to Excel?
by dragonchild (Archbishop) on Dec 13, 2001 at 22:53 UTC
    Spreadsheet::ParseExcel will have issues with very large spreadsheets. We commented out a number of things he was creating and found a 50-75% savings in RAM usage.

    Also, ParseExcel and WriteExcel have complementary issues with formulas. ParseExcel will only read the value and WriteExcel will only write the formula. Excel itself can support either. Just don't be surprised if you use Excel as an intermediate format and get burned.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: Perl to Excel?
by ryddler (Monk) on Dec 14, 2001 at 03:43 UTC
    If the only reason you are using excel is to output charts, then you might want to take a look at GD::Graph and GD::Graph3d which can create graphs on the fly and save them out as any image format that GD supports (and suitable for direct output to a web browser if you like). Not only would you lose the requirement for excel (and license), these modules are also cross platform

    ryddler
Re: Perl to Excel?
by strat (Canon) on Dec 14, 2001 at 14:56 UTC
    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"

Re: Perl to Excel?
by strat (Canon) on Dec 14, 2001 at 15:30 UTC
    By the way: if you want to use an excel workbook in the way of a database, you could access Excel via Win32::ODBC or DBI and DBD::ODBC. An excelsheet is a table, and the name of this sheet plus a $ at the end is the name of the table, e.g. SELECT * FROM Sheet1$

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

Re: Perl to Excel?
by Dinosaur (Beadle) on Dec 14, 2001 at 02:27 UTC

    Thank you all very much for the pointers and (especially) the examples. I promise to reexamine my CPAN search procedures in light of them.

    Should've specified: I and my audience have Excel installed, on Windows boxes. However, the data originates on a Unix system. Given that something, either raw data or spreadsheet, has to cross that interface, I'm leaning toward a Win32::OLE approach, running perl on the Windows side. From a quick read of the docs, that module will have a larger learning curve than Spreadsheet::WriteExcel, but will more flexible in the long run.

    Wish me luck and, again, thanks!

    --Dinosaur

      You can build the spreadsheets and move them to your Windows environment (via FTP or whatever.) I don't think that there is a difference in writing. If it is excel format then excel should be able to read it no matter where it originated.

      Sparky

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://131699]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (9)
As of 2024-04-19 08:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found