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

Hi, I am using ActivePerl 5.6. 1. How can I copy this excel and renamed it,and paste in this directory ? 2. How can I rename its sheet name and create more than one sheets ? 3. How can I operate(for example paste picture,write text etc.) appointed sheet ? I am a beginner. Will appreciate if anybody can help me with this. Thanks Guguqiaqia

Replies are listed 'Best First'.
Re: Excel using Perl ?
by Errto (Vicar) on Dec 28, 2004 at 02:53 UTC

    If you would like to manipulate an Excel file directly, you should use the module Win32::OLE. This will actually launch Excel in the background (you can verify this by looking for instances of EXCEL.EXE in the Task Manager) and do whatever changes you want to make. Some example code might be (for your question 2 (untested since I'm on Linux atm)):

    my $excel = Win32::OLE->new('Excel.Application'); my $workbook = $excel->Open("filenamehere.xls"); $workbook->Sheets[0]->{Name} = "New Sheet Name"; $workbook->Save(); $excel->Quit();

    The full documentation for this is found on MSDN online and available for a free download. Look for "Microsoft Excel Visual Basic Reference." You will obviously need to translate the examples from VB into Perl, and the documentation for Win32::OLE explains more clearly how to do that. Particularly look at the section on Office. As for caveats, note that this will only work on Windows and only on a machine that has Excel installed.

    For your question 1, I think you just want to copy a file under another name. This isn't really specific to Excel, so you should use the module File::Copy, which should be available on PPM if you don't have it already.

      Using Win32::OLE module is one of the better ways of doing excel automation on windows machine. But if the requirement is that you have to have more than one instance running and creating excel files , using SpreadSheet module available on CPAN the best bet.
Re: Excel using Perl ?
by gwhite (Friar) on Dec 28, 2004 at 01:45 UTC
    Use the module Spreadsheet::WriteExcel, you can't paste a picture, but you can create sheets, load cells and do most of your other requirements.
    g_White
Re: Excel using Perl ?
by murugu (Curate) on Dec 28, 2004 at 06:45 UTC